blob: 976cef39a21bfc66f1518bde10190e1d06a0de74 [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);
Eli Friedman6f84fed2011-11-08 01:43:53 +0000417 }
418
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000419 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000420 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000421
Bob Wilson2e076c42009-06-22 23:27:02 +0000422 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000423 addDRTypeForNEON(MVT::v2f32);
424 addDRTypeForNEON(MVT::v8i8);
425 addDRTypeForNEON(MVT::v4i16);
426 addDRTypeForNEON(MVT::v2i32);
427 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000428
Owen Anderson9f944592009-08-11 20:47:22 +0000429 addQRTypeForNEON(MVT::v4f32);
430 addQRTypeForNEON(MVT::v2f64);
431 addQRTypeForNEON(MVT::v16i8);
432 addQRTypeForNEON(MVT::v8i16);
433 addQRTypeForNEON(MVT::v4i32);
434 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000435
Bob Wilson194a2512009-09-15 23:55:57 +0000436 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
437 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000438 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
439 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000440 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
441 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
442 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000443 // FIXME: Code duplication: FDIV and FREM are expanded always, see
444 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000445 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
446 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000447 // FIXME: Create unittest.
448 // In another words, find a way when "copysign" appears in DAG with vector
449 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000450 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000451 // FIXME: Code duplication: SETCC has custom operation action, see
452 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000453 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000454 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000455 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
456 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
457 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
458 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
459 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
460 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
461 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
462 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
463 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
464 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
465 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
466 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000467 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000468 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
469 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
470 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
471 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
472 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000473 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000474
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000475 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
476 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
477 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
478 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
479 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
480 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
481 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
482 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
483 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
484 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000485 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
486 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
487 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
488 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000489 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000490
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000491 // Mark v2f32 intrinsics.
492 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
493 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
494 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
495 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
496 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
497 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
498 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
499 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
500 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
501 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
502 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
503 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
504 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
505 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
506 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
507
Bob Wilson6cc46572009-09-16 00:32:15 +0000508 // Neon does not support some operations on v1i64 and v2i64 types.
509 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000510 // Custom handling for some quad-vector types to detect VMULL.
511 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
512 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
513 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000514 // Custom handling for some vector types to avoid expensive expansions
515 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
516 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
517 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
518 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000519 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
520 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000521 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000522 // a destination type that is wider than the source, and nor does
523 // it have a FP_TO_[SU]INT instruction with a narrower destination than
524 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000525 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
526 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000527 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
528 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000529
Eli Friedmane6385e62012-11-15 22:44:27 +0000530 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000531 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000532
Evan Chengb4eae132012-12-04 22:41:50 +0000533 // NEON does not have single instruction CTPOP for vectors with element
534 // types wider than 8-bits. However, custom lowering can leverage the
535 // v8i8/v16i8 vcnt instruction.
536 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
537 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
538 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
539 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
540
Jim Grosbach5f215872013-02-27 21:31:12 +0000541 // NEON only has FMA instructions as of VFP4.
542 if (!Subtarget->hasVFP4()) {
543 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
544 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
545 }
546
Bob Wilson06fce872011-02-07 17:43:21 +0000547 setTargetDAGCombine(ISD::INTRINSIC_VOID);
548 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000549 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
550 setTargetDAGCombine(ISD::SHL);
551 setTargetDAGCombine(ISD::SRL);
552 setTargetDAGCombine(ISD::SRA);
553 setTargetDAGCombine(ISD::SIGN_EXTEND);
554 setTargetDAGCombine(ISD::ZERO_EXTEND);
555 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsonc6c13a32010-02-18 06:05:53 +0000556 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000557 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000558 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000559 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
560 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000561 setTargetDAGCombine(ISD::FP_TO_SINT);
562 setTargetDAGCombine(ISD::FP_TO_UINT);
563 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem097106b2011-10-15 20:03:12 +0000564
James Molloy547d4c02012-02-20 09:24:05 +0000565 // It is legal to extload from v4i8 to v4i16 or v4i32.
566 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
567 MVT::v4i16, MVT::v2i16,
568 MVT::v2i32};
569 for (unsigned i = 0; i < 6; ++i) {
570 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
571 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
572 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
573 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000574 }
575
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000576 // ARM and Thumb2 support UMLAL/SMLAL.
577 if (!Subtarget->isThumb1Only())
578 setTargetDAGCombine(ISD::ADDC);
579
580
Evan Cheng6addd652007-05-18 00:19:34 +0000581 computeRegisterProperties();
Evan Cheng10043e22007-01-19 07:51:42 +0000582
583 // ARM does not have f32 extending load.
Owen Anderson9f944592009-08-11 20:47:22 +0000584 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000585
Duncan Sands95d46ef2008-01-23 20:39:46 +0000586 // ARM does not have i1 sign extending load.
Owen Anderson9f944592009-08-11 20:47:22 +0000587 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000588
Evan Cheng10043e22007-01-19 07:51:42 +0000589 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000590 if (!Subtarget->isThumb1Only()) {
591 for (unsigned im = (unsigned)ISD::PRE_INC;
592 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000593 setIndexedLoadAction(im, MVT::i1, Legal);
594 setIndexedLoadAction(im, MVT::i8, Legal);
595 setIndexedLoadAction(im, MVT::i16, Legal);
596 setIndexedLoadAction(im, MVT::i32, Legal);
597 setIndexedStoreAction(im, MVT::i1, Legal);
598 setIndexedStoreAction(im, MVT::i8, Legal);
599 setIndexedStoreAction(im, MVT::i16, Legal);
600 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000601 }
Evan Cheng10043e22007-01-19 07:51:42 +0000602 }
603
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000604 setOperationAction(ISD::SADDO, MVT::i32, Custom);
605 setOperationAction(ISD::UADDO, MVT::i32, Custom);
606 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
607 setOperationAction(ISD::USUBO, MVT::i32, Custom);
608
Evan Cheng10043e22007-01-19 07:51:42 +0000609 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000610 setOperationAction(ISD::MUL, MVT::i64, Expand);
611 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000612 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000613 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
614 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000615 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000616 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
617 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000618 setOperationAction(ISD::MULHS, MVT::i32, Expand);
619
Jim Grosbach5d994042009-10-31 19:38:01 +0000620 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000621 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000622 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000623 setOperationAction(ISD::SRL, MVT::i64, Custom);
624 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000625
Evan Chenge8916542011-08-30 01:34:54 +0000626 if (!Subtarget->isThumb1Only()) {
627 // FIXME: We should do this for Thumb1 as well.
628 setOperationAction(ISD::ADDC, MVT::i32, Custom);
629 setOperationAction(ISD::ADDE, MVT::i32, Custom);
630 setOperationAction(ISD::SUBC, MVT::i32, Custom);
631 setOperationAction(ISD::SUBE, MVT::i32, Custom);
632 }
633
Evan Cheng10043e22007-01-19 07:51:42 +0000634 // ARM does not have ROTL.
Owen Anderson9f944592009-08-11 20:47:22 +0000635 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach8546ec92010-01-18 19:58:49 +0000636 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000637 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000638 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000639 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000640
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000641 // These just redirect to CTTZ and CTLZ on ARM.
642 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
643 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
644
Tim Northoverbc933082013-05-23 19:11:20 +0000645 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
646
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000647 // Only ARMv6 has BSWAP.
648 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000649 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000650
Bob Wilsone8a549c2012-09-29 21:43:49 +0000651 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
652 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
653 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbach92d999002010-05-05 20:44:35 +0000654 setOperationAction(ISD::SDIV, MVT::i32, Expand);
655 setOperationAction(ISD::UDIV, MVT::i32, Expand);
656 }
Renato Golin87610692013-07-16 09:32:17 +0000657
658 // FIXME: Also set divmod for SREM on EABI
Owen Anderson9f944592009-08-11 20:47:22 +0000659 setOperationAction(ISD::SREM, MVT::i32, Expand);
660 setOperationAction(ISD::UREM, MVT::i32, Expand);
Renato Golin87610692013-07-16 09:32:17 +0000661 // Register based DivRem for AEABI (RTABI 4.2)
662 if (Subtarget->isTargetAEABI()) {
663 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
664 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
665 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
666 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
667 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
668 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
669 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
670 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
671
672 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
673 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
674 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
675 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
676 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
677 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
678 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
679 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
680
681 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
682 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
683 } else {
684 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
685 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
686 }
Bob Wilson7117a912009-03-20 22:42:55 +0000687
Owen Anderson9f944592009-08-11 20:47:22 +0000688 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
689 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
690 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
691 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000692 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000693
Evan Cheng74d92c12011-04-08 21:37:21 +0000694 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000695
Evan Cheng10043e22007-01-19 07:51:42 +0000696 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000697 setOperationAction(ISD::VASTART, MVT::Other, Custom);
698 setOperationAction(ISD::VAARG, MVT::Other, Expand);
699 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
700 setOperationAction(ISD::VAEND, MVT::Other, Expand);
701 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
702 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000703
Tim Northoverd6a729b2014-01-06 14:28:05 +0000704 if (!Subtarget->isTargetMachO()) {
705 // Non-MachO platforms may return values in these registers via the
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000706 // personality function.
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000707 setExceptionPointerRegister(ARM::R0);
708 setExceptionSelectorRegister(ARM::R1);
709 }
Anton Korobeynikovf3a62312011-01-24 22:38:45 +0000710
Evan Chengf7f97b42010-04-15 22:20:34 +0000711 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng6e809de2010-08-11 06:22:01 +0000712 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
713 // the default expansion.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000714 if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000715 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
716 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000717 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000718
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000719 // On v8, we have particularly efficient implementations of atomic fences
720 // if they can be combined with nearby atomic loads and stores.
721 if (!Subtarget->hasV8Ops()) {
722 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
723 setInsertFencesForAtomic(true);
724 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000725 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000726 // If there's anything we can use as a barrier, go through custom lowering
727 // for ATOMIC_FENCE.
728 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
729 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
730
Jim Grosbach6860bb72010-06-18 22:35:32 +0000731 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000732 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000733 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000734 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000735 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000736 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000737 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000738 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000739 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000740 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000741 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000742 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000743 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000744 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
745 // Unordered/Monotonic case.
746 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
747 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000748 }
Evan Cheng10043e22007-01-19 07:51:42 +0000749
Evan Cheng21acf9f2010-11-04 05:19:35 +0000750 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000751
Eli Friedman8cfa7712010-06-26 04:36:50 +0000752 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
753 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000754 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
755 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000756 }
Owen Anderson9f944592009-08-11 20:47:22 +0000757 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000758
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000759 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
760 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000761 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000762 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000763 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000764 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
765 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000766
767 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000768 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbach31984832010-07-07 00:07:57 +0000769 if (Subtarget->isTargetDarwin()) {
770 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
771 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall7d84ece2011-05-29 19:50:32 +0000772 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbach31984832010-07-07 00:07:57 +0000773 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000774
Owen Anderson9f944592009-08-11 20:47:22 +0000775 setOperationAction(ISD::SETCC, MVT::i32, Expand);
776 setOperationAction(ISD::SETCC, MVT::f32, Expand);
777 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000778 setOperationAction(ISD::SELECT, MVT::i32, Custom);
779 setOperationAction(ISD::SELECT, MVT::f32, Custom);
780 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000781 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
782 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
783 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000784
Owen Anderson9f944592009-08-11 20:47:22 +0000785 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
786 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
787 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
788 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
789 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000790
Dan Gohman482732a2007-10-11 23:21:31 +0000791 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000792 setOperationAction(ISD::FSIN, MVT::f64, Expand);
793 setOperationAction(ISD::FSIN, MVT::f32, Expand);
794 setOperationAction(ISD::FCOS, MVT::f32, Expand);
795 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000796 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
797 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000798 setOperationAction(ISD::FREM, MVT::f64, Expand);
799 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000800 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
801 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000802 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
803 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000804 }
Owen Anderson9f944592009-08-11 20:47:22 +0000805 setOperationAction(ISD::FPOW, MVT::f64, Expand);
806 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000807
Evan Chengd0007f32012-04-10 21:40:28 +0000808 if (!Subtarget->hasVFP4()) {
809 setOperationAction(ISD::FMA, MVT::f64, Expand);
810 setOperationAction(ISD::FMA, MVT::f32, Expand);
811 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000812
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000813 // Various VFP goodness
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000814 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilsone4191e72010-03-19 22:51:32 +0000815 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
816 if (Subtarget->hasVFP2()) {
817 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
818 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
819 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
820 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
821 }
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000822 // Special handling for half-precision FP.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000823 if (!Subtarget->hasFP16()) {
824 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
825 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000826 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000827 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000828
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000829 // Combine sin / cos into one node or libcall if possible.
830 if (Subtarget->hasSinCos()) {
831 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
832 setLibcallName(RTLIB::SINCOS_F64, "sincos");
833 if (Subtarget->getTargetTriple().getOS() == Triple::IOS) {
834 // For iOS, we don't want to the normal expansion of a libcall to
835 // sincos. We want to issue a libcall to __sincos_stret.
836 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
837 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
838 }
839 }
Evan Cheng10043e22007-01-19 07:51:42 +0000840
Chris Lattnerf3f4ad92007-11-27 22:36:16 +0000841 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000842 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +0000843 setTargetDAGCombine(ISD::ADD);
844 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +0000845 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +0000846 setTargetDAGCombine(ISD::AND);
847 setTargetDAGCombine(ISD::OR);
848 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +0000849
Evan Chengf258a152012-02-23 02:58:19 +0000850 if (Subtarget->hasV6Ops())
851 setTargetDAGCombine(ISD::SRL);
852
Evan Cheng10043e22007-01-19 07:51:42 +0000853 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +0000854
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000855 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
856 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +0000857 setSchedulingPreference(Sched::RegPressure);
858 else
859 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +0000860
Evan Cheng3ae2b792011-01-06 06:52:41 +0000861 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000862 MaxStoresPerMemset = 8;
863 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
864 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
865 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
866 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
867 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
Evan Chengb71233f2010-06-26 01:52:05 +0000868
Rafael Espindolaa76eccf2010-07-11 04:01:49 +0000869 // On ARM arguments smaller than 4 bytes are extended, so all arguments
870 // are at least 4 bytes aligned.
871 setMinStackArgumentAlignment(4);
872
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000873 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000874 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000875
Eli Friedman2518f832011-05-06 20:34:06 +0000876 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +0000877}
878
Andrew Trick43f25632011-01-19 02:35:27 +0000879// FIXME: It might make sense to define the representative register class as the
880// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
881// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
882// SPR's representative would be DPR_VFP2. This should work well if register
883// pressure tracking were modified such that a register use would increment the
884// pressure of the register class's representative and all of it's super
885// classes' representatives transitively. We have not implemented this because
886// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000887// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +0000888// and extractions.
Evan Chenga77f3d32010-07-21 06:09:07 +0000889std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000890ARMTargetLowering::findRepresentativeClass(MVT VT) const{
Craig Topper062a2ba2014-04-25 05:30:21 +0000891 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +0000892 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000893 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +0000894 default:
Evan Chenga77f3d32010-07-21 06:09:07 +0000895 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng28590382010-07-21 23:53:58 +0000896 // Use DPR as representative register class for all floating point
897 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
898 // the cost is 1 for both f32 and f64.
899 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +0000900 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +0000901 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +0000902 // When NEON is used for SP, only half of the register file is available
903 // because operations that define both SP and DP results will be constrained
904 // to the VFP2 class (D0-D15). We currently model this constraint prior to
905 // coalescing by double-counting the SP regs. See the FIXME above.
906 if (Subtarget->useNEONForSinglePrecisionFP())
907 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000908 break;
909 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
910 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +0000911 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000912 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000913 break;
914 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +0000915 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000916 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +0000917 break;
918 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +0000919 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000920 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +0000921 break;
Evan Cheng10f99a32010-07-19 22:15:08 +0000922 }
Evan Chenga77f3d32010-07-21 06:09:07 +0000923 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +0000924}
925
Evan Cheng10043e22007-01-19 07:51:42 +0000926const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
927 switch (Opcode) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000928 default: return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +0000929 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +0000930 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +0000931 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
932 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +0000933 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +0000934 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
935 case ARMISD::tCALL: return "ARMISD::tCALL";
936 case ARMISD::BRCOND: return "ARMISD::BRCOND";
937 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +0000938 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +0000939 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +0000940 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +0000941 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
942 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +0000943 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +0000944 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +0000945 case ARMISD::CMPFP: return "ARMISD::CMPFP";
946 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +0000947 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +0000948 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +0000949
Evan Cheng10043e22007-01-19 07:51:42 +0000950 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +0000951
Jim Grosbach8546ec92010-01-18 19:58:49 +0000952 case ARMISD::RBIT: return "ARMISD::RBIT";
953
Bob Wilsone4191e72010-03-19 22:51:32 +0000954 case ARMISD::FTOSI: return "ARMISD::FTOSI";
955 case ARMISD::FTOUI: return "ARMISD::FTOUI";
956 case ARMISD::SITOF: return "ARMISD::SITOF";
957 case ARMISD::UITOF: return "ARMISD::UITOF";
958
Evan Cheng10043e22007-01-19 07:51:42 +0000959 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
960 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
961 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +0000962
Evan Chenge8916542011-08-30 01:34:54 +0000963 case ARMISD::ADDC: return "ARMISD::ADDC";
964 case ARMISD::ADDE: return "ARMISD::ADDE";
965 case ARMISD::SUBC: return "ARMISD::SUBC";
966 case ARMISD::SUBE: return "ARMISD::SUBE";
967
Bob Wilson22806742010-09-22 22:09:21 +0000968 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
969 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +0000970
Evan Chengec6d7c92009-10-28 06:55:03 +0000971 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
972 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
973
Dale Johannesend679ff72010-06-03 21:09:53 +0000974 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +0000975
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +0000976 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +0000977
Evan Chengb972e562009-08-07 00:34:42 +0000978 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
979
Bob Wilson7ed59712010-10-30 00:54:37 +0000980 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +0000981
Evan Cheng8740ee32010-11-03 06:34:55 +0000982 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
983
Bob Wilson2e076c42009-06-22 23:27:02 +0000984 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +0000985 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +0000986 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +0000987 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
988 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +0000989 case ARMISD::VCGEU: return "ARMISD::VCGEU";
990 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +0000991 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
992 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +0000993 case ARMISD::VCGTU: return "ARMISD::VCGTU";
994 case ARMISD::VTST: return "ARMISD::VTST";
995
996 case ARMISD::VSHL: return "ARMISD::VSHL";
997 case ARMISD::VSHRs: return "ARMISD::VSHRs";
998 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +0000999 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1000 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1001 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1002 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1003 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1004 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1005 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1006 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1007 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1008 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1009 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1010 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
1011 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1012 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001013 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001014 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001015 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001016 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001017 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001018 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001019 case ARMISD::VREV64: return "ARMISD::VREV64";
1020 case ARMISD::VREV32: return "ARMISD::VREV32";
1021 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001022 case ARMISD::VZIP: return "ARMISD::VZIP";
1023 case ARMISD::VUZP: return "ARMISD::VUZP";
1024 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001025 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1026 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001027 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1028 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001029 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1030 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001031 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilsonc6c13a32010-02-18 06:05:53 +00001032 case ARMISD::FMAX: return "ARMISD::FMAX";
1033 case ARMISD::FMIN: return "ARMISD::FMIN";
Joey Goulye3dd6842013-08-23 12:01:13 +00001034 case ARMISD::VMAXNM: return "ARMISD::VMAX";
1035 case ARMISD::VMINNM: return "ARMISD::VMIN";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001036 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001037 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1038 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001039 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilson2d790df2010-11-28 06:51:26 +00001040 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1041 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1042 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001043 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1044 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1045 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1046 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1047 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1048 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1049 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1050 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1051 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1052 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1053 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1054 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1055 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1056 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1057 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1058 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1059 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001060 }
1061}
1062
Matt Arsenault758659232013-05-18 00:21:46 +00001063EVT ARMTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Duncan Sandsf2641e12011-09-06 19:07:46 +00001064 if (!VT.isVector()) return getPointerTy();
1065 return VT.changeVectorElementTypeToInteger();
1066}
1067
Evan Cheng4cad68e2010-05-15 02:18:07 +00001068/// getRegClassFor - Return the register class that should be used for the
1069/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001070const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001071 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1072 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1073 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001074 if (Subtarget->hasNEON()) {
1075 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001076 return &ARM::QQPRRegClass;
1077 if (VT == MVT::v8i64)
1078 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001079 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001080 return TargetLowering::getRegClassFor(VT);
1081}
1082
Eric Christopher84bdfd82010-07-21 22:26:11 +00001083// Create a fast isel object.
1084FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001085ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1086 const TargetLibraryInfo *libInfo) const {
1087 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001088}
1089
Anton Korobeynikov19edda02010-07-24 21:52:08 +00001090/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1091/// be used for loads / stores from the global.
1092unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1093 return (Subtarget->isThumb1Only() ? 127 : 4095);
1094}
1095
Evan Cheng4401f882010-05-20 23:26:43 +00001096Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001097 unsigned NumVals = N->getNumValues();
1098 if (!NumVals)
1099 return Sched::RegPressure;
1100
1101 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001102 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001103 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001104 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001105 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001106 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001107 }
Evan Chengbf914992010-05-28 23:25:23 +00001108
1109 if (!N->isMachineOpcode())
1110 return Sched::RegPressure;
1111
1112 // Load are scheduled for latency even if there instruction itinerary
1113 // is not available.
1114 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001115 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001116
Evan Cheng6cc775f2011-06-28 19:10:37 +00001117 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001118 return Sched::RegPressure;
1119 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001120 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001121 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001122
Evan Cheng4401f882010-05-20 23:26:43 +00001123 return Sched::RegPressure;
1124}
1125
Evan Cheng10043e22007-01-19 07:51:42 +00001126//===----------------------------------------------------------------------===//
1127// Lowering Code
1128//===----------------------------------------------------------------------===//
1129
Evan Cheng10043e22007-01-19 07:51:42 +00001130/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1131static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1132 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001133 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001134 case ISD::SETNE: return ARMCC::NE;
1135 case ISD::SETEQ: return ARMCC::EQ;
1136 case ISD::SETGT: return ARMCC::GT;
1137 case ISD::SETGE: return ARMCC::GE;
1138 case ISD::SETLT: return ARMCC::LT;
1139 case ISD::SETLE: return ARMCC::LE;
1140 case ISD::SETUGT: return ARMCC::HI;
1141 case ISD::SETUGE: return ARMCC::HS;
1142 case ISD::SETULT: return ARMCC::LO;
1143 case ISD::SETULE: return ARMCC::LS;
1144 }
1145}
1146
Bob Wilsona2e83332009-09-09 23:14:54 +00001147/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1148static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001149 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001150 CondCode2 = ARMCC::AL;
1151 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001152 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001153 case ISD::SETEQ:
1154 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1155 case ISD::SETGT:
1156 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1157 case ISD::SETGE:
1158 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1159 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001160 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001161 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1162 case ISD::SETO: CondCode = ARMCC::VC; break;
1163 case ISD::SETUO: CondCode = ARMCC::VS; break;
1164 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1165 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1166 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1167 case ISD::SETLT:
1168 case ISD::SETULT: CondCode = ARMCC::LT; break;
1169 case ISD::SETLE:
1170 case ISD::SETULE: CondCode = ARMCC::LE; break;
1171 case ISD::SETNE:
1172 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1173 }
Evan Cheng10043e22007-01-19 07:51:42 +00001174}
1175
Bob Wilsona4c22902009-04-17 19:07:39 +00001176//===----------------------------------------------------------------------===//
1177// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001178//===----------------------------------------------------------------------===//
1179
1180#include "ARMGenCallingConv.inc"
1181
Oliver Stannardc24f2172014-05-09 14:01:47 +00001182/// getEffectiveCallingConv - Get the effective calling convention, taking into
1183/// account presence of floating point hardware and calling convention
1184/// limitations, such as support for variadic functions.
1185CallingConv::ID
1186ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1187 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001188 switch (CC) {
1189 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001190 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001191 case CallingConv::ARM_AAPCS:
1192 case CallingConv::ARM_APCS:
1193 case CallingConv::GHC:
1194 return CC;
1195 case CallingConv::ARM_AAPCS_VFP:
1196 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1197 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001198 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001199 return CallingConv::ARM_APCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001200 else if (Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001201 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1202 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001203 return CallingConv::ARM_AAPCS_VFP;
1204 else
1205 return CallingConv::ARM_AAPCS;
1206 case CallingConv::Fast:
1207 if (!Subtarget->isAAPCS_ABI()) {
1208 if (Subtarget->hasVFP2() && !isVarArg)
1209 return CallingConv::Fast;
1210 return CallingConv::ARM_APCS;
1211 } else if (Subtarget->hasVFP2() && !isVarArg)
1212 return CallingConv::ARM_AAPCS_VFP;
1213 else
1214 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001215 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001216}
1217
1218/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1219/// CallingConvention.
1220CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1221 bool Return,
1222 bool isVarArg) const {
1223 switch (getEffectiveCallingConv(CC, isVarArg)) {
1224 default:
1225 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001226 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001227 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001228 case CallingConv::ARM_AAPCS:
1229 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1230 case CallingConv::ARM_AAPCS_VFP:
1231 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1232 case CallingConv::Fast:
1233 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001234 case CallingConv::GHC:
1235 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001236 }
1237}
1238
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001239/// LowerCallResult - Lower the result values of a call into the
1240/// appropriate copies out of appropriate physical registers.
1241SDValue
1242ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001243 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001244 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001245 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001246 SmallVectorImpl<SDValue> &InVals,
1247 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001248
Bob Wilsona4c22902009-04-17 19:07:39 +00001249 // Assign locations to each value returned by this call.
1250 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00001251 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1252 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001253 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001254 CCAssignFnForNode(CallConv, /* Return*/ true,
1255 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001256
1257 // Copy all of the result registers out of their specified physreg.
1258 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1259 CCValAssign VA = RVLocs[i];
1260
Stephen Linb8bd2322013-04-20 05:14:40 +00001261 // Pass 'this' value directly from the argument to return value, to avoid
1262 // reg unit interference
1263 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001264 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1265 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001266 InVals.push_back(ThisVal);
1267 continue;
1268 }
1269
Bob Wilson0041bd32009-04-25 00:33:20 +00001270 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001271 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001272 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001273 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001274 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001275 Chain = Lo.getValue(1);
1276 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001277 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001278 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001279 InFlag);
1280 Chain = Hi.getValue(1);
1281 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001282 if (!Subtarget->isLittle())
1283 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001284 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001285
Owen Anderson9f944592009-08-11 20:47:22 +00001286 if (VA.getLocVT() == MVT::v2f64) {
1287 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1288 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1289 DAG.getConstant(0, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001290
1291 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001292 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001293 Chain = Lo.getValue(1);
1294 InFlag = Lo.getValue(2);
1295 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001296 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001297 Chain = Hi.getValue(1);
1298 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001299 if (!Subtarget->isLittle())
1300 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001301 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001302 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1303 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001304 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001305 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001306 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1307 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001308 Chain = Val.getValue(1);
1309 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001310 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001311
1312 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001313 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001314 case CCValAssign::Full: break;
1315 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001316 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001317 break;
1318 }
1319
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001320 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001321 }
1322
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001323 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001324}
1325
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001326/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001327SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001328ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1329 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001330 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001331 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001332 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001333 unsigned LocMemOffset = VA.getLocMemOffset();
1334 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1335 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilsona4c22902009-04-17 19:07:39 +00001336 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner886250c2010-09-21 18:51:21 +00001337 MachinePointerInfo::getStack(LocMemOffset),
David Greene0d0149f2010-02-15 16:55:24 +00001338 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001339}
1340
Andrew Trickef9de2a2013-05-25 02:42:55 +00001341void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001342 SDValue Chain, SDValue &Arg,
1343 RegsToPassVector &RegsToPass,
1344 CCValAssign &VA, CCValAssign &NextVA,
1345 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001346 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001347 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001348
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001349 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001350 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001351 unsigned id = Subtarget->isLittle() ? 0 : 1;
1352 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001353
1354 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001355 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001356 else {
1357 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001358 if (!StackPtr.getNode())
Bob Wilson2e076c42009-06-22 23:27:02 +00001359 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1360
Christian Pirkerb5728192014-05-08 14:06:24 +00001361 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001362 dl, DAG, NextVA,
1363 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001364 }
1365}
1366
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001367/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001368/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1369/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001370SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001371ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001372 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001373 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001374 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001375 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1376 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1377 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001378 SDValue Chain = CLI.Chain;
1379 SDValue Callee = CLI.Callee;
1380 bool &isTailCall = CLI.IsTailCall;
1381 CallingConv::ID CallConv = CLI.CallConv;
1382 bool doesNotRet = CLI.DoesNotReturn;
1383 bool isVarArg = CLI.IsVarArg;
1384
Dale Johannesend679ff72010-06-03 21:09:53 +00001385 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001386 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1387 bool isThisReturn = false;
1388 bool isSibCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001389
Bob Wilson8decdc42011-10-07 17:17:49 +00001390 // Disable tail calls if they're not supported.
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001391 if (!Subtarget->supportsTailCall() || MF.getTarget().Options.DisableTailCalls)
Bob Wilson3c9ed762010-08-13 22:43:33 +00001392 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001393
Dale Johannesend679ff72010-06-03 21:09:53 +00001394 if (isTailCall) {
1395 // Check if it's really possible to do a tail call.
1396 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001397 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001398 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001399 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1400 report_fatal_error("failed to perform tail call elimination on a call "
1401 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001402 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1403 // detected sibcalls.
1404 if (isTailCall) {
1405 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001406 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001407 }
1408 }
Evan Cheng10043e22007-01-19 07:51:42 +00001409
Bob Wilsona4c22902009-04-17 19:07:39 +00001410 // Analyze operands of the call, assigning locations to each operand.
1411 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00001412 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1413 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001414 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001415 CCAssignFnForNode(CallConv, /* Return*/ false,
1416 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001417
Bob Wilsona4c22902009-04-17 19:07:39 +00001418 // Get a count of how many bytes are to be pushed on the stack.
1419 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001420
Dale Johannesend679ff72010-06-03 21:09:53 +00001421 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001422 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001423 NumBytes = 0;
1424
Evan Cheng10043e22007-01-19 07:51:42 +00001425 // Adjust the stack pointer for the new arguments...
1426 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001427 if (!isSibCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00001428 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
1429 dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001430
Jim Grosbach6ad4bcb2010-02-24 01:43:03 +00001431 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Cheng10043e22007-01-19 07:51:42 +00001432
Bob Wilson2e076c42009-06-22 23:27:02 +00001433 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001434 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001435
Bob Wilsona4c22902009-04-17 19:07:39 +00001436 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001437 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001438 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1439 i != e;
1440 ++i, ++realArgIdx) {
1441 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001442 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001443 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001444 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001445
Bob Wilsona4c22902009-04-17 19:07:39 +00001446 // Promote the value if needed.
1447 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001448 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001449 case CCValAssign::Full: break;
1450 case CCValAssign::SExt:
1451 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1452 break;
1453 case CCValAssign::ZExt:
1454 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1455 break;
1456 case CCValAssign::AExt:
1457 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1458 break;
1459 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001460 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001461 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001462 }
1463
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001464 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001465 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001466 if (VA.getLocVT() == MVT::v2f64) {
1467 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1468 DAG.getConstant(0, MVT::i32));
1469 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1470 DAG.getConstant(1, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001471
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001472 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001473 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1474
1475 VA = ArgLocs[++i]; // skip ahead to next loc
1476 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001477 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001478 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1479 } else {
1480 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001481
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001482 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1483 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001484 }
1485 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001486 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001487 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001488 }
1489 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001490 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1491 assert(VA.getLocVT() == MVT::i32 &&
1492 "unexpected calling convention register assignment");
1493 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001494 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001495 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001496 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001497 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001498 } else if (isByVal) {
1499 assert(VA.isMemLoc());
1500 unsigned offset = 0;
1501
1502 // True if this byval aggregate will be split between registers
1503 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001504 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
1505 unsigned CurByValIdx = CCInfo.getInRegsParamsProceed();
1506
1507 if (CurByValIdx < ByValArgsCount) {
1508
1509 unsigned RegBegin, RegEnd;
1510 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1511
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001512 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1513 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001514 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001515 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1516 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1517 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1518 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001519 false, false, false,
1520 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001521 MemOpChains.push_back(Load.getValue(1));
1522 RegsToPass.push_back(std::make_pair(j, Load));
1523 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001524
1525 // If parameter size outsides register area, "offset" value
1526 // helps us to calculate stack slot for remained part properly.
1527 offset = RegEnd - RegBegin;
1528
1529 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001530 }
1531
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001532 if (Flags.getByValSize() > 4*offset) {
Manman Ren9f911162012-06-01 02:44:42 +00001533 unsigned LocMemOffset = VA.getLocMemOffset();
1534 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1535 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1536 StkPtrOff);
1537 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1538 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1539 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1540 MVT::i32);
Manman Rene8735522012-06-01 19:33:18 +00001541 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001542
Manman Ren9f911162012-06-01 02:44:42 +00001543 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001544 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001545 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001546 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001547 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001548 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001549 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001550
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001551 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1552 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001553 }
Evan Cheng10043e22007-01-19 07:51:42 +00001554 }
1555
1556 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001557 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001558
1559 // Build a sequence of copy-to-reg nodes chained together with token chain
1560 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001561 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001562 // Tail call byval lowering might overwrite argument registers so in case of
1563 // tail call optimization the copies to registers are lowered later.
1564 if (!isTailCall)
1565 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1566 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1567 RegsToPass[i].second, InFlag);
1568 InFlag = Chain.getValue(1);
1569 }
Evan Cheng10043e22007-01-19 07:51:42 +00001570
Dale Johannesend679ff72010-06-03 21:09:53 +00001571 // For tail calls lower the arguments to the 'real' stack slot.
1572 if (isTailCall) {
1573 // Force all the incoming stack arguments to be loaded from the stack
1574 // before any new outgoing arguments are stored to the stack, because the
1575 // outgoing stack slots may alias the incoming argument stack slots, and
1576 // the alias isn't otherwise explicit. This is slightly more conservative
1577 // than necessary, because it means that each store effectively depends
1578 // on every argument instead of just those arguments it would clobber.
1579
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001580 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001581 InFlag = SDValue();
1582 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1583 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1584 RegsToPass[i].second, InFlag);
1585 InFlag = Chain.getValue(1);
1586 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001587 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001588 }
1589
Bill Wendling24c79f22008-09-16 21:48:12 +00001590 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1591 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1592 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001593 bool isDirect = false;
1594 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001595 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001596 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001597
1598 if (EnableARMLongCalls) {
1599 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1600 && "long-calls with non-static relocation model!");
1601 // Handle a global address or an external symbol. If it's not one of
1602 // those, the target's already in a register, so we don't need to do
1603 // anything extra.
1604 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001605 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001606 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001607 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001608 ARMConstantPoolValue *CPV =
1609 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1610
Jim Grosbach32bb3622010-04-14 22:28:31 +00001611 // Get the address of the callee into a register
1612 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1613 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1614 Callee = DAG.getLoad(getPointerTy(), dl,
1615 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001616 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001617 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001618 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1619 const char *Sym = S->getSymbol();
1620
1621 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001622 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001623 ARMConstantPoolValue *CPV =
1624 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1625 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001626 // Get the address of the callee into a register
1627 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1628 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1629 Callee = DAG.getLoad(getPointerTy(), dl,
1630 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001631 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001632 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001633 }
1634 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001635 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001636 isDirect = true;
Chris Lattner55452c22009-07-15 04:12:33 +00001637 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Tim Northoverd6a729b2014-01-06 14:28:05 +00001638 bool isStub = (isExt && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001639 getTargetMachine().getRelocationModel() != Reloc::Static;
1640 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc3c949b42007-06-19 21:05:09 +00001641 // ARM call to a local ARM function is predicable.
Evan Chengf128bdc2010-06-16 07:35:02 +00001642 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001643 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001644 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001645 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Tim Northover72360d22013-12-02 10:35:41 +00001646 Callee = DAG.getNode(ARMISD::WrapperPIC, dl, getPointerTy(),
1647 DAG.getTargetGlobalAddress(GV, dl, getPointerTy()));
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001648 } else {
1649 // On ELF targets for PIC code, direct calls should go through the PLT
1650 unsigned OpFlags = 0;
1651 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001652 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001653 OpFlags = ARMII::MO_PLT;
1654 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1655 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001656 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001657 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001658 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001659 getTargetMachine().getRelocationModel() != Reloc::Static;
1660 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng83f35172007-01-30 20:37:08 +00001661 // tBX takes a register source operand.
1662 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001663 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001664 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001665 ARMConstantPoolValue *CPV =
1666 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1667 ARMPCLabelIndex, 4);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001668 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001669 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen021052a2009-02-04 20:06:27 +00001670 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Chengcdbb70c2009-10-31 03:39:36 +00001671 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001672 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001673 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00001674 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson7117a912009-03-20 22:42:55 +00001675 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen021052a2009-02-04 20:06:27 +00001676 getPointerTy(), Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001677 } else {
1678 unsigned OpFlags = 0;
1679 // On ELF targets for PIC code, direct calls should go through the PLT
1680 if (Subtarget->isTargetELF() &&
1681 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1682 OpFlags = ARMII::MO_PLT;
1683 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1684 }
Evan Cheng10043e22007-01-19 07:51:42 +00001685 }
1686
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001687 // FIXME: handle tail calls differently.
1688 unsigned CallOpc;
Tim Northoverdee86042013-12-02 14:46:26 +00001689 bool HasMinSizeAttr = Subtarget->isMinSize();
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001690 if (Subtarget->isThumb()) {
1691 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001692 CallOpc = ARMISD::CALL_NOLINK;
1693 else
1694 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1695 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001696 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001697 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001698 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet8e1fe842012-11-02 21:32:17 +00001699 // Emit regular call when code size is the priority
1700 !HasMinSizeAttr)
Evan Cheng65f9d192012-02-28 18:51:51 +00001701 // "mov lr, pc; b _foo" to avoid confusing the RSP
1702 CallOpc = ARMISD::CALL_NOLINK;
1703 else
1704 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001705 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001706
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001707 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001708 Ops.push_back(Chain);
1709 Ops.push_back(Callee);
1710
1711 // Add argument registers to the end of the list so that they are known live
1712 // into the call.
1713 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1714 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1715 RegsToPass[i].second.getValueType()));
1716
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001717 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001718 if (!isTailCall) {
1719 const uint32_t *Mask;
1720 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1721 const ARMBaseRegisterInfo *ARI = static_cast<const ARMBaseRegisterInfo*>(TRI);
1722 if (isThisReturn) {
1723 // For 'this' returns, use the R0-preserving mask if applicable
1724 Mask = ARI->getThisReturnPreservedMask(CallConv);
1725 if (!Mask) {
1726 // Set isThisReturn to false if the calling convention is not one that
1727 // allows 'returned' to be modeled in this way, so LowerCallResult does
1728 // not try to pass 'this' straight through
1729 isThisReturn = false;
1730 Mask = ARI->getCallPreservedMask(CallConv);
1731 }
1732 } else
Stephen Linff7fcee2013-06-26 21:42:14 +00001733 Mask = ARI->getCallPreservedMask(CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001734
Matthias Braunc22630e2013-10-04 16:52:54 +00001735 assert(Mask && "Missing call preserved mask for calling convention");
1736 Ops.push_back(DAG.getRegisterMask(Mask));
1737 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001738
Gabor Greiff304a7a2008-08-28 21:40:38 +00001739 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001740 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001741
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001742 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001743 if (isTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001744 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Dale Johannesend679ff72010-06-03 21:09:53 +00001745
Duncan Sands739a0542008-07-02 17:40:58 +00001746 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001747 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001748 InFlag = Chain.getValue(1);
1749
Chris Lattner27539552008-10-11 22:08:30 +00001750 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001751 DAG.getIntPtrConstant(0, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001752 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001753 InFlag = Chain.getValue(1);
1754
Bob Wilsona4c22902009-04-17 19:07:39 +00001755 // Handle result values, copying them out of physregs into vregs that we
1756 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001757 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001758 InVals, isThisReturn,
1759 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001760}
1761
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001762/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001763/// on the stack. Remember the next parameter register to allocate,
1764/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001765/// this.
1766void
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001767ARMTargetLowering::HandleByVal(
1768 CCState *State, unsigned &size, unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001769 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1770 assert((State->getCallOrPrologue() == Prologue ||
1771 State->getCallOrPrologue() == Call) &&
1772 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001773
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001774 if ((ARM::R0 <= reg) && (reg <= ARM::R3)) {
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001775 if (Subtarget->isAAPCS_ABI() && Align > 4) {
1776 unsigned AlignInRegs = Align / 4;
1777 unsigned Waste = (ARM::R4 - reg) % AlignInRegs;
1778 for (unsigned i = 0; i < Waste; ++i)
1779 reg = State->AllocateReg(GPRArgRegs, 4);
1780 }
1781 if (reg != 0) {
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001782 unsigned excess = 4 * (ARM::R4 - reg);
1783
1784 // Special case when NSAA != SP and parameter size greater than size of
1785 // all remained GPR regs. In that case we can't split parameter, we must
1786 // send it to stack. We also must set NCRN to R4, so waste all
1787 // remained registers.
Oliver Stannardd55e1152014-03-05 15:25:27 +00001788 const unsigned NSAAOffset = State->getNextStackOffset();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001789 if (Subtarget->isAAPCS_ABI() && NSAAOffset != 0 && size > excess) {
1790 while (State->AllocateReg(GPRArgRegs, 4))
1791 ;
1792 return;
1793 }
1794
1795 // First register for byval parameter is the first register that wasn't
1796 // allocated before this method call, so it would be "reg".
1797 // If parameter is small enough to be saved in range [reg, r4), then
1798 // the end (first after last) register would be reg + param-size-in-regs,
1799 // else parameter would be splitted between registers and stack,
1800 // end register would be r4 in this case.
1801 unsigned ByValRegBegin = reg;
Stepan Dyatkovskiy2703bca2013-05-08 14:51:27 +00001802 unsigned ByValRegEnd = (size < excess) ? reg + size/4 : (unsigned)ARM::R4;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001803 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
1804 // Note, first register is allocated in the beginning of function already,
1805 // allocate remained amount of registers we need.
1806 for (unsigned i = reg+1; i != ByValRegEnd; ++i)
1807 State->AllocateReg(GPRArgRegs, 4);
Oliver Stannardd55e1152014-03-05 15:25:27 +00001808 // A byval parameter that is split between registers and memory needs its
1809 // size truncated here.
1810 // In the case where the entire structure fits in registers, we set the
1811 // size in memory to zero.
1812 if (size < excess)
1813 size = 0;
1814 else
1815 size -= excess;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001816 }
1817 }
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001818}
1819
Dale Johannesend679ff72010-06-03 21:09:53 +00001820/// MatchingStackOffset - Return true if the given stack call argument is
1821/// already available in the same position (relatively) of the caller's
1822/// incoming argument stack.
1823static
1824bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1825 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00001826 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00001827 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1828 int FI = INT_MAX;
1829 if (Arg.getOpcode() == ISD::CopyFromReg) {
1830 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001831 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00001832 return false;
1833 MachineInstr *Def = MRI->getVRegDef(VR);
1834 if (!Def)
1835 return false;
1836 if (!Flags.isByVal()) {
1837 if (!TII->isLoadFromStackSlot(Def, FI))
1838 return false;
1839 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00001840 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00001841 }
1842 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1843 if (Flags.isByVal())
1844 // ByVal argument is passed in as a pointer but it's now being
1845 // dereferenced. e.g.
1846 // define @foo(%struct.X* %A) {
1847 // tail call @bar(%struct.X* byval %A)
1848 // }
1849 return false;
1850 SDValue Ptr = Ld->getBasePtr();
1851 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1852 if (!FINode)
1853 return false;
1854 FI = FINode->getIndex();
1855 } else
1856 return false;
1857
1858 assert(FI != INT_MAX);
1859 if (!MFI->isFixedObjectIndex(FI))
1860 return false;
1861 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1862}
1863
1864/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1865/// for tail call optimization. Targets which want to do tail call
1866/// optimization should implement this function.
1867bool
1868ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1869 CallingConv::ID CalleeCC,
1870 bool isVarArg,
1871 bool isCalleeStructRet,
1872 bool isCallerStructRet,
1873 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001874 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00001875 const SmallVectorImpl<ISD::InputArg> &Ins,
1876 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00001877 const Function *CallerF = DAG.getMachineFunction().getFunction();
1878 CallingConv::ID CallerCC = CallerF->getCallingConv();
1879 bool CCMatch = CallerCC == CalleeCC;
1880
1881 // Look for obvious safe cases to perform tail call optimization that do not
1882 // require ABI changes. This is what gcc calls sibcall.
1883
Jim Grosbache3864cc2010-06-16 23:45:49 +00001884 // Do not sibcall optimize vararg calls unless the call site is not passing
1885 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00001886 if (isVarArg && !Outs.empty())
1887 return false;
1888
Tim Northoverd8407452013-10-01 14:33:28 +00001889 // Exception-handling functions need a special set of instructions to indicate
1890 // a return to the hardware. Tail-calling another function would probably
1891 // break this.
1892 if (CallerF->hasFnAttribute("interrupt"))
1893 return false;
1894
Dale Johannesend679ff72010-06-03 21:09:53 +00001895 // Also avoid sibcall optimization if either caller or callee uses struct
1896 // return semantics.
1897 if (isCalleeStructRet || isCallerStructRet)
1898 return false;
1899
Dale Johannesend24c66b2010-06-23 18:52:34 +00001900 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach3840c902011-07-08 20:18:11 +00001901 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1902 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1903 // support in the assembler and linker to be used. This would need to be
1904 // fixed to fully support tail calls in Thumb1.
1905 //
Dale Johannesene2289282010-07-08 01:18:23 +00001906 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1907 // LR. This means if we need to reload LR, it takes an extra instructions,
1908 // which outweighs the value of the tail call; but here we don't know yet
1909 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach535d3b42010-09-08 03:54:02 +00001910 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesene2289282010-07-08 01:18:23 +00001911 // emitEpilogue if LR is used.
Dale Johannesene2289282010-07-08 01:18:23 +00001912
1913 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1914 // but we need to make sure there are enough registers; the only valid
1915 // registers are the 4 used for parameters. We don't currently do this
1916 // case.
Evan Chengd4b08732010-11-30 23:55:39 +00001917 if (Subtarget->isThumb1Only())
1918 return false;
Dale Johannesen3ac52b32010-06-18 18:13:11 +00001919
Dale Johannesend679ff72010-06-03 21:09:53 +00001920 // If the calling conventions do not match, then we'd better make sure the
1921 // results are returned in the same way as what the caller expects.
1922 if (!CCMatch) {
1923 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwarich89019782011-06-10 20:59:24 +00001924 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1925 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00001926 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1927
1928 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwarich89019782011-06-10 20:59:24 +00001929 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1930 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00001931 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1932
1933 if (RVLocs1.size() != RVLocs2.size())
1934 return false;
1935 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1936 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1937 return false;
1938 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1939 return false;
1940 if (RVLocs1[i].isRegLoc()) {
1941 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1942 return false;
1943 } else {
1944 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1945 return false;
1946 }
1947 }
1948 }
1949
Manman Ren7e48b252012-10-12 23:39:43 +00001950 // If Caller's vararg or byval argument has been split between registers and
1951 // stack, do not perform tail call, since part of the argument is in caller's
1952 // local frame.
1953 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
1954 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00001955 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00001956 return false;
1957
Dale Johannesend679ff72010-06-03 21:09:53 +00001958 // If the callee takes no arguments then go on to check the results of the
1959 // call.
1960 if (!Outs.empty()) {
1961 // Check if stack adjustment is needed. For now, do not do this if any
1962 // argument is passed on the stack.
1963 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00001964 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1965 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00001966 CCInfo.AnalyzeCallOperands(Outs,
1967 CCAssignFnForNode(CalleeCC, false, isVarArg));
1968 if (CCInfo.getNextStackOffset()) {
1969 MachineFunction &MF = DAG.getMachineFunction();
1970
1971 // Check if the arguments are already laid out in the right way as
1972 // the caller's fixed stack objects.
1973 MachineFrameInfo *MFI = MF.getFrameInfo();
1974 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topper07720d82012-03-25 23:49:58 +00001975 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001976 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1977 i != e;
1978 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00001979 CCValAssign &VA = ArgLocs[i];
1980 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001981 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001982 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00001983 if (VA.getLocInfo() == CCValAssign::Indirect)
1984 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001985 if (VA.needsCustom()) {
1986 // f64 and vector types are split into multiple registers or
1987 // register/stack-slot combinations. The types will not match
1988 // the registers; give up on memory f64 refs until we figure
1989 // out what to do about this.
1990 if (!VA.isRegLoc())
1991 return false;
1992 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00001993 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001994 if (RegVT == MVT::v2f64) {
1995 if (!ArgLocs[++i].isRegLoc())
1996 return false;
1997 if (!ArgLocs[++i].isRegLoc())
1998 return false;
1999 }
2000 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002001 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2002 MFI, MRI, TII))
2003 return false;
2004 }
2005 }
2006 }
2007 }
2008
2009 return true;
2010}
2011
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002012bool
2013ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2014 MachineFunction &MF, bool isVarArg,
2015 const SmallVectorImpl<ISD::OutputArg> &Outs,
2016 LLVMContext &Context) const {
2017 SmallVector<CCValAssign, 16> RVLocs;
2018 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), RVLocs, Context);
2019 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2020 isVarArg));
2021}
2022
Tim Northoverd8407452013-10-01 14:33:28 +00002023static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2024 SDLoc DL, SelectionDAG &DAG) {
2025 const MachineFunction &MF = DAG.getMachineFunction();
2026 const Function *F = MF.getFunction();
2027
2028 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2029
2030 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2031 // version of the "preferred return address". These offsets affect the return
2032 // instruction if this is a return from PL1 without hypervisor extensions.
2033 // IRQ/FIQ: +4 "subs pc, lr, #4"
2034 // SWI: 0 "subs pc, lr, #0"
2035 // ABORT: +4 "subs pc, lr, #4"
2036 // UNDEF: +4/+2 "subs pc, lr, #0"
2037 // UNDEF varies depending on where the exception came from ARM or Thumb
2038 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2039
2040 int64_t LROffset;
2041 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2042 IntKind == "ABORT")
2043 LROffset = 4;
2044 else if (IntKind == "SWI" || IntKind == "UNDEF")
2045 LROffset = 0;
2046 else
2047 report_fatal_error("Unsupported interrupt attribute. If present, value "
2048 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2049
2050 RetOps.insert(RetOps.begin() + 1, DAG.getConstant(LROffset, MVT::i32, false));
2051
Craig Topper48d114b2014-04-26 18:35:24 +00002052 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002053}
2054
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002055SDValue
2056ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002057 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002058 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002059 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002060 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002061
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002062 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002063 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002064
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002065 // CCState - Info about the registers and stack slots.
Cameron Zwarich89019782011-06-10 20:59:24 +00002066 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2067 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002068
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002069 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002070 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2071 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002072
Bob Wilsona4c22902009-04-17 19:07:39 +00002073 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002074 SmallVector<SDValue, 4> RetOps;
2075 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002076 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002077
2078 // Copy the result values into the output registers.
2079 for (unsigned i = 0, realRVLocIdx = 0;
2080 i != RVLocs.size();
2081 ++i, ++realRVLocIdx) {
2082 CCValAssign &VA = RVLocs[i];
2083 assert(VA.isRegLoc() && "Can only return in registers!");
2084
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002085 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002086
2087 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002088 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002089 case CCValAssign::Full: break;
2090 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002091 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002092 break;
2093 }
2094
Bob Wilsona4c22902009-04-17 19:07:39 +00002095 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002096 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002097 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002098 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2099 DAG.getConstant(0, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002100 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002101 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002102
Christian Pirkerb5728192014-05-08 14:06:24 +00002103 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2104 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2105 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002106 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002107 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002108 VA = RVLocs[++i]; // skip ahead to next loc
2109 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002110 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2111 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002112 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002113 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002114 VA = RVLocs[++i]; // skip ahead to next loc
2115
2116 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002117 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2118 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002119 }
2120 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2121 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002122 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002123 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002124 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2125 fmrrd.getValue(isLittleEndian ? 0 : 1),
2126 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002127 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002128 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002129 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002130 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2131 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002132 Flag);
2133 } else
2134 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2135
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002136 // Guarantee that all emitted copies are
2137 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002138 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002139 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002140 }
2141
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002142 // Update chain and glue.
2143 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002144 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002145 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002146
Tim Northoverd8407452013-10-01 14:33:28 +00002147 // CPUs which aren't M-class use a special sequence to return from
2148 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2149 // though we use "subs pc, lr, #N").
2150 //
2151 // M-class CPUs actually use a normal return sequence with a special
2152 // (hardware-provided) value in LR, so the normal code path works.
2153 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2154 !Subtarget->isMClass()) {
2155 if (Subtarget->isThumb1Only())
2156 report_fatal_error("interrupt attribute is not supported in Thumb1");
2157 return LowerInterruptReturn(RetOps, dl, DAG);
2158 }
2159
Craig Topper48d114b2014-04-26 18:35:24 +00002160 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002161}
2162
Evan Chengf8bad082012-04-10 01:51:00 +00002163bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002164 if (N->getNumValues() != 1)
2165 return false;
2166 if (!N->hasNUsesOfValue(1, 0))
2167 return false;
2168
Evan Chengf8bad082012-04-10 01:51:00 +00002169 SDValue TCChain = Chain;
2170 SDNode *Copy = *N->use_begin();
2171 if (Copy->getOpcode() == ISD::CopyToReg) {
2172 // If the copy has a glue operand, we conservatively assume it isn't safe to
2173 // perform a tail call.
2174 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2175 return false;
2176 TCChain = Copy->getOperand(0);
2177 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2178 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002179 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002180 SmallPtrSet<SDNode*, 2> Copies;
2181 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002182 UI != UE; ++UI) {
2183 if (UI->getOpcode() != ISD::CopyToReg)
2184 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002185 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002186 }
Evan Chengf8bad082012-04-10 01:51:00 +00002187 if (Copies.size() > 2)
2188 return false;
2189
2190 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2191 UI != UE; ++UI) {
2192 SDValue UseChain = UI->getOperand(0);
2193 if (Copies.count(UseChain.getNode()))
2194 // Second CopyToReg
2195 Copy = *UI;
2196 else
2197 // First CopyToReg
2198 TCChain = UseChain;
2199 }
2200 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002201 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002202 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002203 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002204 Copy = *Copy->use_begin();
2205 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002206 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002207 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002208 } else {
2209 return false;
2210 }
2211
Evan Cheng419ea282010-12-01 22:59:46 +00002212 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002213 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2214 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002215 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2216 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002217 return false;
2218 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002219 }
2220
Evan Chengf8bad082012-04-10 01:51:00 +00002221 if (!HasRet)
2222 return false;
2223
2224 Chain = TCChain;
2225 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002226}
2227
Evan Cheng0663f232011-03-21 01:19:09 +00002228bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002229 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002230 return false;
2231
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00002232 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng0663f232011-03-21 01:19:09 +00002233 return false;
2234
2235 return !Subtarget->isThumb1Only();
2236}
2237
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002238// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2239// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2240// one of the above mentioned nodes. It has to be wrapped because otherwise
2241// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2242// be used to form addressing mode. These wrapped nodes will be selected
2243// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002244static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002245 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002246 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002247 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002248 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002249 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002250 if (CP->isMachineConstantPoolEntry())
2251 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2252 CP->getAlignment());
2253 else
2254 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2255 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002256 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002257}
2258
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002259unsigned ARMTargetLowering::getJumpTableEncoding() const {
2260 return MachineJumpTableInfo::EK_Inline;
2261}
2262
Dan Gohman21cea8a2010-04-17 15:26:15 +00002263SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2264 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002265 MachineFunction &MF = DAG.getMachineFunction();
2266 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2267 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002268 SDLoc DL(Op);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002269 EVT PtrVT = getPointerTy();
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002270 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002271 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2272 SDValue CPAddr;
2273 if (RelocM == Reloc::Static) {
2274 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2275 } else {
2276 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002277 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002278 ARMConstantPoolValue *CPV =
2279 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2280 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002281 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2282 }
2283 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2284 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002285 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002286 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002287 if (RelocM == Reloc::Static)
2288 return Result;
Evan Cheng408aa562009-11-06 22:24:13 +00002289 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002290 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002291}
2292
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002293// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002294SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002295ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002296 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002297 SDLoc dl(GA);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002298 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002299 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002300 MachineFunction &MF = DAG.getMachineFunction();
2301 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002302 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002303 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002304 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2305 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002306 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002307 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002308 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattner7727d052010-09-21 06:44:06 +00002309 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002310 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002311 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002312
Evan Cheng408aa562009-11-06 22:24:13 +00002313 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002314 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002315
2316 // call __tls_get_addr.
2317 ArgListTy Args;
2318 ArgListEntry Entry;
2319 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002320 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002321 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002322
Dale Johannesen555a3752009-01-30 23:10:59 +00002323 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002324 TargetLowering::CallLoweringInfo CLI(DAG);
2325 CLI.setDebugLoc(dl).setChain(Chain)
2326 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
2327 DAG.getExternalSymbol("__tls_get_addr", PtrVT), &Args, 0);
2328
Justin Holewinskiaa583972012-05-25 16:35:28 +00002329 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002330 return CallResult.first;
2331}
2332
2333// Lower ISD::GlobalTLSAddress using the "initial exec" or
2334// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002335SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002336ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002337 SelectionDAG &DAG,
2338 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002339 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002340 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002341 SDValue Offset;
2342 SDValue Chain = DAG.getEntryNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002343 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002344 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002345 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002346
Hans Wennborgaea41202012-05-04 09:40:39 +00002347 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002348 MachineFunction &MF = DAG.getMachineFunction();
2349 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002350 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002351 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002352 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2353 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002354 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2355 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2356 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002357 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002358 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002359 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002360 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002361 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002362 Chain = Offset.getValue(1);
2363
Evan Cheng408aa562009-11-06 22:24:13 +00002364 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002365 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002366
Evan Chengcdbb70c2009-10-31 03:39:36 +00002367 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002368 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002369 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002370 } else {
2371 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002372 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002373 ARMConstantPoolValue *CPV =
2374 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002375 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002376 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002377 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002378 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002379 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002380 }
2381
2382 // The address of the thread local variable is the add of the thread
2383 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002384 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002385}
2386
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002387SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002388ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002389 // TODO: implement the "local dynamic" model
2390 assert(Subtarget->isTargetELF() &&
2391 "TLS not implemented for non-ELF targets");
2392 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgaea41202012-05-04 09:40:39 +00002393
2394 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2395
2396 switch (model) {
2397 case TLSModel::GeneralDynamic:
2398 case TLSModel::LocalDynamic:
2399 return LowerToTLSGeneralDynamicModel(GA, DAG);
2400 case TLSModel::InitialExec:
2401 case TLSModel::LocalExec:
2402 return LowerToTLSExecModels(GA, DAG, model);
2403 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002404 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002405}
2406
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002407SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002408 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002409 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002410 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002411 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002412 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Rafael Espindola6de96a12009-01-15 20:18:42 +00002413 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002414 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002415 ARMConstantPoolConstant::Create(GV,
2416 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002417 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002418 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson7117a912009-03-20 22:42:55 +00002419 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002420 CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002421 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002422 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002423 SDValue Chain = Result.getValue(1);
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002424 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00002425 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002426 if (!UseGOTOFF)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002427 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002428 MachinePointerInfo::getGOT(),
2429 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002430 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002431 }
2432
2433 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002434 // pair. This is always cheaper.
2435 if (Subtarget->useMovt()) {
Evan Cheng68aec142011-01-19 02:16:49 +00002436 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002437 // FIXME: Once remat is capable of dealing with instructions with register
2438 // operands, expand this into two nodes.
2439 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2440 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002441 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002442 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2443 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2444 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2445 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002446 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002447 }
2448}
2449
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002450SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002451 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002452 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002453 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002454 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002455 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002456
Tim Northover72360d22013-12-02 10:35:41 +00002457 if (Subtarget->useMovt())
Evan Cheng68aec142011-01-19 02:16:49 +00002458 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002459
Tim Northover72360d22013-12-02 10:35:41 +00002460 // FIXME: Once remat is capable of dealing with instructions with register
2461 // operands, expand this into multiple nodes
2462 unsigned Wrapper =
2463 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002464
Tim Northover72360d22013-12-02 10:35:41 +00002465 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2466 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002467
Evan Cheng1b389522009-09-03 07:04:02 +00002468 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002469 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2470 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002471 return Result;
2472}
2473
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002474SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2475 SelectionDAG &DAG) const {
2476 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
2477 assert(Subtarget->useMovt() && "Windows on ARM expects to use movw/movt");
2478
2479 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2480 EVT PtrVT = getPointerTy();
2481 SDLoc DL(Op);
2482
2483 ++NumMovwMovt;
2484
2485 // FIXME: Once remat is capable of dealing with instructions with register
2486 // operands, expand this into two nodes.
2487 return DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2488 DAG.getTargetGlobalAddress(GV, DL, PtrVT));
2489}
2490
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002491SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002492 SelectionDAG &DAG) const {
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002493 assert(Subtarget->isTargetELF() &&
2494 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Cheng408aa562009-11-06 22:24:13 +00002495 MachineFunction &MF = DAG.getMachineFunction();
2496 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002497 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002498 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002499 SDLoc dl(Op);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002500 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingc214cb02011-10-01 08:58:29 +00002501 ARMConstantPoolValue *CPV =
2502 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2503 ARMPCLabelIndex, PCAdj);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002504 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002505 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002506 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002507 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002508 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00002509 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002510 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002511}
2512
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002513SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002514ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002515 SDLoc dl(Op);
Jim Grosbachfaa3abb2010-05-27 23:49:24 +00002516 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002517 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2518 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002519 Op.getOperand(1), Val);
2520}
2521
2522SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002523ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002524 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002525 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2526 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2527}
2528
2529SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002530ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002531 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002532 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002533 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002534 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002535 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson17f88782009-08-04 00:25:01 +00002536 case Intrinsic::arm_thread_pointer: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002537 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson17f88782009-08-04 00:25:01 +00002538 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2539 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002540 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002541 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002542 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002543 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002544 EVT PtrVT = getPointerTy();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002545 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2546 SDValue CPAddr;
2547 unsigned PCAdj = (RelocM != Reloc::PIC_)
2548 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002549 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002550 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2551 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002552 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002553 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002554 SDValue Result =
Evan Chengcdbb70c2009-10-31 03:39:36 +00002555 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002556 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002557 false, false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002558
2559 if (RelocM == Reloc::PIC_) {
Evan Cheng408aa562009-11-06 22:24:13 +00002560 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002561 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2562 }
2563 return Result;
2564 }
Evan Cheng18381b42011-03-29 23:06:19 +00002565 case Intrinsic::arm_neon_vmulls:
2566 case Intrinsic::arm_neon_vmullu: {
2567 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2568 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002569 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002570 Op.getOperand(1), Op.getOperand(2));
2571 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002572 }
2573}
2574
Eli Friedman30a49e92011-08-03 21:06:02 +00002575static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2576 const ARMSubtarget *Subtarget) {
2577 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002578 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00002579 if (!Subtarget->hasDataBarrier()) {
2580 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2581 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2582 // here.
2583 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00002584 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00002585 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00002586 DAG.getConstant(0, MVT::i32));
2587 }
2588
Tim Northover36b24172013-07-03 09:20:36 +00002589 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
2590 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
2591 unsigned Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00002592 if (Subtarget->isMClass()) {
2593 // Only a full system barrier exists in the M-class architectures.
2594 Domain = ARM_MB::SY;
2595 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00002596 // Swift happens to implement ISHST barriers in a way that's compatible with
2597 // Release semantics but weaker than ISH so we'd be fools not to use
2598 // it. Beware: other processors probably don't!
2599 Domain = ARM_MB::ISHST;
2600 }
2601
Joey Gouly926d3f52013-09-05 15:35:24 +00002602 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
2603 DAG.getConstant(Intrinsic::arm_dmb, MVT::i32),
Tim Northover36b24172013-07-03 09:20:36 +00002604 DAG.getConstant(Domain, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002605}
2606
Evan Cheng8740ee32010-11-03 06:34:55 +00002607static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2608 const ARMSubtarget *Subtarget) {
2609 // ARM pre v5TE and Thumb1 does not have preload instructions.
2610 if (!(Subtarget->isThumb2() ||
2611 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2612 // Just preserve the chain.
2613 return Op.getOperand(0);
2614
Andrew Trickef9de2a2013-05-25 02:42:55 +00002615 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00002616 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2617 if (!isRead &&
2618 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2619 // ARMv7 with MP extension has PLDW.
2620 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00002621
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002622 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2623 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00002624 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00002625 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002626 isData = ~isData & 1;
2627 }
Evan Cheng8740ee32010-11-03 06:34:55 +00002628
2629 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng21acf9f2010-11-04 05:19:35 +00002630 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2631 DAG.getConstant(isData, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00002632}
2633
Dan Gohman31ae5862010-04-17 14:41:14 +00002634static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2635 MachineFunction &MF = DAG.getMachineFunction();
2636 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2637
Evan Cheng10043e22007-01-19 07:51:42 +00002638 // vastart just stores the address of the VarArgsFrameIndex slot into the
2639 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002640 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002641 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00002642 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002643 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00002644 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2645 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002646}
2647
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002648SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00002649ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2650 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002651 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00002652 MachineFunction &MF = DAG.getMachineFunction();
2653 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2654
Craig Topper760b1342012-02-22 05:59:10 +00002655 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00002656 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002657 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002658 else
Craig Topperc7242e02012-04-20 07:30:17 +00002659 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002660
2661 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002662 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002663 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002664
2665 SDValue ArgValue2;
2666 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002667 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00002668 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00002669
2670 // Create load node to retrieve arguments from the stack.
2671 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Chengcdbb70c2009-10-31 03:39:36 +00002672 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002673 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002674 false, false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00002675 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00002676 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002677 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002678 }
Christian Pirkerb5728192014-05-08 14:06:24 +00002679 if (!Subtarget->isLittle())
2680 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002681 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00002682}
2683
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002684void
2685ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002686 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002687 unsigned ArgSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002688 unsigned &ArgRegsSize,
2689 unsigned &ArgRegsSaveSize)
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002690 const {
2691 unsigned NumGPRs;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002692 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2693 unsigned RBegin, REnd;
2694 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2695 NumGPRs = REnd - RBegin;
2696 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002697 unsigned int firstUnalloced;
2698 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2699 sizeof(GPRArgRegs) /
2700 sizeof(GPRArgRegs[0]));
2701 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2702 }
2703
2704 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002705 ArgRegsSize = NumGPRs * 4;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002706
2707 // If parameter is split between stack and GPRs...
Mark Seabornbe266aa2014-02-16 18:59:48 +00002708 if (NumGPRs && Align > 4 &&
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002709 (ArgRegsSize < ArgSize ||
2710 InRegsParamRecordIdx >= CCInfo.getInRegsParamsCount())) {
Mark Seabornbe266aa2014-02-16 18:59:48 +00002711 // Add padding for part of param recovered from GPRs. For example,
2712 // if Align == 8, its last byte must be at address K*8 - 1.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002713 // We need to do it, since remained (stack) part of parameter has
2714 // stack alignment, and we need to "attach" "GPRs head" without gaps
2715 // to it:
2716 // Stack:
2717 // |---- 8 bytes block ----| |---- 8 bytes block ----| |---- 8 bytes...
2718 // [ [padding] [GPRs head] ] [ Tail passed via stack ....
2719 //
2720 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2721 unsigned Padding =
Mark Seabornbe266aa2014-02-16 18:59:48 +00002722 OffsetToAlignment(ArgRegsSize + AFI->getArgRegsSaveSize(), Align);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002723 ArgRegsSaveSize = ArgRegsSize + Padding;
2724 } else
2725 // We don't need to extend regs save size for byval parameters if they
2726 // are passed via GPRs only.
2727 ArgRegsSaveSize = ArgRegsSize;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002728}
2729
2730// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00002731// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002732// byval). Either way, we allocate stack slots adjacent to the data
2733// provided by our caller, and store the unallocated registers there.
2734// If this is a variadic function, the va_list pointer will begin with
2735// these values; otherwise, this reassembles a (byval) structure that
2736// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002737// Return: The frame index registers were stored into.
2738int
2739ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002740 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002741 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002742 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002743 unsigned OffsetFromOrigArg,
2744 unsigned ArgOffset,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002745 unsigned ArgSize,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002746 bool ForceMutable,
2747 unsigned ByValStoreOffset,
2748 unsigned TotalArgRegsSaveSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002749
2750 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00002751 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002752 // Setup first unallocated register as first byval register;
2753 // eat all remained registers
2754 // (these two actions are performed by HandleByVal method).
2755 // Then, here, we initialize stack frame with
2756 // "store-reg" instructions.
2757 // Case #2. Var-args function, that doesn't contain byval parameters.
2758 // The same: eat all remained unallocated registers,
2759 // initialize stack frame.
2760
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002761 MachineFunction &MF = DAG.getMachineFunction();
2762 MachineFrameInfo *MFI = MF.getFrameInfo();
2763 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002764 unsigned firstRegToSaveIndex, lastRegToSaveIndex;
2765 unsigned RBegin, REnd;
2766 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2767 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2768 firstRegToSaveIndex = RBegin - ARM::R0;
2769 lastRegToSaveIndex = REnd - ARM::R0;
2770 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002771 firstRegToSaveIndex = CCInfo.getFirstUnallocated
Craig Topper58713212013-07-15 04:27:47 +00002772 (GPRArgRegs, array_lengthof(GPRArgRegs));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002773 lastRegToSaveIndex = 4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002774 }
2775
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002776 unsigned ArgRegsSize, ArgRegsSaveSize;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002777 computeRegArea(CCInfo, MF, InRegsParamRecordIdx, ArgSize,
2778 ArgRegsSize, ArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002779
2780 // Store any by-val regs to their spots on the stack so that they may be
2781 // loaded by deferencing the result of formal parameter pointer or va_next.
2782 // Note: once stack area for byval/varargs registers
2783 // was initialized, it can't be initialized again.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002784 if (ArgRegsSaveSize) {
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002785 unsigned Padding = ArgRegsSaveSize - ArgRegsSize;
2786
2787 if (Padding) {
2788 assert(AFI->getStoredByValParamsPadding() == 0 &&
2789 "The only parameter may be padded.");
2790 AFI->setStoredByValParamsPadding(Padding);
2791 }
2792
Oliver Stannardd55e1152014-03-05 15:25:27 +00002793 int FrameIndex = MFI->CreateFixedObject(ArgRegsSaveSize,
2794 Padding +
2795 ByValStoreOffset -
2796 (int64_t)TotalArgRegsSaveSize,
2797 false);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002798 SDValue FIN = DAG.getFrameIndex(FrameIndex, getPointerTy());
Oliver Stannardd55e1152014-03-05 15:25:27 +00002799 if (Padding) {
2800 MFI->CreateFixedObject(Padding,
2801 ArgOffset + ByValStoreOffset -
2802 (int64_t)ArgRegsSaveSize,
2803 false);
2804 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002805
2806 SmallVector<SDValue, 4> MemOps;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002807 for (unsigned i = 0; firstRegToSaveIndex < lastRegToSaveIndex;
2808 ++firstRegToSaveIndex, ++i) {
Craig Topper760b1342012-02-22 05:59:10 +00002809 const TargetRegisterClass *RC;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002810 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002811 RC = &ARM::tGPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002812 else
Craig Topperc7242e02012-04-20 07:30:17 +00002813 RC = &ARM::GPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002814
2815 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2816 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2817 SDValue Store =
2818 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002819 MachinePointerInfo(OrigArg, OffsetFromOrigArg + 4*i),
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002820 false, false, 0);
2821 MemOps.push_back(Store);
2822 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2823 DAG.getConstant(4, getPointerTy()));
2824 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002825
2826 AFI->setArgRegsSaveSize(ArgRegsSaveSize + AFI->getArgRegsSaveSize());
2827
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002828 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002829 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002830 return FrameIndex;
Oliver Stannardd55e1152014-03-05 15:25:27 +00002831 } else {
2832 if (ArgSize == 0) {
2833 // We cannot allocate a zero-byte object for the first variadic argument,
2834 // so just make up a size.
2835 ArgSize = 4;
2836 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002837 // This will point to the next argument passed via stack.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002838 return MFI->CreateFixedObject(
Oliver Stannardd55e1152014-03-05 15:25:27 +00002839 ArgSize, ArgOffset, !ForceMutable);
2840 }
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002841}
2842
2843// Setup stack frame, the va_list pointer will start from.
2844void
2845ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002846 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002847 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002848 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002849 bool ForceMutable) const {
2850 MachineFunction &MF = DAG.getMachineFunction();
2851 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2852
2853 // Try to store any remaining integer argument regs
2854 // to their spots on the stack so that they may be loaded by deferencing
2855 // the result of va_next.
2856 // If there is no regs to be stored, just point address after last
2857 // argument passed via stack.
2858 int FrameIndex =
Craig Topper062a2ba2014-04-25 05:30:21 +00002859 StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
2860 CCInfo.getInRegsParamsCount(), 0, ArgOffset, 0, ForceMutable,
2861 0, TotalArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002862
2863 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002864}
2865
Bob Wilson2e076c42009-06-22 23:27:02 +00002866SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002867ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002868 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002869 const SmallVectorImpl<ISD::InputArg>
2870 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002871 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002872 SmallVectorImpl<SDValue> &InVals)
2873 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00002874 MachineFunction &MF = DAG.getMachineFunction();
2875 MachineFrameInfo *MFI = MF.getFrameInfo();
2876
Bob Wilsona4c22902009-04-17 19:07:39 +00002877 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2878
2879 // Assign locations to all of the incoming arguments.
2880 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00002881 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2882 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002883 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002884 CCAssignFnForNode(CallConv, /* Return*/ false,
2885 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00002886
Bob Wilsona4c22902009-04-17 19:07:39 +00002887 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002888 int lastInsIndex = -1;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002889 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002890 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
2891 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002892
2893 // Initially ArgRegsSaveSize is zero.
2894 // Then we increase this value each time we meet byval parameter.
2895 // We also increase this value in case of varargs function.
2896 AFI->setArgRegsSaveSize(0);
2897
Oliver Stannardd55e1152014-03-05 15:25:27 +00002898 unsigned ByValStoreOffset = 0;
2899 unsigned TotalArgRegsSaveSize = 0;
2900 unsigned ArgRegsSaveSizeMaxAlign = 4;
2901
2902 // Calculate the amount of stack space that we need to allocate to store
2903 // byval and variadic arguments that are passed in registers.
2904 // We need to know this before we allocate the first byval or variadic
2905 // argument, as they will be allocated a stack slot below the CFA (Canonical
2906 // Frame Address, the stack pointer at entry to the function).
2907 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2908 CCValAssign &VA = ArgLocs[i];
2909 if (VA.isMemLoc()) {
2910 int index = VA.getValNo();
2911 if (index != lastInsIndex) {
2912 ISD::ArgFlagsTy Flags = Ins[index].Flags;
2913 if (Flags.isByVal()) {
2914 unsigned ExtraArgRegsSize;
2915 unsigned ExtraArgRegsSaveSize;
2916 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsProceed(),
2917 Flags.getByValSize(),
2918 ExtraArgRegsSize, ExtraArgRegsSaveSize);
2919
2920 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
2921 if (Flags.getByValAlign() > ArgRegsSaveSizeMaxAlign)
2922 ArgRegsSaveSizeMaxAlign = Flags.getByValAlign();
2923 CCInfo.nextInRegsParam();
2924 }
2925 lastInsIndex = index;
2926 }
2927 }
2928 }
2929 CCInfo.rewindByValRegsInfo();
2930 lastInsIndex = -1;
2931 if (isVarArg) {
2932 unsigned ExtraArgRegsSize;
2933 unsigned ExtraArgRegsSaveSize;
2934 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsCount(), 0,
2935 ExtraArgRegsSize, ExtraArgRegsSaveSize);
2936 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
2937 }
2938 // If the arg regs save area contains N-byte aligned values, the
2939 // bottom of it must be at least N-byte aligned.
2940 TotalArgRegsSaveSize = RoundUpToAlignment(TotalArgRegsSaveSize, ArgRegsSaveSizeMaxAlign);
2941 TotalArgRegsSaveSize = std::min(TotalArgRegsSaveSize, 16U);
2942
Bob Wilsona4c22902009-04-17 19:07:39 +00002943 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2944 CCValAssign &VA = ArgLocs[i];
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002945 std::advance(CurOrigArg, Ins[VA.getValNo()].OrigArgIndex - CurArgIdx);
2946 CurArgIdx = Ins[VA.getValNo()].OrigArgIndex;
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002947 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00002948 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002949 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00002950
Bob Wilsona4c22902009-04-17 19:07:39 +00002951 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002952 // f64 and vector types are split up into multiple registers or
2953 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00002954 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002955 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002956 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00002957 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00002958 SDValue ArgValue2;
2959 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00002960 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson699bdf72010-04-13 22:03:22 +00002961 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2962 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002963 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002964 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00002965 } else {
2966 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2967 Chain, DAG, dl);
2968 }
Owen Anderson9f944592009-08-11 20:47:22 +00002969 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2970 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00002971 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson9f944592009-08-11 20:47:22 +00002972 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00002973 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2974 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002975 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00002976
Bob Wilson2e076c42009-06-22 23:27:02 +00002977 } else {
Craig Topper760b1342012-02-22 05:59:10 +00002978 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002979
Owen Anderson9f944592009-08-11 20:47:22 +00002980 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00002981 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00002982 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00002983 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00002984 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00002985 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00002986 else if (RegVT == MVT::i32)
Craig Topperc7242e02012-04-20 07:30:17 +00002987 RC = AFI->isThumb1OnlyFunction() ?
2988 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2989 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002990 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00002991 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00002992
2993 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002994 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002995 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00002996 }
2997
2998 // If this is an 8 or 16-bit value, it is really passed promoted
2999 // to 32 bits. Insert an assert[sz]ext to capture this, then
3000 // truncate to the right size.
3001 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003002 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003003 case CCValAssign::Full: break;
3004 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003005 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003006 break;
3007 case CCValAssign::SExt:
3008 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3009 DAG.getValueType(VA.getValVT()));
3010 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3011 break;
3012 case CCValAssign::ZExt:
3013 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3014 DAG.getValueType(VA.getValVT()));
3015 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3016 break;
3017 }
3018
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003019 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003020
3021 } else { // VA.isRegLoc()
3022
3023 // sanity check
3024 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003025 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003026
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003027 int index = ArgLocs[i].getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003028
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003029 // Some Ins[] entries become multiple ArgLoc[] entries.
3030 // Process them only once.
3031 if (index != lastInsIndex)
3032 {
3033 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003034 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003035 // This can be changed with more analysis.
3036 // In case of tail call optimization mark all arguments mutable.
3037 // Since they could be overwritten by lowering of arguments in case of
3038 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003039 if (Flags.isByVal()) {
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003040 unsigned CurByValIndex = CCInfo.getInRegsParamsProceed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003041
3042 ByValStoreOffset = RoundUpToAlignment(ByValStoreOffset, Flags.getByValAlign());
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003043 int FrameIndex = StoreByValRegs(
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003044 CCInfo, DAG, dl, Chain, CurOrigArg,
3045 CurByValIndex,
3046 Ins[VA.getValNo()].PartOffset,
3047 VA.getLocMemOffset(),
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003048 Flags.getByValSize(),
Oliver Stannardd55e1152014-03-05 15:25:27 +00003049 true /*force mutable frames*/,
3050 ByValStoreOffset,
3051 TotalArgRegsSaveSize);
3052 ByValStoreOffset += Flags.getByValSize();
3053 ByValStoreOffset = std::min(ByValStoreOffset, 16U);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003054 InVals.push_back(DAG.getFrameIndex(FrameIndex, getPointerTy()));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003055 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003056 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003057 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003058 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003059 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003060
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003061 // Create load nodes to retrieve arguments from the stack.
3062 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3063 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
3064 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003065 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003066 }
3067 lastInsIndex = index;
3068 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003069 }
3070 }
3071
3072 // varargs
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003073 if (isVarArg)
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003074 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003075 CCInfo.getNextStackOffset(),
3076 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003077
Oliver Stannardb14c6252014-04-02 16:10:33 +00003078 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3079
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003080 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003081}
3082
3083/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003084static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003085 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003086 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003087 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003088 // Maybe this has already been legalized into the constant pool?
3089 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003090 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003091 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003092 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003093 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003094 }
3095 }
3096 return false;
3097}
3098
Evan Cheng10043e22007-01-19 07:51:42 +00003099/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3100/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003101SDValue
3102ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003103 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003104 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003105 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003106 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003107 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003108 // Constant does not fit, try adjusting it by one?
3109 switch (CC) {
3110 default: break;
3111 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003112 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003113 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003114 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003115 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003116 }
3117 break;
3118 case ISD::SETULT:
3119 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003120 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003121 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003122 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003123 }
3124 break;
3125 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003126 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003127 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003128 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003129 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003130 }
3131 break;
3132 case ISD::SETULE:
3133 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003134 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003135 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003136 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003137 }
3138 break;
3139 }
3140 }
3141 }
3142
3143 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003144 ARMISD::NodeType CompareType;
3145 switch (CondCode) {
3146 default:
3147 CompareType = ARMISD::CMP;
3148 break;
3149 case ARMCC::EQ:
3150 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003151 // Uses only Z Flag
3152 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003153 break;
3154 }
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003155 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003156 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003157}
3158
3159/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003160SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003161ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003162 SDLoc dl) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003163 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003164 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003165 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003166 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003167 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3168 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003169}
3170
Bob Wilson45acbd02011-03-08 01:17:20 +00003171/// duplicateCmp - Glue values can have only one use, so this function
3172/// duplicates a comparison node.
3173SDValue
3174ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3175 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003176 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003177 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3178 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3179
3180 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3181 Cmp = Cmp.getOperand(0);
3182 Opc = Cmp.getOpcode();
3183 if (Opc == ARMISD::CMPFP)
3184 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3185 else {
3186 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3187 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3188 }
3189 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3190}
3191
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003192std::pair<SDValue, SDValue>
3193ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3194 SDValue &ARMcc) const {
3195 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3196
3197 SDValue Value, OverflowCmp;
3198 SDValue LHS = Op.getOperand(0);
3199 SDValue RHS = Op.getOperand(1);
3200
3201
3202 // FIXME: We are currently always generating CMPs because we don't support
3203 // generating CMN through the backend. This is not as good as the natural
3204 // CMP case because it causes a register dependency and cannot be folded
3205 // later.
3206
3207 switch (Op.getOpcode()) {
3208 default:
3209 llvm_unreachable("Unknown overflow instruction!");
3210 case ISD::SADDO:
3211 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32);
3212 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS);
3213 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS);
3214 break;
3215 case ISD::UADDO:
3216 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32);
3217 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS);
3218 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS);
3219 break;
3220 case ISD::SSUBO:
3221 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32);
3222 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS);
3223 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS);
3224 break;
3225 case ISD::USUBO:
3226 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32);
3227 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS);
3228 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS);
3229 break;
3230 } // switch (...)
3231
3232 return std::make_pair(Value, OverflowCmp);
3233}
3234
3235
3236SDValue
3237ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3238 // Let legalize expand this if it isn't a legal type yet.
3239 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3240 return SDValue();
3241
3242 SDValue Value, OverflowCmp;
3243 SDValue ARMcc;
3244 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3245 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3246 // We use 0 and 1 as false and true values.
3247 SDValue TVal = DAG.getConstant(1, MVT::i32);
3248 SDValue FVal = DAG.getConstant(0, MVT::i32);
3249 EVT VT = Op.getValueType();
3250
3251 SDValue Overflow = DAG.getNode(ARMISD::CMOV, SDLoc(Op), VT, TVal, FVal,
3252 ARMcc, CCR, OverflowCmp);
3253
3254 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
3255 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), VTs, Value, Overflow);
3256}
3257
3258
Bill Wendling6a981312010-08-11 08:43:16 +00003259SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3260 SDValue Cond = Op.getOperand(0);
3261 SDValue SelectTrue = Op.getOperand(1);
3262 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003263 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003264 unsigned Opc = Cond.getOpcode();
3265
3266 if (Cond.getResNo() == 1 &&
3267 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3268 Opc == ISD::USUBO)) {
3269 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3270 return SDValue();
3271
3272 SDValue Value, OverflowCmp;
3273 SDValue ARMcc;
3274 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3275 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3276 EVT VT = Op.getValueType();
3277
3278 return DAG.getNode(ARMISD::CMOV, SDLoc(Op), VT, SelectTrue, SelectFalse,
3279 ARMcc, CCR, OverflowCmp);
3280
3281 }
Bill Wendling6a981312010-08-11 08:43:16 +00003282
3283 // Convert:
3284 //
3285 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3286 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3287 //
3288 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3289 const ConstantSDNode *CMOVTrue =
3290 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3291 const ConstantSDNode *CMOVFalse =
3292 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3293
3294 if (CMOVTrue && CMOVFalse) {
3295 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3296 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3297
3298 SDValue True;
3299 SDValue False;
3300 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3301 True = SelectTrue;
3302 False = SelectFalse;
3303 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3304 True = SelectFalse;
3305 False = SelectTrue;
3306 }
3307
3308 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003309 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003310 SDValue ARMcc = Cond.getOperand(2);
3311 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003312 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003313 assert(True.getValueType() == VT);
3314 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendling6a981312010-08-11 08:43:16 +00003315 }
3316 }
3317 }
3318
Dan Gohmand4a77c42012-02-24 00:09:36 +00003319 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3320 // undefined bits before doing a full-word comparison with zero.
3321 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
3322 DAG.getConstant(1, Cond.getValueType()));
3323
Bill Wendling6a981312010-08-11 08:43:16 +00003324 return DAG.getSelectCC(dl, Cond,
3325 DAG.getConstant(0, Cond.getValueType()),
3326 SelectTrue, SelectFalse, ISD::SETNE);
3327}
3328
Joey Gouly881eab52013-08-22 15:29:11 +00003329static ISD::CondCode getInverseCCForVSEL(ISD::CondCode CC) {
3330 if (CC == ISD::SETNE)
3331 return ISD::SETEQ;
Weiming Zhao63871d22013-12-18 22:25:17 +00003332 return ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003333}
3334
3335static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3336 bool &swpCmpOps, bool &swpVselOps) {
3337 // Start by selecting the GE condition code for opcodes that return true for
3338 // 'equality'
3339 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3340 CC == ISD::SETULE)
3341 CondCode = ARMCC::GE;
3342
3343 // and GT for opcodes that return false for 'equality'.
3344 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3345 CC == ISD::SETULT)
3346 CondCode = ARMCC::GT;
3347
3348 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3349 // to swap the compare operands.
3350 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3351 CC == ISD::SETULT)
3352 swpCmpOps = true;
3353
3354 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3355 // If we have an unordered opcode, we need to swap the operands to the VSEL
3356 // instruction (effectively negating the condition).
3357 //
3358 // This also has the effect of swapping which one of 'less' or 'greater'
3359 // returns true, so we also swap the compare operands. It also switches
3360 // whether we return true for 'equality', so we compensate by picking the
3361 // opposite condition code to our original choice.
3362 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3363 CC == ISD::SETUGT) {
3364 swpCmpOps = !swpCmpOps;
3365 swpVselOps = !swpVselOps;
3366 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3367 }
3368
3369 // 'ordered' is 'anything but unordered', so use the VS condition code and
3370 // swap the VSEL operands.
3371 if (CC == ISD::SETO) {
3372 CondCode = ARMCC::VS;
3373 swpVselOps = true;
3374 }
3375
3376 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3377 // code and swap the VSEL operands.
3378 if (CC == ISD::SETUNE) {
3379 CondCode = ARMCC::EQ;
3380 swpVselOps = true;
3381 }
3382}
3383
Dan Gohman21cea8a2010-04-17 15:26:15 +00003384SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003385 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003386 SDValue LHS = Op.getOperand(0);
3387 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003388 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003389 SDValue TrueVal = Op.getOperand(2);
3390 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003391 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003392
Owen Anderson9f944592009-08-11 20:47:22 +00003393 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003394 // Try to generate VSEL on ARMv8.
3395 // The VSEL instruction can't use all the usual ARM condition
3396 // codes: it only has two bits to select the condition code, so it's
3397 // constrained to use only GE, GT, VS and EQ.
3398 //
3399 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3400 // swap the operands of the previous compare instruction (effectively
3401 // inverting the compare condition, swapping 'less' and 'greater') and
3402 // sometimes need to swap the operands to the VSEL (which inverts the
3403 // condition in the sense of firing whenever the previous condition didn't)
Joey Goulyccd04892013-09-13 13:46:57 +00003404 if (getSubtarget()->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
Joey Gouly881eab52013-08-22 15:29:11 +00003405 TrueVal.getValueType() == MVT::f64)) {
3406 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3407 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3408 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
3409 CC = getInverseCCForVSEL(CC);
3410 std::swap(TrueVal, FalseVal);
3411 }
3412 }
3413
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003414 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003415 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003416 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Joey Gouly881eab52013-08-22 15:29:11 +00003417 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3418 Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003419 }
3420
3421 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003422 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003423
Joey Gouly881eab52013-08-22 15:29:11 +00003424 // Try to generate VSEL on ARMv8.
Joey Goulyccd04892013-09-13 13:46:57 +00003425 if (getSubtarget()->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
Joey Gouly881eab52013-08-22 15:29:11 +00003426 TrueVal.getValueType() == MVT::f64)) {
Joey Goulye3dd6842013-08-23 12:01:13 +00003427 // We can select VMAXNM/VMINNM from a compare followed by a select with the
3428 // same operands, as follows:
3429 // c = fcmp [ogt, olt, ugt, ult] a, b
3430 // select c, a, b
3431 // We only do this in unsafe-fp-math, because signed zeros and NaNs are
3432 // handled differently than the original code sequence.
3433 if (getTargetMachine().Options.UnsafeFPMath && LHS == TrueVal &&
3434 RHS == FalseVal) {
3435 if (CC == ISD::SETOGT || CC == ISD::SETUGT)
3436 return DAG.getNode(ARMISD::VMAXNM, dl, VT, TrueVal, FalseVal);
3437 if (CC == ISD::SETOLT || CC == ISD::SETULT)
3438 return DAG.getNode(ARMISD::VMINNM, dl, VT, TrueVal, FalseVal);
3439 }
3440
Joey Gouly881eab52013-08-22 15:29:11 +00003441 bool swpCmpOps = false;
3442 bool swpVselOps = false;
3443 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3444
3445 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3446 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3447 if (swpCmpOps)
3448 std::swap(LHS, RHS);
3449 if (swpVselOps)
3450 std::swap(TrueVal, FalseVal);
3451 }
3452 }
3453
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003454 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3455 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003456 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00003457 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003458 ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003459 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003460 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003461 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003462 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson7117a912009-03-20 22:42:55 +00003463 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003464 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Cheng10043e22007-01-19 07:51:42 +00003465 }
3466 return Result;
3467}
3468
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003469/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3470/// to morph to an integer compare sequence.
3471static bool canChangeToInt(SDValue Op, bool &SeenZero,
3472 const ARMSubtarget *Subtarget) {
3473 SDNode *N = Op.getNode();
3474 if (!N->hasOneUse())
3475 // Otherwise it requires moving the value from fp to integer registers.
3476 return false;
3477 if (!N->getNumValues())
3478 return false;
3479 EVT VT = Op.getValueType();
3480 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3481 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3482 // vmrs are very slow, e.g. cortex-a8.
3483 return false;
3484
3485 if (isFloatingPointZero(Op)) {
3486 SeenZero = true;
3487 return true;
3488 }
3489 return ISD::isNormalLoad(N);
3490}
3491
3492static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3493 if (isFloatingPointZero(Op))
3494 return DAG.getConstant(0, MVT::i32);
3495
3496 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003497 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003498 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003499 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003500 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003501
3502 llvm_unreachable("Unknown VFP cmp argument!");
3503}
3504
3505static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3506 SDValue &RetVal1, SDValue &RetVal2) {
3507 if (isFloatingPointZero(Op)) {
3508 RetVal1 = DAG.getConstant(0, MVT::i32);
3509 RetVal2 = DAG.getConstant(0, MVT::i32);
3510 return;
3511 }
3512
3513 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3514 SDValue Ptr = Ld->getBasePtr();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003515 RetVal1 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003516 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003517 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003518 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003519 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003520
3521 EVT PtrType = Ptr.getValueType();
3522 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003523 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003524 PtrType, Ptr, DAG.getConstant(4, PtrType));
Andrew Trickef9de2a2013-05-25 02:42:55 +00003525 RetVal2 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003526 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003527 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003528 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003529 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003530 return;
3531 }
3532
3533 llvm_unreachable("Unknown VFP cmp argument!");
3534}
3535
3536/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3537/// f32 and even f64 comparisons to integer ones.
3538SDValue
3539ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3540 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003541 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003542 SDValue LHS = Op.getOperand(2);
3543 SDValue RHS = Op.getOperand(3);
3544 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003545 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003546
Evan Chengd12af5d2012-03-01 23:27:13 +00003547 bool LHSSeenZero = false;
3548 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3549 bool RHSSeenZero = false;
3550 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3551 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003552 // If unsafe fp math optimization is enabled and there are no other uses of
3553 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003554 // to an integer comparison.
3555 if (CC == ISD::SETOEQ)
3556 CC = ISD::SETEQ;
3557 else if (CC == ISD::SETUNE)
3558 CC = ISD::SETNE;
3559
Evan Chengd12af5d2012-03-01 23:27:13 +00003560 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003561 SDValue ARMcc;
3562 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003563 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3564 bitcastf32Toi32(LHS, DAG), Mask);
3565 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3566 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003567 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3568 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3569 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3570 Chain, Dest, ARMcc, CCR, Cmp);
3571 }
3572
3573 SDValue LHS1, LHS2;
3574 SDValue RHS1, RHS2;
3575 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3576 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003577 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3578 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003579 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3580 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003581 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003582 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003583 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003584 }
3585
3586 return SDValue();
3587}
3588
3589SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3590 SDValue Chain = Op.getOperand(0);
3591 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3592 SDValue LHS = Op.getOperand(2);
3593 SDValue RHS = Op.getOperand(3);
3594 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003595 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003596
Owen Anderson9f944592009-08-11 20:47:22 +00003597 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003598 SDValue ARMcc;
3599 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003600 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003601 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003602 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003603 }
3604
Owen Anderson9f944592009-08-11 20:47:22 +00003605 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003606
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003607 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003608 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3609 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3610 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3611 if (Result.getNode())
3612 return Result;
3613 }
3614
Evan Cheng10043e22007-01-19 07:51:42 +00003615 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003616 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003617
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003618 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3619 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003620 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003621 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003622 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003623 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003624 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003625 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3626 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003627 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003628 }
3629 return Res;
3630}
3631
Dan Gohman21cea8a2010-04-17 15:26:15 +00003632SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003633 SDValue Chain = Op.getOperand(0);
3634 SDValue Table = Op.getOperand(1);
3635 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003636 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003637
Owen Anderson53aa7a92009-08-10 22:56:29 +00003638 EVT PTy = getPointerTy();
Evan Cheng10043e22007-01-19 07:51:42 +00003639 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3640 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3f17aee2009-07-14 18:44:34 +00003641 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003642 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson9f944592009-08-11 20:47:22 +00003643 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chengc8bed032009-07-28 20:53:24 +00003644 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3645 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003646 if (Subtarget->isThumb2()) {
3647 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3648 // which does another jump to the destination. This also makes it easier
3649 // to translate it to TBB / TBH later.
3650 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00003651 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Chengc6d70ae2009-07-29 02:18:14 +00003652 Addr, Op.getOperand(2), JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003653 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00003654 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003655 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattner7727d052010-09-21 06:44:06 +00003656 MachinePointerInfo::getJumpTable(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003657 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003658 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00003659 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson9f944592009-08-11 20:47:22 +00003660 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003661 } else {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003662 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003663 MachinePointerInfo::getJumpTable(),
3664 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003665 Chain = Addr.getValue(1);
Owen Anderson9f944592009-08-11 20:47:22 +00003666 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003667 }
Evan Cheng10043e22007-01-19 07:51:42 +00003668}
3669
Eli Friedman2d4055b2011-11-09 23:36:02 +00003670static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00003671 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003672 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003673
James Molloy547d4c02012-02-20 09:24:05 +00003674 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3675 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3676 return Op;
3677 return DAG.UnrollVectorOp(Op.getNode());
3678 }
3679
3680 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3681 "Invalid type for custom lowering!");
3682 if (VT != MVT::v4i16)
3683 return DAG.UnrollVectorOp(Op.getNode());
3684
3685 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3686 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003687}
3688
Bob Wilsone4191e72010-03-19 22:51:32 +00003689static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman2d4055b2011-11-09 23:36:02 +00003690 EVT VT = Op.getValueType();
3691 if (VT.isVector())
3692 return LowerVectorFP_TO_INT(Op, DAG);
3693
Andrew Trickef9de2a2013-05-25 02:42:55 +00003694 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003695 unsigned Opc;
3696
3697 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003698 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003699 case ISD::FP_TO_SINT:
3700 Opc = ARMISD::FTOSI;
3701 break;
3702 case ISD::FP_TO_UINT:
3703 Opc = ARMISD::FTOUI;
3704 break;
3705 }
3706 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peck527da1b2010-11-23 03:31:01 +00003707 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003708}
3709
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003710static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3711 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003712 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003713
Eli Friedman2d4055b2011-11-09 23:36:02 +00003714 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3715 if (VT.getVectorElementType() == MVT::f32)
3716 return Op;
3717 return DAG.UnrollVectorOp(Op.getNode());
3718 }
3719
Duncan Sandsa41634e2011-08-12 14:54:45 +00003720 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3721 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003722 if (VT != MVT::v4f32)
3723 return DAG.UnrollVectorOp(Op.getNode());
3724
3725 unsigned CastOpc;
3726 unsigned Opc;
3727 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003728 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003729 case ISD::SINT_TO_FP:
3730 CastOpc = ISD::SIGN_EXTEND;
3731 Opc = ISD::SINT_TO_FP;
3732 break;
3733 case ISD::UINT_TO_FP:
3734 CastOpc = ISD::ZERO_EXTEND;
3735 Opc = ISD::UINT_TO_FP;
3736 break;
3737 }
3738
3739 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3740 return DAG.getNode(Opc, dl, VT, Op);
3741}
3742
Bob Wilsone4191e72010-03-19 22:51:32 +00003743static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3744 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003745 if (VT.isVector())
3746 return LowerVectorINT_TO_FP(Op, DAG);
3747
Andrew Trickef9de2a2013-05-25 02:42:55 +00003748 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003749 unsigned Opc;
3750
3751 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003752 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003753 case ISD::SINT_TO_FP:
3754 Opc = ARMISD::SITOF;
3755 break;
3756 case ISD::UINT_TO_FP:
3757 Opc = ARMISD::UITOF;
3758 break;
3759 }
3760
Wesley Peck527da1b2010-11-23 03:31:01 +00003761 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilsone4191e72010-03-19 22:51:32 +00003762 return DAG.getNode(Opc, dl, VT, Op);
3763}
3764
Evan Cheng25f93642010-07-08 02:08:50 +00003765SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00003766 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003767 SDValue Tmp0 = Op.getOperand(0);
3768 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003769 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003770 EVT VT = Op.getValueType();
3771 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00003772 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3773 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3774 bool UseNEON = !InGPR && Subtarget->hasNEON();
3775
3776 if (UseNEON) {
3777 // Use VBSL to copy the sign bit.
3778 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3779 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3780 DAG.getTargetConstant(EncodedVal, MVT::i32));
3781 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3782 if (VT == MVT::f64)
3783 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3784 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3785 DAG.getConstant(32, MVT::i32));
3786 else /*if (VT == MVT::f32)*/
3787 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3788 if (SrcVT == MVT::f32) {
3789 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3790 if (VT == MVT::f64)
3791 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3792 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3793 DAG.getConstant(32, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00003794 } else if (VT == MVT::f32)
3795 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3796 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3797 DAG.getConstant(32, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00003798 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3799 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3800
3801 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3802 MVT::i32);
3803 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3804 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3805 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00003806
Evan Chengd6b641e2011-02-23 02:24:55 +00003807 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3808 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3809 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00003810 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00003811 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3812 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3813 DAG.getConstant(0, MVT::i32));
3814 } else {
3815 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3816 }
3817
3818 return Res;
3819 }
Evan Cheng2da1c952011-02-11 02:28:55 +00003820
3821 // Bitcast operand 1 to i32.
3822 if (SrcVT == MVT::f64)
3823 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00003824 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00003825 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3826
Evan Chengd6b641e2011-02-23 02:24:55 +00003827 // Or in the signbit with integer operations.
3828 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3829 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3830 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3831 if (VT == MVT::f32) {
3832 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3833 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3834 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3835 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00003836 }
3837
Evan Chengd6b641e2011-02-23 02:24:55 +00003838 // f64: Or the high part with signbit and then combine two parts.
3839 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00003840 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00003841 SDValue Lo = Tmp0.getValue(0);
3842 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3843 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3844 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00003845}
3846
Evan Cheng168ced92010-05-22 01:47:14 +00003847SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3848 MachineFunction &MF = DAG.getMachineFunction();
3849 MachineFrameInfo *MFI = MF.getFrameInfo();
3850 MFI->setReturnAddressIsTaken(true);
3851
Bill Wendling908bf812014-01-06 00:43:20 +00003852 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00003853 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00003854
Evan Cheng168ced92010-05-22 01:47:14 +00003855 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003856 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00003857 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3858 if (Depth) {
3859 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3860 SDValue Offset = DAG.getConstant(4, MVT::i32);
3861 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3862 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003863 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00003864 }
3865
3866 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00003867 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00003868 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3869}
3870
Dan Gohman21cea8a2010-04-17 15:26:15 +00003871SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00003872 const ARMBaseRegisterInfo &ARI =
3873 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
3874 MachineFunction &MF = DAG.getMachineFunction();
3875 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003876 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00003877
Owen Anderson53aa7a92009-08-10 22:56:29 +00003878 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003879 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003880 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00003881 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003882 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3883 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00003884 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3885 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003886 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003887 return FrameAddr;
3888}
3889
Renato Golinc7aea402014-05-06 16:51:25 +00003890// FIXME? Maybe this could be a TableGen attribute on some registers and
3891// this table could be generated automatically from RegInfo.
Hal Finkelf0e086a2014-05-11 19:29:07 +00003892unsigned ARMTargetLowering::getRegisterByName(const char* RegName,
3893 EVT VT) const {
Renato Golinc7aea402014-05-06 16:51:25 +00003894 unsigned Reg = StringSwitch<unsigned>(RegName)
3895 .Case("sp", ARM::SP)
3896 .Default(0);
3897 if (Reg)
3898 return Reg;
3899 report_fatal_error("Invalid register name global variable");
3900}
3901
Wesley Peck527da1b2010-11-23 03:31:01 +00003902/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00003903/// expand a bit convert where either the source or destination type is i64 to
3904/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3905/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3906/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00003907static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00003908 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003909 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003910 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00003911
Bob Wilson59b70ea2010-04-17 05:30:19 +00003912 // This function is only supposed to be called for i64 types, either as the
3913 // source or destination of the bit convert.
3914 EVT SrcVT = Op.getValueType();
3915 EVT DstVT = N->getValueType(0);
3916 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00003917 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00003918
Bob Wilson59b70ea2010-04-17 05:30:19 +00003919 // Turn i64->f64 into VMOVDRR.
3920 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson9f944592009-08-11 20:47:22 +00003921 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3922 DAG.getConstant(0, MVT::i32));
3923 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3924 DAG.getConstant(1, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00003925 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00003926 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00003927 }
Bob Wilson7117a912009-03-20 22:42:55 +00003928
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00003929 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00003930 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00003931 SDValue Cvt;
Christian Pirker6692e7c2014-05-14 16:59:44 +00003932 if (TLI.isBigEndian() && SrcVT.isVector() &&
3933 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00003934 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3935 DAG.getVTList(MVT::i32, MVT::i32),
3936 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
3937 else
3938 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3939 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00003940 // Merge the pieces into a single i64 value.
3941 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3942 }
Bob Wilson7117a912009-03-20 22:42:55 +00003943
Bob Wilson59b70ea2010-04-17 05:30:19 +00003944 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00003945}
3946
Bob Wilson2e076c42009-06-22 23:27:02 +00003947/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00003948/// Zero vectors are used to represent vector negation and in those cases
3949/// will be implemented with the NEON VNEG instruction. However, VNEG does
3950/// not support i64 elements, so sometimes the zero vectors will need to be
3951/// explicitly constructed. Regardless, use a canonical VMOV to create the
3952/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003953static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003954 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00003955 // The canonical modified immediate encoding of a zero vector is....0!
3956 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3957 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3958 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00003959 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00003960}
3961
Jim Grosbach624fcb22009-10-31 21:00:56 +00003962/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3963/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00003964SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3965 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00003966 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3967 EVT VT = Op.getValueType();
3968 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003969 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00003970 SDValue ShOpLo = Op.getOperand(0);
3971 SDValue ShOpHi = Op.getOperand(1);
3972 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003973 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00003974 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00003975
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00003976 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3977
Jim Grosbach624fcb22009-10-31 21:00:56 +00003978 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3979 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3980 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3981 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3982 DAG.getConstant(VTBits, MVT::i32));
3983 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3984 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00003985 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00003986
3987 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3988 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003989 ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00003990 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003991 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00003992 CCR, Cmp);
3993
3994 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00003995 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00003996}
3997
Jim Grosbach5d994042009-10-31 19:38:01 +00003998/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3999/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004000SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4001 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004002 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4003 EVT VT = Op.getValueType();
4004 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004005 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004006 SDValue ShOpLo = Op.getOperand(0);
4007 SDValue ShOpHi = Op.getOperand(1);
4008 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004009 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004010
4011 assert(Op.getOpcode() == ISD::SHL_PARTS);
4012 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
4013 DAG.getConstant(VTBits, MVT::i32), ShAmt);
4014 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4015 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
4016 DAG.getConstant(VTBits, MVT::i32));
4017 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4018 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4019
4020 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4021 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4022 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004023 ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004024 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004025 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004026 CCR, Cmp);
4027
4028 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004029 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004030}
4031
Jim Grosbach535d3b42010-09-08 03:54:02 +00004032SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004033 SelectionDAG &DAG) const {
4034 // The rounding mode is in bits 23:22 of the FPSCR.
4035 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4036 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4037 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004038 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004039 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
4040 DAG.getConstant(Intrinsic::arm_get_fpscr,
4041 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004042 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemanb69b1822010-08-03 21:31:55 +00004043 DAG.getConstant(1U << 22, MVT::i32));
4044 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
4045 DAG.getConstant(22, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004046 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemanb69b1822010-08-03 21:31:55 +00004047 DAG.getConstant(3, MVT::i32));
4048}
4049
Jim Grosbach8546ec92010-01-18 19:58:49 +00004050static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4051 const ARMSubtarget *ST) {
4052 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004053 SDLoc dl(N);
Jim Grosbach8546ec92010-01-18 19:58:49 +00004054
4055 if (!ST->hasV6T2Ops())
4056 return SDValue();
4057
4058 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
4059 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4060}
4061
Evan Chengb4eae132012-12-04 22:41:50 +00004062/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4063/// for each 16-bit element from operand, repeated. The basic idea is to
4064/// leverage vcnt to get the 8-bit counts, gather and add the results.
4065///
4066/// Trace for v4i16:
4067/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4068/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4069/// 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 +00004070/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004071/// [b0 b1 b2 b3 b4 b5 b6 b7]
4072/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4073/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4074/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4075static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4076 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004077 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004078
4079 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4080 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4081 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4082 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4083 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4084 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4085}
4086
4087/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4088/// bit-count for each 16-bit element from the operand. We need slightly
4089/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4090/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004091///
Evan Chengb4eae132012-12-04 22:41:50 +00004092/// Trace for v4i16:
4093/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4094/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4095/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4096/// v4i16:Extracted = [k0 k1 k2 k3 ]
4097static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4098 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004099 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004100
4101 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4102 if (VT.is64BitVector()) {
4103 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4104 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
4105 DAG.getIntPtrConstant(0));
4106 } else {
4107 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
4108 BitCounts, DAG.getIntPtrConstant(0));
4109 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4110 }
4111}
4112
4113/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4114/// bit-count for each 32-bit element from the operand. The idea here is
4115/// to split the vector into 16-bit elements, leverage the 16-bit count
4116/// routine, and then combine the results.
4117///
4118/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4119/// input = [v0 v1 ] (vi: 32-bit elements)
4120/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4121/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004122/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004123/// [k0 k1 k2 k3 ]
4124/// N1 =+[k1 k0 k3 k2 ]
4125/// [k0 k2 k1 k3 ]
4126/// N2 =+[k1 k3 k0 k2 ]
4127/// [k0 k2 k1 k3 ]
4128/// Extended =+[k1 k3 k0 k2 ]
4129/// [k0 k2 ]
4130/// Extracted=+[k1 k3 ]
4131///
4132static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4133 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004134 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004135
4136 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4137
4138 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4139 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4140 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4141 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4142 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4143
4144 if (VT.is64BitVector()) {
4145 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4146 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
4147 DAG.getIntPtrConstant(0));
4148 } else {
4149 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
4150 DAG.getIntPtrConstant(0));
4151 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4152 }
4153}
4154
4155static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4156 const ARMSubtarget *ST) {
4157 EVT VT = N->getValueType(0);
4158
4159 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004160 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4161 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004162 "Unexpected type for custom ctpop lowering");
4163
4164 if (VT.getVectorElementType() == MVT::i32)
4165 return lowerCTPOP32BitElements(N, DAG);
4166 else
4167 return lowerCTPOP16BitElements(N, DAG);
4168}
4169
Bob Wilson2e076c42009-06-22 23:27:02 +00004170static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4171 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004172 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004173 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004174
Bob Wilson7d471332010-11-18 21:16:28 +00004175 if (!VT.isVector())
4176 return SDValue();
4177
Bob Wilson2e076c42009-06-22 23:27:02 +00004178 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004179 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004180
Bob Wilson7d471332010-11-18 21:16:28 +00004181 // Left shifts translate directly to the vshiftu intrinsic.
4182 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004183 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilson7d471332010-11-18 21:16:28 +00004184 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
4185 N->getOperand(0), N->getOperand(1));
4186
4187 assert((N->getOpcode() == ISD::SRA ||
4188 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4189
4190 // NEON uses the same intrinsics for both left and right shifts. For
4191 // right shifts, the shift amounts are negative, so negate the vector of
4192 // shift amounts.
4193 EVT ShiftVT = N->getOperand(1).getValueType();
4194 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4195 getZeroVector(ShiftVT, DAG, dl),
4196 N->getOperand(1));
4197 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4198 Intrinsic::arm_neon_vshifts :
4199 Intrinsic::arm_neon_vshiftu);
4200 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4201 DAG.getConstant(vshiftInt, MVT::i32),
4202 N->getOperand(0), NegatedCount);
4203}
4204
4205static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4206 const ARMSubtarget *ST) {
4207 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004208 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004209
Eli Friedman682d8c12009-08-22 03:13:10 +00004210 // We can get here for a node like i32 = ISD::SHL i32, i64
4211 if (VT != MVT::i64)
4212 return SDValue();
4213
4214 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004215 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004216
Chris Lattnerf81d5882007-11-24 07:07:01 +00004217 // We only lower SRA, SRL of 1 here, all others use generic lowering.
4218 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmaneffb8942008-09-12 16:56:44 +00004219 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands6ed40142008-12-01 11:39:25 +00004220 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004221
Chris Lattnerf81d5882007-11-24 07:07:01 +00004222 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004223 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004224
Chris Lattnerf81d5882007-11-24 07:07:01 +00004225 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004226 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004227 DAG.getConstant(0, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004228 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004229 DAG.getConstant(1, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004230
Chris Lattnerf81d5882007-11-24 07:07:01 +00004231 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4232 // captures the result into a carry flag.
4233 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004234 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004235
Chris Lattnerf81d5882007-11-24 07:07:01 +00004236 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004237 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004238
Chris Lattnerf81d5882007-11-24 07:07:01 +00004239 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004240 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004241}
4242
Bob Wilson2e076c42009-06-22 23:27:02 +00004243static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4244 SDValue TmpOp0, TmpOp1;
4245 bool Invert = false;
4246 bool Swap = false;
4247 unsigned Opc = 0;
4248
4249 SDValue Op0 = Op.getOperand(0);
4250 SDValue Op1 = Op.getOperand(1);
4251 SDValue CC = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004252 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004253 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004254 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004255
4256 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
4257 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004258 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004259 case ISD::SETUNE:
4260 case ISD::SETNE: Invert = true; // Fallthrough
4261 case ISD::SETOEQ:
4262 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4263 case ISD::SETOLT:
4264 case ISD::SETLT: Swap = true; // Fallthrough
4265 case ISD::SETOGT:
4266 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4267 case ISD::SETOLE:
4268 case ISD::SETLE: Swap = true; // Fallthrough
4269 case ISD::SETOGE:
4270 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4271 case ISD::SETUGE: Swap = true; // Fallthrough
4272 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4273 case ISD::SETUGT: Swap = true; // Fallthrough
4274 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4275 case ISD::SETUEQ: Invert = true; // Fallthrough
4276 case ISD::SETONE:
4277 // Expand this to (OLT | OGT).
4278 TmpOp0 = Op0;
4279 TmpOp1 = Op1;
4280 Opc = ISD::OR;
4281 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
4282 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
4283 break;
4284 case ISD::SETUO: Invert = true; // Fallthrough
4285 case ISD::SETO:
4286 // Expand this to (OLT | OGE).
4287 TmpOp0 = Op0;
4288 TmpOp1 = Op1;
4289 Opc = ISD::OR;
4290 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
4291 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
4292 break;
4293 }
4294 } else {
4295 // Integer comparisons.
4296 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004297 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004298 case ISD::SETNE: Invert = true;
4299 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4300 case ISD::SETLT: Swap = true;
4301 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4302 case ISD::SETLE: Swap = true;
4303 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4304 case ISD::SETULT: Swap = true;
4305 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4306 case ISD::SETULE: Swap = true;
4307 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4308 }
4309
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004310 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004311 if (Opc == ARMISD::VCEQ) {
4312
4313 SDValue AndOp;
4314 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4315 AndOp = Op0;
4316 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4317 AndOp = Op1;
4318
4319 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004320 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004321 AndOp = AndOp.getOperand(0);
4322
4323 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4324 Opc = ARMISD::VTST;
Wesley Peck527da1b2010-11-23 03:31:01 +00004325 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
4326 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004327 Invert = !Invert;
4328 }
4329 }
4330 }
4331
4332 if (Swap)
4333 std::swap(Op0, Op1);
4334
Owen Andersonc7baee32010-11-08 23:21:22 +00004335 // If one of the operands is a constant vector zero, attempt to fold the
4336 // comparison to a specialized compare-against-zero form.
4337 SDValue SingleOp;
4338 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4339 SingleOp = Op0;
4340 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4341 if (Opc == ARMISD::VCGE)
4342 Opc = ARMISD::VCLEZ;
4343 else if (Opc == ARMISD::VCGT)
4344 Opc = ARMISD::VCLTZ;
4345 SingleOp = Op1;
4346 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004347
Owen Andersonc7baee32010-11-08 23:21:22 +00004348 SDValue Result;
4349 if (SingleOp.getNode()) {
4350 switch (Opc) {
4351 case ARMISD::VCEQ:
4352 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
4353 case ARMISD::VCGE:
4354 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
4355 case ARMISD::VCLEZ:
4356 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
4357 case ARMISD::VCGT:
4358 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
4359 case ARMISD::VCLTZ:
4360 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
4361 default:
4362 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
4363 }
4364 } else {
4365 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
4366 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004367
4368 if (Invert)
4369 Result = DAG.getNOT(dl, Result, VT);
4370
4371 return Result;
4372}
4373
Bob Wilson5b2b5042010-06-14 22:19:57 +00004374/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4375/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004376/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004377static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4378 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Andersona4076922010-11-05 21:57:54 +00004379 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004380 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004381
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004382 // SplatBitSize is set to the smallest size that splats the vector, so a
4383 // zero vector will always have SplatBitSize == 8. However, NEON modified
4384 // immediate instructions others than VMOV do not support the 8-bit encoding
4385 // of a zero vector, and the default encoding of zero is supposed to be the
4386 // 32-bit version.
4387 if (SplatBits == 0)
4388 SplatBitSize = 32;
4389
Bob Wilson2e076c42009-06-22 23:27:02 +00004390 switch (SplatBitSize) {
4391 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004392 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004393 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004394 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004395 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004396 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004397 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004398 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004399 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004400
4401 case 16:
4402 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004403 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004404 if ((SplatBits & ~0xff) == 0) {
4405 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004406 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004407 Imm = SplatBits;
4408 break;
4409 }
4410 if ((SplatBits & ~0xff00) == 0) {
4411 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004412 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004413 Imm = SplatBits >> 8;
4414 break;
4415 }
4416 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004417
4418 case 32:
4419 // NEON's 32-bit VMOV supports splat values where:
4420 // * only one byte is nonzero, or
4421 // * the least significant byte is 0xff and the second byte is nonzero, or
4422 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004423 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004424 if ((SplatBits & ~0xff) == 0) {
4425 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004426 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004427 Imm = SplatBits;
4428 break;
4429 }
4430 if ((SplatBits & ~0xff00) == 0) {
4431 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004432 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004433 Imm = SplatBits >> 8;
4434 break;
4435 }
4436 if ((SplatBits & ~0xff0000) == 0) {
4437 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004438 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004439 Imm = SplatBits >> 16;
4440 break;
4441 }
4442 if ((SplatBits & ~0xff000000) == 0) {
4443 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004444 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004445 Imm = SplatBits >> 24;
4446 break;
4447 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004448
Owen Andersona4076922010-11-05 21:57:54 +00004449 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4450 if (type == OtherModImm) return SDValue();
4451
Bob Wilson2e076c42009-06-22 23:27:02 +00004452 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004453 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4454 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004455 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004456 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004457 break;
4458 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004459
4460 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004461 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4462 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004463 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004464 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004465 break;
4466 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004467
4468 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4469 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4470 // VMOV.I32. A (very) minor optimization would be to replicate the value
4471 // and fall through here to test for a valid 64-bit splat. But, then the
4472 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004473 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004474
4475 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004476 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004477 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004478 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00004479 uint64_t BitMask = 0xff;
4480 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004481 unsigned ImmMask = 1;
4482 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00004483 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00004484 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004485 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00004486 Imm |= ImmMask;
4487 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004488 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004489 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004490 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004491 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00004492 }
Bob Wilson6eae5202010-06-11 21:34:50 +00004493 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004494 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00004495 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00004496 break;
4497 }
4498
Bob Wilson6eae5202010-06-11 21:34:50 +00004499 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00004500 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00004501 }
4502
Bob Wilsona3f19012010-07-13 21:16:48 +00004503 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
4504 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00004505}
4506
Lang Hames591cdaf2012-03-29 21:56:11 +00004507SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4508 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004509 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00004510 return SDValue();
4511
Tim Northoverf79c3a52013-08-20 08:57:11 +00004512 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00004513 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004514
4515 // Try splatting with a VMOV.f32...
4516 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00004517 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
4518
Lang Hames591cdaf2012-03-29 21:56:11 +00004519 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004520 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
4521 // We have code in place to select a valid ConstantFP already, no need to
4522 // do any mangling.
4523 return Op;
4524 }
4525
4526 // It's a float and we are trying to use NEON operations where
4527 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004528 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004529 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
4530 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4531 NewVal);
4532 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
4533 DAG.getConstant(0, MVT::i32));
4534 }
4535
Tim Northoverf79c3a52013-08-20 08:57:11 +00004536 // The rest of our options are NEON only, make sure that's allowed before
4537 // proceeding..
4538 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
4539 return SDValue();
4540
Lang Hames591cdaf2012-03-29 21:56:11 +00004541 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00004542 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
4543
4544 // It wouldn't really be worth bothering for doubles except for one very
4545 // important value, which does happen to match: 0.0. So make sure we don't do
4546 // anything stupid.
4547 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
4548 return SDValue();
4549
4550 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
4551 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4552 false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004553 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004554 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004555 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4556 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004557 if (IsDouble)
4558 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4559
4560 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004561 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4562 VecConstant);
4563 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4564 DAG.getConstant(0, MVT::i32));
4565 }
4566
4567 // Finally, try a VMVN.i32
Tim Northoverf79c3a52013-08-20 08:57:11 +00004568 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4569 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004570 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004571 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004572 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004573
4574 if (IsDouble)
4575 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4576
4577 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004578 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4579 VecConstant);
4580 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4581 DAG.getConstant(0, MVT::i32));
4582 }
4583
4584 return SDValue();
4585}
4586
Quentin Colombet8e1fe842012-11-02 21:32:17 +00004587// check if an VEXT instruction can handle the shuffle mask when the
4588// vector sources of the shuffle are the same.
4589static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4590 unsigned NumElts = VT.getVectorNumElements();
4591
4592 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4593 if (M[0] < 0)
4594 return false;
4595
4596 Imm = M[0];
4597
4598 // If this is a VEXT shuffle, the immediate value is the index of the first
4599 // element. The other shuffle indices must be the successive elements after
4600 // the first one.
4601 unsigned ExpectedElt = Imm;
4602 for (unsigned i = 1; i < NumElts; ++i) {
4603 // Increment the expected index. If it wraps around, just follow it
4604 // back to index zero and keep going.
4605 ++ExpectedElt;
4606 if (ExpectedElt == NumElts)
4607 ExpectedElt = 0;
4608
4609 if (M[i] < 0) continue; // ignore UNDEF indices
4610 if (ExpectedElt != static_cast<unsigned>(M[i]))
4611 return false;
4612 }
4613
4614 return true;
4615}
4616
Lang Hames591cdaf2012-03-29 21:56:11 +00004617
Benjamin Kramer339ced42012-01-15 13:16:05 +00004618static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004619 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004620 unsigned NumElts = VT.getVectorNumElements();
4621 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00004622
4623 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4624 if (M[0] < 0)
4625 return false;
4626
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004627 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00004628
4629 // If this is a VEXT shuffle, the immediate value is the index of the first
4630 // element. The other shuffle indices must be the successive elements after
4631 // the first one.
4632 unsigned ExpectedElt = Imm;
4633 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004634 // Increment the expected index. If it wraps around, it may still be
4635 // a VEXT but the source vectors must be swapped.
4636 ExpectedElt += 1;
4637 if (ExpectedElt == NumElts * 2) {
4638 ExpectedElt = 0;
4639 ReverseVEXT = true;
4640 }
4641
Bob Wilson411dfad2010-08-17 05:54:34 +00004642 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004643 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00004644 return false;
4645 }
4646
4647 // Adjust the index value if the source operands will be swapped.
4648 if (ReverseVEXT)
4649 Imm -= NumElts;
4650
Bob Wilson32cd8552009-08-19 17:03:43 +00004651 return true;
4652}
4653
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004654/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4655/// instruction with the specified blocksize. (The order of the elements
4656/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00004657static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004658 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4659 "Only possible block sizes for VREV are: 16, 32, 64");
4660
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004661 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00004662 if (EltSz == 64)
4663 return false;
4664
4665 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004666 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00004667 // If the first shuffle index is UNDEF, be optimistic.
4668 if (M[0] < 0)
4669 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004670
4671 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4672 return false;
4673
4674 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004675 if (M[i] < 0) continue; // ignore UNDEF indices
4676 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004677 return false;
4678 }
4679
4680 return true;
4681}
4682
Benjamin Kramer339ced42012-01-15 13:16:05 +00004683static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00004684 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
4685 // range, then 0 is placed into the resulting vector. So pretty much any mask
4686 // of 8 elements can work here.
4687 return VT == MVT::v8i8 && M.size() == 8;
4688}
4689
Benjamin Kramer339ced42012-01-15 13:16:05 +00004690static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004691 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4692 if (EltSz == 64)
4693 return false;
4694
Bob Wilsona7062312009-08-21 20:54:19 +00004695 unsigned NumElts = VT.getVectorNumElements();
4696 WhichResult = (M[0] == 0 ? 0 : 1);
4697 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004698 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4699 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsona7062312009-08-21 20:54:19 +00004700 return false;
4701 }
4702 return true;
4703}
4704
Bob Wilson0bbd3072009-12-03 06:40:55 +00004705/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
4706/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4707/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004708static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004709 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4710 if (EltSz == 64)
4711 return false;
4712
4713 unsigned NumElts = VT.getVectorNumElements();
4714 WhichResult = (M[0] == 0 ? 0 : 1);
4715 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004716 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4717 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004718 return false;
4719 }
4720 return true;
4721}
4722
Benjamin Kramer339ced42012-01-15 13:16:05 +00004723static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004724 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4725 if (EltSz == 64)
4726 return false;
4727
Bob Wilsona7062312009-08-21 20:54:19 +00004728 unsigned NumElts = VT.getVectorNumElements();
4729 WhichResult = (M[0] == 0 ? 0 : 1);
4730 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004731 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsona7062312009-08-21 20:54:19 +00004732 if ((unsigned) M[i] != 2 * i + WhichResult)
4733 return false;
4734 }
4735
4736 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004737 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004738 return false;
4739
4740 return true;
4741}
4742
Bob Wilson0bbd3072009-12-03 06:40:55 +00004743/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4744/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4745/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00004746static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004747 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4748 if (EltSz == 64)
4749 return false;
4750
4751 unsigned Half = VT.getVectorNumElements() / 2;
4752 WhichResult = (M[0] == 0 ? 0 : 1);
4753 for (unsigned j = 0; j != 2; ++j) {
4754 unsigned Idx = WhichResult;
4755 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004756 int MIdx = M[i + j * Half];
4757 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson0bbd3072009-12-03 06:40:55 +00004758 return false;
4759 Idx += 2;
4760 }
4761 }
4762
4763 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4764 if (VT.is64BitVector() && EltSz == 32)
4765 return false;
4766
4767 return true;
4768}
4769
Benjamin Kramer339ced42012-01-15 13:16:05 +00004770static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004771 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4772 if (EltSz == 64)
4773 return false;
4774
Bob Wilsona7062312009-08-21 20:54:19 +00004775 unsigned NumElts = VT.getVectorNumElements();
4776 WhichResult = (M[0] == 0 ? 0 : 1);
4777 unsigned Idx = WhichResult * NumElts / 2;
4778 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004779 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4780 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsona7062312009-08-21 20:54:19 +00004781 return false;
4782 Idx += 1;
4783 }
4784
4785 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004786 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004787 return false;
4788
4789 return true;
4790}
4791
Bob Wilson0bbd3072009-12-03 06:40:55 +00004792/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4793/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4794/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004795static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004796 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4797 if (EltSz == 64)
4798 return false;
4799
4800 unsigned NumElts = VT.getVectorNumElements();
4801 WhichResult = (M[0] == 0 ? 0 : 1);
4802 unsigned Idx = WhichResult * NumElts / 2;
4803 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004804 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4805 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004806 return false;
4807 Idx += 1;
4808 }
4809
4810 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4811 if (VT.is64BitVector() && EltSz == 32)
4812 return false;
4813
4814 return true;
4815}
4816
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00004817/// \return true if this is a reverse operation on an vector.
4818static bool isReverseMask(ArrayRef<int> M, EVT VT) {
4819 unsigned NumElts = VT.getVectorNumElements();
4820 // Make sure the mask has the right size.
4821 if (NumElts != M.size())
4822 return false;
4823
4824 // Look for <15, ..., 3, -1, 1, 0>.
4825 for (unsigned i = 0; i != NumElts; ++i)
4826 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
4827 return false;
4828
4829 return true;
4830}
4831
Dale Johannesen2bff5052010-07-29 20:10:08 +00004832// If N is an integer constant that can be moved into a register in one
4833// instruction, return an SDValue of such a constant (will become a MOV
4834// instruction). Otherwise return null.
4835static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004836 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00004837 uint64_t Val;
4838 if (!isa<ConstantSDNode>(N))
4839 return SDValue();
4840 Val = cast<ConstantSDNode>(N)->getZExtValue();
4841
4842 if (ST->isThumb1Only()) {
4843 if (Val <= 255 || ~Val <= 255)
4844 return DAG.getConstant(Val, MVT::i32);
4845 } else {
4846 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4847 return DAG.getConstant(Val, MVT::i32);
4848 }
4849 return SDValue();
4850}
4851
Bob Wilson2e076c42009-06-22 23:27:02 +00004852// If this is a case we can't handle, return null and let the default
4853// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00004854SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4855 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00004856 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004857 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004858 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004859
4860 APInt SplatBits, SplatUndef;
4861 unsigned SplatBitSize;
4862 bool HasAnyUndefs;
4863 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00004864 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00004865 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00004866 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00004867 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00004868 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00004869 DAG, VmovVT, VT.is128BitVector(),
4870 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00004871 if (Val.getNode()) {
4872 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00004873 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00004874 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00004875
4876 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00004877 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004878 Val = isNEONModifiedImm(NegatedImm,
4879 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00004880 DAG, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00004881 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00004882 if (Val.getNode()) {
4883 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00004884 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00004885 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00004886
4887 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00004888 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00004889 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00004890 if (ImmVal != -1) {
4891 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4892 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4893 }
4894 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00004895 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00004896 }
4897
Bob Wilson91fdf682010-05-22 00:23:12 +00004898 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00004899 //
4900 // As an optimisation, even if more than one value is used it may be more
4901 // profitable to splat with one value then change some lanes.
4902 //
4903 // Heuristically we decide to do this if the vector has a "dominant" value,
4904 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00004905 unsigned NumElts = VT.getVectorNumElements();
4906 bool isOnlyLowElement = true;
4907 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00004908 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00004909 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00004910
4911 // Map of the number of times a particular SDValue appears in the
4912 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00004913 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00004914 SDValue Value;
4915 for (unsigned i = 0; i < NumElts; ++i) {
4916 SDValue V = Op.getOperand(i);
4917 if (V.getOpcode() == ISD::UNDEF)
4918 continue;
4919 if (i > 0)
4920 isOnlyLowElement = false;
4921 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4922 isConstant = false;
4923
James Molloy49bdbce2012-09-06 09:55:02 +00004924 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00004925 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00004926
James Molloy49bdbce2012-09-06 09:55:02 +00004927 // Is this value dominant? (takes up more than half of the lanes)
4928 if (++Count > (NumElts / 2)) {
4929 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00004930 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00004931 }
Bob Wilson91fdf682010-05-22 00:23:12 +00004932 }
James Molloy49bdbce2012-09-06 09:55:02 +00004933 if (ValueCounts.size() != 1)
4934 usesOnlyOneValue = false;
4935 if (!Value.getNode() && ValueCounts.size() > 0)
4936 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00004937
James Molloy49bdbce2012-09-06 09:55:02 +00004938 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00004939 return DAG.getUNDEF(VT);
4940
Quentin Colombet0f2fe742013-07-23 22:34:47 +00004941 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
4942 // Keep going if we are hitting this case.
4943 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00004944 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4945
Dale Johannesen2bff5052010-07-29 20:10:08 +00004946 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4947
Dale Johannesen710a2d92010-10-19 20:00:17 +00004948 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4949 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00004950 if (hasDominantValue && EltSize <= 32) {
4951 if (!isConstant) {
4952 SDValue N;
4953
4954 // If we are VDUPing a value that comes directly from a vector, that will
4955 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00004956 // just use VDUPLANE. We can only do this if the lane being extracted
4957 // is at a constant index, as the VDUP from lane instructions only have
4958 // constant-index forms.
4959 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4960 isa<ConstantSDNode>(Value->getOperand(1))) {
Silviu Barangab1409702012-10-15 09:41:32 +00004961 // We need to create a new undef vector to use for the VDUPLANE if the
4962 // size of the vector from which we get the value is different than the
4963 // size of the vector that we need to create. We will insert the element
4964 // such that the register coalescer will remove unnecessary copies.
4965 if (VT != Value->getOperand(0).getValueType()) {
4966 ConstantSDNode *constIndex;
4967 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
4968 assert(constIndex && "The index is not a constant!");
4969 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
4970 VT.getVectorNumElements();
4971 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
4972 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
4973 Value, DAG.getConstant(index, MVT::i32)),
4974 DAG.getConstant(index, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00004975 } else
Silviu Barangab1409702012-10-15 09:41:32 +00004976 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00004977 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00004978 } else
James Molloy49bdbce2012-09-06 09:55:02 +00004979 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4980
4981 if (!usesOnlyOneValue) {
4982 // The dominant value was splatted as 'N', but we now have to insert
4983 // all differing elements.
4984 for (unsigned I = 0; I < NumElts; ++I) {
4985 if (Op.getOperand(I) == Value)
4986 continue;
4987 SmallVector<SDValue, 3> Ops;
4988 Ops.push_back(N);
4989 Ops.push_back(Op.getOperand(I));
4990 Ops.push_back(DAG.getConstant(I, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00004991 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00004992 }
4993 }
4994 return N;
4995 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00004996 if (VT.getVectorElementType().isFloatingPoint()) {
4997 SmallVector<SDValue, 8> Ops;
4998 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00004999 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005000 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005001 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00005002 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005003 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5004 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005005 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005006 }
James Molloy49bdbce2012-09-06 09:55:02 +00005007 if (usesOnlyOneValue) {
5008 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5009 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005010 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005011 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005012 }
5013
5014 // If all elements are constants and the case above didn't get hit, fall back
5015 // to the default expansion, which will generate a load from the constant
5016 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005017 if (isConstant)
5018 return SDValue();
5019
Bob Wilson6f2b8962011-01-07 21:37:30 +00005020 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5021 if (NumElts >= 4) {
5022 SDValue shuffle = ReconstructShuffle(Op, DAG);
5023 if (shuffle != SDValue())
5024 return shuffle;
5025 }
5026
Bob Wilson91fdf682010-05-22 00:23:12 +00005027 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005028 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5029 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005030 if (EltSize >= 32) {
5031 // Do the expansion with floating-point types, since that is what the VFP
5032 // registers are defined to use, and since i64 is not legal.
5033 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5034 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005035 SmallVector<SDValue, 8> Ops;
5036 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005037 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005038 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005039 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005040 }
5041
Jim Grosbach24e102a2013-07-08 18:18:52 +00005042 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5043 // know the default expansion would otherwise fall back on something even
5044 // worse. For a vector with one or two non-undef values, that's
5045 // scalar_to_vector for the elements followed by a shuffle (provided the
5046 // shuffle is valid for the target) and materialization element by element
5047 // on the stack followed by a load for everything else.
5048 if (!isConstant && !usesOnlyOneValue) {
5049 SDValue Vec = DAG.getUNDEF(VT);
5050 for (unsigned i = 0 ; i < NumElts; ++i) {
5051 SDValue V = Op.getOperand(i);
5052 if (V.getOpcode() == ISD::UNDEF)
5053 continue;
5054 SDValue LaneIdx = DAG.getConstant(i, MVT::i32);
5055 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5056 }
5057 return Vec;
5058 }
5059
Bob Wilson2e076c42009-06-22 23:27:02 +00005060 return SDValue();
5061}
5062
Bob Wilson6f2b8962011-01-07 21:37:30 +00005063// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005064// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005065SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5066 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005067 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005068 EVT VT = Op.getValueType();
5069 unsigned NumElts = VT.getVectorNumElements();
5070
5071 SmallVector<SDValue, 2> SourceVecs;
5072 SmallVector<unsigned, 2> MinElts;
5073 SmallVector<unsigned, 2> MaxElts;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005074
Bob Wilson6f2b8962011-01-07 21:37:30 +00005075 for (unsigned i = 0; i < NumElts; ++i) {
5076 SDValue V = Op.getOperand(i);
5077 if (V.getOpcode() == ISD::UNDEF)
5078 continue;
5079 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5080 // A shuffle can only come from building a vector from various
5081 // elements of other vectors.
5082 return SDValue();
Eli Friedman74d1da52011-10-14 23:58:49 +00005083 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
5084 VT.getVectorElementType()) {
5085 // This code doesn't know how to handle shuffles where the vector
5086 // element types do not match (this happens because type legalization
5087 // promotes the return type of EXTRACT_VECTOR_ELT).
5088 // FIXME: It might be appropriate to extend this code to handle
5089 // mismatched types.
5090 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005091 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005092
Bob Wilson6f2b8962011-01-07 21:37:30 +00005093 // Record this extraction against the appropriate vector if possible...
5094 SDValue SourceVec = V.getOperand(0);
Jim Grosbach6df755c2012-07-25 17:02:47 +00005095 // If the element number isn't a constant, we can't effectively
5096 // analyze what's going on.
5097 if (!isa<ConstantSDNode>(V.getOperand(1)))
5098 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005099 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5100 bool FoundSource = false;
5101 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
5102 if (SourceVecs[j] == SourceVec) {
5103 if (MinElts[j] > EltNo)
5104 MinElts[j] = EltNo;
5105 if (MaxElts[j] < EltNo)
5106 MaxElts[j] = EltNo;
5107 FoundSource = true;
5108 break;
5109 }
5110 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005111
Bob Wilson6f2b8962011-01-07 21:37:30 +00005112 // Or record a new source if not...
5113 if (!FoundSource) {
5114 SourceVecs.push_back(SourceVec);
5115 MinElts.push_back(EltNo);
5116 MaxElts.push_back(EltNo);
5117 }
5118 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005119
Bob Wilson6f2b8962011-01-07 21:37:30 +00005120 // Currently only do something sane when at most two source vectors
5121 // involved.
5122 if (SourceVecs.size() > 2)
5123 return SDValue();
5124
5125 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
5126 int VEXTOffsets[2] = {0, 0};
Andrew Trick5eb0a302011-01-19 02:26:13 +00005127
Bob Wilson6f2b8962011-01-07 21:37:30 +00005128 // This loop extracts the usage patterns of the source vectors
5129 // and prepares appropriate SDValues for a shuffle if possible.
5130 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
5131 if (SourceVecs[i].getValueType() == VT) {
5132 // No VEXT necessary
5133 ShuffleSrcs[i] = SourceVecs[i];
5134 VEXTOffsets[i] = 0;
5135 continue;
5136 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
5137 // It probably isn't worth padding out a smaller vector just to
5138 // break it down again in a shuffle.
5139 return SDValue();
5140 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005141
Bob Wilson6f2b8962011-01-07 21:37:30 +00005142 // Since only 64-bit and 128-bit vectors are legal on ARM and
5143 // we've eliminated the other cases...
Bob Wilson3fa9c062011-01-07 23:40:46 +00005144 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
5145 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick5eb0a302011-01-19 02:26:13 +00005146
Bob Wilson6f2b8962011-01-07 21:37:30 +00005147 if (MaxElts[i] - MinElts[i] >= NumElts) {
5148 // Span too large for a VEXT to cope
5149 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005150 }
5151
Bob Wilson6f2b8962011-01-07 21:37:30 +00005152 if (MinElts[i] >= NumElts) {
5153 // The extraction can just take the second half
5154 VEXTOffsets[i] = NumElts;
Eric Christopher2af95512011-01-14 23:50:53 +00005155 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5156 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005157 DAG.getIntPtrConstant(NumElts));
5158 } else if (MaxElts[i] < NumElts) {
5159 // The extraction can just take the first half
5160 VEXTOffsets[i] = 0;
Eric Christopher2af95512011-01-14 23:50:53 +00005161 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5162 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005163 DAG.getIntPtrConstant(0));
5164 } else {
5165 // An actual VEXT is needed
5166 VEXTOffsets[i] = MinElts[i];
Eric Christopher2af95512011-01-14 23:50:53 +00005167 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5168 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005169 DAG.getIntPtrConstant(0));
Eric Christopher2af95512011-01-14 23:50:53 +00005170 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5171 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005172 DAG.getIntPtrConstant(NumElts));
5173 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
5174 DAG.getConstant(VEXTOffsets[i], MVT::i32));
5175 }
5176 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005177
Bob Wilson6f2b8962011-01-07 21:37:30 +00005178 SmallVector<int, 8> Mask;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005179
Bob Wilson6f2b8962011-01-07 21:37:30 +00005180 for (unsigned i = 0; i < NumElts; ++i) {
5181 SDValue Entry = Op.getOperand(i);
5182 if (Entry.getOpcode() == ISD::UNDEF) {
5183 Mask.push_back(-1);
5184 continue;
5185 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005186
Bob Wilson6f2b8962011-01-07 21:37:30 +00005187 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher2af95512011-01-14 23:50:53 +00005188 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
5189 .getOperand(1))->getSExtValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005190 if (ExtractVec == SourceVecs[0]) {
5191 Mask.push_back(ExtractElt - VEXTOffsets[0]);
5192 } else {
5193 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
5194 }
5195 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005196
Bob Wilson6f2b8962011-01-07 21:37:30 +00005197 // Final check before we try to produce nonsense...
5198 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher2af95512011-01-14 23:50:53 +00005199 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
5200 &Mask[0]);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005201
Bob Wilson6f2b8962011-01-07 21:37:30 +00005202 return SDValue();
5203}
5204
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005205/// isShuffleMaskLegal - Targets can use this to indicate that they only
5206/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5207/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5208/// are assumed to be legal.
5209bool
5210ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5211 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005212 if (VT.getVectorNumElements() == 4 &&
5213 (VT.is128BitVector() || VT.is64BitVector())) {
5214 unsigned PFIndexes[4];
5215 for (unsigned i = 0; i != 4; ++i) {
5216 if (M[i] < 0)
5217 PFIndexes[i] = 8;
5218 else
5219 PFIndexes[i] = M[i];
5220 }
5221
5222 // Compute the index in the perfect shuffle table.
5223 unsigned PFTableIndex =
5224 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5225 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5226 unsigned Cost = (PFEntry >> 30);
5227
5228 if (Cost <= 4)
5229 return true;
5230 }
5231
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005232 bool ReverseVEXT;
Bob Wilsona7062312009-08-21 20:54:19 +00005233 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005234
Bob Wilson846bd792010-06-07 23:53:38 +00005235 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5236 return (EltSize >= 32 ||
5237 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005238 isVREVMask(M, VT, 64) ||
5239 isVREVMask(M, VT, 32) ||
5240 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005241 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005242 isVTBLMask(M, VT) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005243 isVTRNMask(M, VT, WhichResult) ||
5244 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson0bbd3072009-12-03 06:40:55 +00005245 isVZIPMask(M, VT, WhichResult) ||
5246 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
5247 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005248 isVZIP_v_undef_Mask(M, VT, WhichResult) ||
5249 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005250}
5251
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005252/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5253/// the specified operations to build the shuffle.
5254static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5255 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005256 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005257 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5258 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5259 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5260
5261 enum {
5262 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5263 OP_VREV,
5264 OP_VDUP0,
5265 OP_VDUP1,
5266 OP_VDUP2,
5267 OP_VDUP3,
5268 OP_VEXT1,
5269 OP_VEXT2,
5270 OP_VEXT3,
5271 OP_VUZPL, // VUZP, left result
5272 OP_VUZPR, // VUZP, right result
5273 OP_VZIPL, // VZIP, left result
5274 OP_VZIPR, // VZIP, right result
5275 OP_VTRNL, // VTRN, left result
5276 OP_VTRNR // VTRN, right result
5277 };
5278
5279 if (OpNum == OP_COPY) {
5280 if (LHSID == (1*9+2)*9+3) return LHS;
5281 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5282 return RHS;
5283 }
5284
5285 SDValue OpLHS, OpRHS;
5286 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5287 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5288 EVT VT = OpLHS.getValueType();
5289
5290 switch (OpNum) {
5291 default: llvm_unreachable("Unknown shuffle opcode!");
5292 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00005293 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00005294 if (VT.getVectorElementType() == MVT::i32 ||
5295 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00005296 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
5297 // vrev <4 x i16> -> VREV32
5298 if (VT.getVectorElementType() == MVT::i16)
5299 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
5300 // vrev <4 x i8> -> VREV16
5301 assert(VT.getVectorElementType() == MVT::i8);
5302 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005303 case OP_VDUP0:
5304 case OP_VDUP1:
5305 case OP_VDUP2:
5306 case OP_VDUP3:
5307 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005308 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005309 case OP_VEXT1:
5310 case OP_VEXT2:
5311 case OP_VEXT3:
5312 return DAG.getNode(ARMISD::VEXT, dl, VT,
5313 OpLHS, OpRHS,
5314 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
5315 case OP_VUZPL:
5316 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005317 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005318 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
5319 case OP_VZIPL:
5320 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005321 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005322 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
5323 case OP_VTRNL:
5324 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005325 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5326 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005327 }
5328}
5329
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005330static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005331 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005332 SelectionDAG &DAG) {
5333 // Check to see if we can use the VTBL instruction.
5334 SDValue V1 = Op.getOperand(0);
5335 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005336 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005337
5338 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00005339 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005340 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
5341 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
5342
5343 if (V2.getNode()->getOpcode() == ISD::UNDEF)
5344 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Craig Topper48d114b2014-04-26 18:35:24 +00005345 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00005346
Owen Anderson77aa2662011-04-05 21:48:57 +00005347 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Craig Topper48d114b2014-04-26 18:35:24 +00005348 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005349}
5350
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005351static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
5352 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005353 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005354 SDValue OpLHS = Op.getOperand(0);
5355 EVT VT = OpLHS.getValueType();
5356
5357 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
5358 "Expect an v8i16/v16i8 type");
5359 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
5360 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
5361 // extract the first 8 bytes into the top double word and the last 8 bytes
5362 // into the bottom double word. The v8i16 case is similar.
5363 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
5364 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
5365 DAG.getConstant(ExtractNum, MVT::i32));
5366}
5367
Bob Wilson2e076c42009-06-22 23:27:02 +00005368static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005369 SDValue V1 = Op.getOperand(0);
5370 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005371 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00005372 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005373 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00005374
Bob Wilsonc6800b52009-08-13 02:13:04 +00005375 // Convert shuffles that are directly supported on NEON to target-specific
5376 // DAG nodes, instead of keeping them as shuffles and matching them again
5377 // during code selection. This is more efficient and avoids the possibility
5378 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00005379 // FIXME: floating-point vectors should be canonicalized to integer vectors
5380 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005381 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005382
Bob Wilson846bd792010-06-07 23:53:38 +00005383 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5384 if (EltSize <= 32) {
5385 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
5386 int Lane = SVN->getSplatIndex();
5387 // If this is undef splat, generate it via "just" vdup, if possible.
5388 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00005389
Dan Gohman198b7ff2011-11-03 21:49:52 +00005390 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00005391 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5392 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5393 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005394 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
5395 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
5396 // reaches it).
5397 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
5398 !isa<ConstantSDNode>(V1.getOperand(0))) {
5399 bool IsScalarToVector = true;
5400 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
5401 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
5402 IsScalarToVector = false;
5403 break;
5404 }
5405 if (IsScalarToVector)
5406 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5407 }
Bob Wilson846bd792010-06-07 23:53:38 +00005408 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
5409 DAG.getConstant(Lane, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00005410 }
Bob Wilson846bd792010-06-07 23:53:38 +00005411
5412 bool ReverseVEXT;
5413 unsigned Imm;
5414 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
5415 if (ReverseVEXT)
5416 std::swap(V1, V2);
5417 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
5418 DAG.getConstant(Imm, MVT::i32));
5419 }
5420
5421 if (isVREVMask(ShuffleMask, VT, 64))
5422 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
5423 if (isVREVMask(ShuffleMask, VT, 32))
5424 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
5425 if (isVREVMask(ShuffleMask, VT, 16))
5426 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
5427
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005428 if (V2->getOpcode() == ISD::UNDEF &&
5429 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
5430 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
5431 DAG.getConstant(Imm, MVT::i32));
5432 }
5433
Bob Wilson846bd792010-06-07 23:53:38 +00005434 // Check for Neon shuffles that modify both input vectors in place.
5435 // If both results are used, i.e., if there are two shuffles with the same
5436 // source operands and with masks corresponding to both results of one of
5437 // these operations, DAG memoization will ensure that a single node is
5438 // used for both shuffles.
5439 unsigned WhichResult;
5440 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5441 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5442 V1, V2).getValue(WhichResult);
5443 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5444 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5445 V1, V2).getValue(WhichResult);
5446 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5447 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5448 V1, V2).getValue(WhichResult);
5449
5450 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5451 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5452 V1, V1).getValue(WhichResult);
5453 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5454 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5455 V1, V1).getValue(WhichResult);
5456 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5457 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5458 V1, V1).getValue(WhichResult);
Bob Wilsoncce31f62009-08-14 05:08:32 +00005459 }
Bob Wilson32cd8552009-08-19 17:03:43 +00005460
Bob Wilsona7062312009-08-21 20:54:19 +00005461 // If the shuffle is not directly supported and it has 4 elements, use
5462 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00005463 unsigned NumElts = VT.getVectorNumElements();
5464 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005465 unsigned PFIndexes[4];
5466 for (unsigned i = 0; i != 4; ++i) {
5467 if (ShuffleMask[i] < 0)
5468 PFIndexes[i] = 8;
5469 else
5470 PFIndexes[i] = ShuffleMask[i];
5471 }
5472
5473 // Compute the index in the perfect shuffle table.
5474 unsigned PFTableIndex =
5475 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005476 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5477 unsigned Cost = (PFEntry >> 30);
5478
5479 if (Cost <= 4)
5480 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
5481 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00005482
Bob Wilsond8a9a042010-06-04 00:04:02 +00005483 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00005484 if (EltSize >= 32) {
5485 // Do the expansion with floating-point types, since that is what the VFP
5486 // registers are defined to use, and since i64 is not legal.
5487 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5488 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00005489 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
5490 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005491 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00005492 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00005493 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00005494 Ops.push_back(DAG.getUNDEF(EltVT));
5495 else
5496 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
5497 ShuffleMask[i] < (int)NumElts ? V1 : V2,
5498 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
5499 MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00005500 }
Craig Topper48d114b2014-04-26 18:35:24 +00005501 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005502 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00005503 }
5504
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005505 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
5506 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
5507
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005508 if (VT == MVT::v8i8) {
5509 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
5510 if (NewOp.getNode())
5511 return NewOp;
5512 }
5513
Bob Wilson6f34e272009-08-14 05:16:33 +00005514 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005515}
5516
Eli Friedmana5e244c2011-10-24 23:08:52 +00005517static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
5518 // INSERT_VECTOR_ELT is legal only for immediate indexes.
5519 SDValue Lane = Op.getOperand(2);
5520 if (!isa<ConstantSDNode>(Lane))
5521 return SDValue();
5522
5523 return Op;
5524}
5525
Bob Wilson2e076c42009-06-22 23:27:02 +00005526static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00005527 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00005528 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00005529 if (!isa<ConstantSDNode>(Lane))
5530 return SDValue();
5531
5532 SDValue Vec = Op.getOperand(0);
5533 if (Op.getValueType() == MVT::i32 &&
5534 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005535 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00005536 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
5537 }
5538
5539 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00005540}
5541
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005542static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5543 // The only time a CONCAT_VECTORS operation can have legal types is when
5544 // two 64-bit vectors are concatenated to a 128-bit vector.
5545 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
5546 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005547 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005548 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005549 SDValue Op0 = Op.getOperand(0);
5550 SDValue Op1 = Op.getOperand(1);
5551 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005552 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005553 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005554 DAG.getIntPtrConstant(0));
5555 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005556 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005557 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005558 DAG.getIntPtrConstant(1));
Wesley Peck527da1b2010-11-23 03:31:01 +00005559 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005560}
5561
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005562/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
5563/// element has been zero/sign-extended, depending on the isSigned parameter,
5564/// from an integer type half its size.
5565static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
5566 bool isSigned) {
5567 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
5568 EVT VT = N->getValueType(0);
5569 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
5570 SDNode *BVN = N->getOperand(0).getNode();
5571 if (BVN->getValueType(0) != MVT::v4i32 ||
5572 BVN->getOpcode() != ISD::BUILD_VECTOR)
5573 return false;
5574 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5575 unsigned HiElt = 1 - LoElt;
5576 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
5577 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
5578 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
5579 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
5580 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
5581 return false;
5582 if (isSigned) {
5583 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
5584 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
5585 return true;
5586 } else {
5587 if (Hi0->isNullValue() && Hi1->isNullValue())
5588 return true;
5589 }
5590 return false;
5591 }
5592
5593 if (N->getOpcode() != ISD::BUILD_VECTOR)
5594 return false;
5595
5596 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5597 SDNode *Elt = N->getOperand(i).getNode();
5598 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
5599 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5600 unsigned HalfSize = EltSize / 2;
5601 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005602 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005603 return false;
5604 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005605 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005606 return false;
5607 }
5608 continue;
5609 }
5610 return false;
5611 }
5612
5613 return true;
5614}
5615
5616/// isSignExtended - Check if a node is a vector value that is sign-extended
5617/// or a constant BUILD_VECTOR with sign-extended elements.
5618static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
5619 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
5620 return true;
5621 if (isExtendedBUILD_VECTOR(N, DAG, true))
5622 return true;
5623 return false;
5624}
5625
5626/// isZeroExtended - Check if a node is a vector value that is zero-extended
5627/// or a constant BUILD_VECTOR with zero-extended elements.
5628static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
5629 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
5630 return true;
5631 if (isExtendedBUILD_VECTOR(N, DAG, false))
5632 return true;
5633 return false;
5634}
5635
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005636static EVT getExtensionTo64Bits(const EVT &OrigVT) {
5637 if (OrigVT.getSizeInBits() >= 64)
5638 return OrigVT;
5639
5640 assert(OrigVT.isSimple() && "Expecting a simple value type");
5641
5642 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
5643 switch (OrigSimpleTy) {
5644 default: llvm_unreachable("Unexpected Vector Type");
5645 case MVT::v2i8:
5646 case MVT::v2i16:
5647 return MVT::v2i32;
5648 case MVT::v4i8:
5649 return MVT::v4i16;
5650 }
5651}
5652
Sebastian Popa204f722012-11-30 19:08:04 +00005653/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
5654/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
5655/// We insert the required extension here to get the vector to fill a D register.
5656static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
5657 const EVT &OrigTy,
5658 const EVT &ExtTy,
5659 unsigned ExtOpcode) {
5660 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
5661 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
5662 // 64-bits we need to insert a new extension so that it will be 64-bits.
5663 assert(ExtTy.is128BitVector() && "Unexpected extension size");
5664 if (OrigTy.getSizeInBits() >= 64)
5665 return N;
5666
5667 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005668 EVT NewVT = getExtensionTo64Bits(OrigTy);
5669
Andrew Trickef9de2a2013-05-25 02:42:55 +00005670 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00005671}
5672
5673/// SkipLoadExtensionForVMULL - return a load of the original vector size that
5674/// does not do any sign/zero extension. If the original vector is less
5675/// than 64 bits, an appropriate extension will be added after the load to
5676/// reach a total size of 64 bits. We have to add the extension separately
5677/// because ARM does not have a sign/zero extending load for vectors.
5678static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005679 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
5680
5681 // The load already has the right type.
5682 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00005683 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00005684 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
5685 LD->isNonTemporal(), LD->isInvariant(),
5686 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005687
5688 // We need to create a zextload/sextload. We cannot just create a load
5689 // followed by a zext/zext node because LowerMUL is also run during normal
5690 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005691 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005692 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
5693 LD->getMemoryVT(), LD->isVolatile(),
5694 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00005695}
5696
5697/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
5698/// extending load, or BUILD_VECTOR with extended elements, return the
5699/// unextended value. The unextended vector should be 64 bits so that it can
5700/// be used as an operand to a VMULL instruction. If the original vector size
5701/// before extension is less than 64 bits we add a an extension to resize
5702/// the vector to 64 bits.
5703static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00005704 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00005705 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
5706 N->getOperand(0)->getValueType(0),
5707 N->getValueType(0),
5708 N->getOpcode());
5709
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005710 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00005711 return SkipLoadExtensionForVMULL(LD, DAG);
5712
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005713 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
5714 // have been legalized as a BITCAST from v4i32.
5715 if (N->getOpcode() == ISD::BITCAST) {
5716 SDNode *BVN = N->getOperand(0).getNode();
5717 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
5718 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
5719 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00005720 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005721 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
5722 }
5723 // Construct a new BUILD_VECTOR with elements truncated to half the size.
5724 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
5725 EVT VT = N->getValueType(0);
5726 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
5727 unsigned NumElts = VT.getVectorNumElements();
5728 MVT TruncVT = MVT::getIntegerVT(EltSize);
5729 SmallVector<SDValue, 8> Ops;
5730 for (unsigned i = 0; i != NumElts; ++i) {
5731 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
5732 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00005733 // Element types smaller than 32 bits are not legal, so use i32 elements.
5734 // The values are implicitly truncated so sext vs. zext doesn't matter.
5735 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005736 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005737 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Craig Topper48d114b2014-04-26 18:35:24 +00005738 MVT::getVectorVT(TruncVT, NumElts), Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00005739}
5740
Evan Chenge2086e72011-03-29 01:56:09 +00005741static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
5742 unsigned Opcode = N->getOpcode();
5743 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5744 SDNode *N0 = N->getOperand(0).getNode();
5745 SDNode *N1 = N->getOperand(1).getNode();
5746 return N0->hasOneUse() && N1->hasOneUse() &&
5747 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
5748 }
5749 return false;
5750}
5751
5752static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
5753 unsigned Opcode = N->getOpcode();
5754 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5755 SDNode *N0 = N->getOperand(0).getNode();
5756 SDNode *N1 = N->getOperand(1).getNode();
5757 return N0->hasOneUse() && N1->hasOneUse() &&
5758 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
5759 }
5760 return false;
5761}
5762
Bob Wilson38ab35a2010-09-01 23:50:19 +00005763static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
5764 // Multiplications are only custom-lowered for 128-bit vectors so that
5765 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
5766 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00005767 assert(VT.is128BitVector() && VT.isInteger() &&
5768 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00005769 SDNode *N0 = Op.getOperand(0).getNode();
5770 SDNode *N1 = Op.getOperand(1).getNode();
5771 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00005772 bool isMLA = false;
5773 bool isN0SExt = isSignExtended(N0, DAG);
5774 bool isN1SExt = isSignExtended(N1, DAG);
5775 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00005776 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00005777 else {
5778 bool isN0ZExt = isZeroExtended(N0, DAG);
5779 bool isN1ZExt = isZeroExtended(N1, DAG);
5780 if (isN0ZExt && isN1ZExt)
5781 NewOpc = ARMISD::VMULLu;
5782 else if (isN1SExt || isN1ZExt) {
5783 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
5784 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
5785 if (isN1SExt && isAddSubSExt(N0, DAG)) {
5786 NewOpc = ARMISD::VMULLs;
5787 isMLA = true;
5788 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
5789 NewOpc = ARMISD::VMULLu;
5790 isMLA = true;
5791 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
5792 std::swap(N0, N1);
5793 NewOpc = ARMISD::VMULLu;
5794 isMLA = true;
5795 }
5796 }
5797
5798 if (!NewOpc) {
5799 if (VT == MVT::v2i64)
5800 // Fall through to expand this. It is not legal.
5801 return SDValue();
5802 else
5803 // Other vector multiplications are legal.
5804 return Op;
5805 }
5806 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00005807
5808 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005809 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00005810 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00005811 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005812 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00005813 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005814 assert(Op0.getValueType().is64BitVector() &&
5815 Op1.getValueType().is64BitVector() &&
5816 "unexpected types for extended operands to VMULL");
5817 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
5818 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00005819
Evan Chenge2086e72011-03-29 01:56:09 +00005820 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
5821 // isel lowering to take advantage of no-stall back to back vmul + vmla.
5822 // vmull q0, d4, d6
5823 // vmlal q0, d5, d6
5824 // is faster than
5825 // vaddl q0, d4, d5
5826 // vmovl q1, d6
5827 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00005828 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
5829 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005830 EVT Op1VT = Op1.getValueType();
5831 return DAG.getNode(N0->getOpcode(), DL, VT,
5832 DAG.getNode(NewOpc, DL, VT,
5833 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
5834 DAG.getNode(NewOpc, DL, VT,
5835 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00005836}
5837
Owen Anderson77aa2662011-04-05 21:48:57 +00005838static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005839LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00005840 // Convert to float
5841 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
5842 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
5843 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
5844 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
5845 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
5846 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
5847 // Get reciprocal estimate.
5848 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00005849 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005850 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
5851 // Because char has a smaller range than uchar, we can actually get away
5852 // without any newton steps. This requires that we use a weird bias
5853 // of 0xb000, however (again, this has been exhaustively tested).
5854 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
5855 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
5856 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
5857 Y = DAG.getConstant(0xb000, MVT::i32);
5858 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
5859 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
5860 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
5861 // Convert back to short.
5862 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
5863 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
5864 return X;
5865}
5866
Owen Anderson77aa2662011-04-05 21:48:57 +00005867static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005868LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00005869 SDValue N2;
5870 // Convert to float.
5871 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
5872 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
5873 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
5874 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
5875 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
5876 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005877
Nate Begemanfa62d502011-02-11 20:53:29 +00005878 // Use reciprocal estimate and one refinement step.
5879 // float4 recip = vrecpeq_f32(yf);
5880 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00005881 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005882 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005883 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005884 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
5885 N1, N2);
5886 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5887 // Because short has a smaller range than ushort, we can actually get away
5888 // with only a single newton step. This requires that we use a weird bias
5889 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005890 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00005891 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5892 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005893 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00005894 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5895 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5896 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5897 // Convert back to integer and return.
5898 // return vmovn_s32(vcvt_s32_f32(result));
5899 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5900 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5901 return N0;
5902}
5903
5904static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
5905 EVT VT = Op.getValueType();
5906 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5907 "unexpected type for custom-lowering ISD::SDIV");
5908
Andrew Trickef9de2a2013-05-25 02:42:55 +00005909 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00005910 SDValue N0 = Op.getOperand(0);
5911 SDValue N1 = Op.getOperand(1);
5912 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00005913
Nate Begemanfa62d502011-02-11 20:53:29 +00005914 if (VT == MVT::v8i8) {
5915 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
5916 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005917
Nate Begemanfa62d502011-02-11 20:53:29 +00005918 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5919 DAG.getIntPtrConstant(4));
5920 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00005921 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00005922 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5923 DAG.getIntPtrConstant(0));
5924 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5925 DAG.getIntPtrConstant(0));
5926
5927 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5928 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5929
5930 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5931 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00005932
Nate Begemanfa62d502011-02-11 20:53:29 +00005933 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5934 return N0;
5935 }
5936 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5937}
5938
5939static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5940 EVT VT = Op.getValueType();
5941 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5942 "unexpected type for custom-lowering ISD::UDIV");
5943
Andrew Trickef9de2a2013-05-25 02:42:55 +00005944 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00005945 SDValue N0 = Op.getOperand(0);
5946 SDValue N1 = Op.getOperand(1);
5947 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00005948
Nate Begemanfa62d502011-02-11 20:53:29 +00005949 if (VT == MVT::v8i8) {
5950 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5951 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005952
Nate Begemanfa62d502011-02-11 20:53:29 +00005953 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5954 DAG.getIntPtrConstant(4));
5955 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00005956 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00005957 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5958 DAG.getIntPtrConstant(0));
5959 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5960 DAG.getIntPtrConstant(0));
Owen Anderson77aa2662011-04-05 21:48:57 +00005961
Nate Begemanfa62d502011-02-11 20:53:29 +00005962 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5963 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00005964
Nate Begemanfa62d502011-02-11 20:53:29 +00005965 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5966 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00005967
5968 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begemanfa62d502011-02-11 20:53:29 +00005969 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5970 N0);
5971 return N0;
5972 }
Owen Anderson77aa2662011-04-05 21:48:57 +00005973
Nate Begemanfa62d502011-02-11 20:53:29 +00005974 // v4i16 sdiv ... Convert to float.
5975 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5976 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5977 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5978 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5979 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005980 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00005981
5982 // Use reciprocal estimate and two refinement steps.
5983 // float4 recip = vrecpeq_f32(yf);
5984 // recip *= vrecpsq_f32(yf, recip);
5985 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00005986 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005987 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005988 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005989 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005990 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00005991 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00005992 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005993 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005994 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00005995 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5996 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5997 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5998 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005999 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006000 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6001 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
6002 N1 = DAG.getConstant(2, MVT::i32);
6003 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6004 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6005 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6006 // Convert back to integer and return.
6007 // return vmovn_u32(vcvt_s32_f32(result));
6008 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6009 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6010 return N0;
6011}
6012
Evan Chenge8916542011-08-30 01:34:54 +00006013static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6014 EVT VT = Op.getNode()->getValueType(0);
6015 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6016
6017 unsigned Opc;
6018 bool ExtraOp = false;
6019 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006020 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006021 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6022 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6023 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6024 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6025 }
6026
6027 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006028 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006029 Op.getOperand(1));
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), Op.getOperand(2));
6032}
6033
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006034SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6035 assert(Subtarget->isTargetDarwin());
6036
6037 // For iOS, we want to call an alternative entry point: __sincos_stret,
6038 // return values are passed via sret.
6039 SDLoc dl(Op);
6040 SDValue Arg = Op.getOperand(0);
6041 EVT ArgVT = Arg.getValueType();
6042 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
6043
6044 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
6045 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6046
6047 // Pair of floats / doubles used to pass the result.
6048 StructType *RetTy = StructType::get(ArgTy, ArgTy, NULL);
6049
6050 // Create stack object for sret.
6051 const uint64_t ByteSize = TLI.getDataLayout()->getTypeAllocSize(RetTy);
6052 const unsigned StackAlign = TLI.getDataLayout()->getPrefTypeAlignment(RetTy);
6053 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
6054 SDValue SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
6055
6056 ArgListTy Args;
6057 ArgListEntry Entry;
6058
6059 Entry.Node = SRet;
6060 Entry.Ty = RetTy->getPointerTo();
6061 Entry.isSExt = false;
6062 Entry.isZExt = false;
6063 Entry.isSRet = true;
6064 Args.push_back(Entry);
6065
6066 Entry.Node = Arg;
6067 Entry.Ty = ArgTy;
6068 Entry.isSExt = false;
6069 Entry.isZExt = false;
6070 Args.push_back(Entry);
6071
6072 const char *LibcallName = (ArgVT == MVT::f64)
6073 ? "__sincos_stret" : "__sincosf_stret";
6074 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
6075
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006076 TargetLowering::CallLoweringInfo CLI(DAG);
6077 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
6078 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), Callee,
6079 &Args, 0)
6080 .setDiscardResult();
6081
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006082 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6083
6084 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6085 MachinePointerInfo(), false, false, false, 0);
6086
6087 // Address of cos field.
6088 SDValue Add = DAG.getNode(ISD::ADD, dl, getPointerTy(), SRet,
6089 DAG.getIntPtrConstant(ArgVT.getStoreSize()));
6090 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6091 MachinePointerInfo(), false, false, false, 0);
6092
6093 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6094 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6095 LoadSin.getValue(0), LoadCos.getValue(0));
6096}
6097
Eli Friedman10f9ce22011-09-15 22:26:18 +00006098static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00006099 // Monotonic load/store is legal for all targets
6100 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
6101 return Op;
6102
Alp Tokercb402912014-01-24 17:20:08 +00006103 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00006104 // dmb or equivalent available.
6105 return SDValue();
6106}
6107
Tim Northoverbc933082013-05-23 19:11:20 +00006108static void ReplaceREADCYCLECOUNTER(SDNode *N,
6109 SmallVectorImpl<SDValue> &Results,
6110 SelectionDAG &DAG,
6111 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006112 SDLoc DL(N);
Tim Northoverbc933082013-05-23 19:11:20 +00006113 SDValue Cycles32, OutChain;
6114
6115 if (Subtarget->hasPerfMon()) {
6116 // Under Power Management extensions, the cycle-count is:
6117 // mrc p15, #0, <Rt>, c9, c13, #0
6118 SDValue Ops[] = { N->getOperand(0), // Chain
6119 DAG.getConstant(Intrinsic::arm_mrc, MVT::i32),
6120 DAG.getConstant(15, MVT::i32),
6121 DAG.getConstant(0, MVT::i32),
6122 DAG.getConstant(9, MVT::i32),
6123 DAG.getConstant(13, MVT::i32),
6124 DAG.getConstant(0, MVT::i32)
6125 };
6126
6127 Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00006128 DAG.getVTList(MVT::i32, MVT::Other), Ops);
Tim Northoverbc933082013-05-23 19:11:20 +00006129 OutChain = Cycles32.getValue(1);
6130 } else {
6131 // Intrinsic is defined to return 0 on unsupported platforms. Technically
6132 // there are older ARM CPUs that have implementation-specific ways of
6133 // obtaining this information (FIXME!).
6134 Cycles32 = DAG.getConstant(0, MVT::i32);
6135 OutChain = DAG.getEntryNode();
6136 }
6137
6138
6139 SDValue Cycles64 = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64,
6140 Cycles32, DAG.getConstant(0, MVT::i32));
6141 Results.push_back(Cycles64);
6142 Results.push_back(OutChain);
6143}
6144
Dan Gohman21cea8a2010-04-17 15:26:15 +00006145SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006146 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006147 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Cheng10043e22007-01-19 07:51:42 +00006148 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006149 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006150 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00006151 switch (Subtarget->getTargetTriple().getObjectFormat()) {
6152 default: llvm_unreachable("unknown object format");
6153 case Triple::COFF:
6154 return LowerGlobalAddressWindows(Op, DAG);
6155 case Triple::ELF:
6156 return LowerGlobalAddressELF(Op, DAG);
6157 case Triple::MachO:
6158 return LowerGlobalAddressDarwin(Op, DAG);
6159 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006160 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006161 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006162 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6163 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006164 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006165 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006166 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006167 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006168 case ISD::SINT_TO_FP:
6169 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6170 case ISD::FP_TO_SINT:
6171 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006172 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006173 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006174 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006175 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006176 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006177 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006178 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6179 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006180 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006181 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006182 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006183 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng15b80e42009-11-12 07:13:11 +00006184 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00006185 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00006186 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach8546ec92010-01-18 19:58:49 +00006187 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00006188 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00006189 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00006190 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006191 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00006192 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00006193 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006194 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006195 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00006196 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006197 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begemanfa62d502011-02-11 20:53:29 +00006198 case ISD::SDIV: return LowerSDIV(Op, DAG);
6199 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00006200 case ISD::ADDC:
6201 case ISD::ADDE:
6202 case ISD::SUBC:
6203 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00006204 case ISD::SADDO:
6205 case ISD::UADDO:
6206 case ISD::SSUBO:
6207 case ISD::USUBO:
6208 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00006209 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00006210 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006211 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00006212 case ISD::SDIVREM:
6213 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006214 }
Evan Cheng10043e22007-01-19 07:51:42 +00006215}
6216
Duncan Sands6ed40142008-12-01 11:39:25 +00006217/// ReplaceNodeResults - Replace the results of node with an illegal result
6218/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00006219void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
6220 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006221 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00006222 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006223 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006224 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00006225 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peck527da1b2010-11-23 03:31:01 +00006226 case ISD::BITCAST:
6227 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006228 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006229 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00006230 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00006231 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006232 break;
Tim Northoverbc933082013-05-23 19:11:20 +00006233 case ISD::READCYCLECOUNTER:
6234 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
6235 return;
Duncan Sands6ed40142008-12-01 11:39:25 +00006236 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00006237 if (Res.getNode())
6238 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00006239}
Chris Lattnerf81d5882007-11-24 07:07:01 +00006240
Evan Cheng10043e22007-01-19 07:51:42 +00006241//===----------------------------------------------------------------------===//
6242// ARM Scheduler Hooks
6243//===----------------------------------------------------------------------===//
6244
Bill Wendling030b58e2011-10-06 22:18:16 +00006245/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6246/// registers the function context.
6247void ARMTargetLowering::
6248SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6249 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendling374ee192011-10-03 21:25:38 +00006250 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6251 DebugLoc dl = MI->getDebugLoc();
6252 MachineFunction *MF = MBB->getParent();
6253 MachineRegisterInfo *MRI = &MF->getRegInfo();
6254 MachineConstantPool *MCP = MF->getConstantPool();
6255 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6256 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00006257
Bill Wendling374ee192011-10-03 21:25:38 +00006258 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006259 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00006260
Bill Wendling374ee192011-10-03 21:25:38 +00006261 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006262 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00006263 ARMConstantPoolValue *CPV =
6264 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6265 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6266
Craig Topperc7242e02012-04-20 07:30:17 +00006267 const TargetRegisterClass *TRC = isThumb ?
6268 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6269 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00006270
Bill Wendling030b58e2011-10-06 22:18:16 +00006271 // Grab constant pool and fixed stack memory operands.
6272 MachineMemOperand *CPMMO =
6273 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6274 MachineMemOperand::MOLoad, 4, 4);
6275
6276 MachineMemOperand *FIMMOSt =
6277 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6278 MachineMemOperand::MOStore, 4, 4);
6279
6280 // Load the address of the dispatch MBB into the jump buffer.
6281 if (isThumb2) {
6282 // Incoming value: jbuf
6283 // ldr.n r5, LCPI1_1
6284 // orr r5, r5, #1
6285 // add r5, pc
6286 // str r5, [$jbuf, #+4] ; &jbuf[1]
6287 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6288 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6289 .addConstantPoolIndex(CPI)
6290 .addMemOperand(CPMMO));
6291 // Set the low bit because of thumb mode.
6292 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6293 AddDefaultCC(
6294 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6295 .addReg(NewVReg1, RegState::Kill)
6296 .addImm(0x01)));
6297 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6298 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6299 .addReg(NewVReg2, RegState::Kill)
6300 .addImm(PCLabelId);
6301 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6302 .addReg(NewVReg3, RegState::Kill)
6303 .addFrameIndex(FI)
6304 .addImm(36) // &jbuf[1] :: pc
6305 .addMemOperand(FIMMOSt));
6306 } else if (isThumb) {
6307 // Incoming value: jbuf
6308 // ldr.n r1, LCPI1_4
6309 // add r1, pc
6310 // mov r2, #1
6311 // orrs r1, r2
6312 // add r2, $jbuf, #+4 ; &jbuf[1]
6313 // str r1, [r2]
6314 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6315 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6316 .addConstantPoolIndex(CPI)
6317 .addMemOperand(CPMMO));
6318 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6319 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6320 .addReg(NewVReg1, RegState::Kill)
6321 .addImm(PCLabelId);
6322 // Set the low bit because of thumb mode.
6323 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6324 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6325 .addReg(ARM::CPSR, RegState::Define)
6326 .addImm(1));
6327 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6328 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6329 .addReg(ARM::CPSR, RegState::Define)
6330 .addReg(NewVReg2, RegState::Kill)
6331 .addReg(NewVReg3, RegState::Kill));
6332 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6333 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
6334 .addFrameIndex(FI)
6335 .addImm(36)); // &jbuf[1] :: pc
6336 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6337 .addReg(NewVReg4, RegState::Kill)
6338 .addReg(NewVReg5, RegState::Kill)
6339 .addImm(0)
6340 .addMemOperand(FIMMOSt));
6341 } else {
6342 // Incoming value: jbuf
6343 // ldr r1, LCPI1_1
6344 // add r1, pc, r1
6345 // str r1, [$jbuf, #+4] ; &jbuf[1]
6346 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6347 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6348 .addConstantPoolIndex(CPI)
6349 .addImm(0)
6350 .addMemOperand(CPMMO));
6351 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6352 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6353 .addReg(NewVReg1, RegState::Kill)
6354 .addImm(PCLabelId));
6355 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6356 .addReg(NewVReg2, RegState::Kill)
6357 .addFrameIndex(FI)
6358 .addImm(36) // &jbuf[1] :: pc
6359 .addMemOperand(FIMMOSt));
6360 }
6361}
6362
6363MachineBasicBlock *ARMTargetLowering::
6364EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
6365 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6366 DebugLoc dl = MI->getDebugLoc();
6367 MachineFunction *MF = MBB->getParent();
6368 MachineRegisterInfo *MRI = &MF->getRegInfo();
6369 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6370 MachineFrameInfo *MFI = MF->getFrameInfo();
6371 int FI = MFI->getFunctionContextIndex();
6372
Craig Topperc7242e02012-04-20 07:30:17 +00006373 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
6374 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen691ae332012-05-20 06:38:47 +00006375 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00006376
Bill Wendling362c1b02011-10-06 21:29:56 +00006377 // Get a mapping of the call site numbers to all of the landing pads they're
6378 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00006379 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6380 unsigned MaxCSNum = 0;
6381 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00006382 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6383 ++BB) {
Bill Wendling202803e2011-10-05 00:02:33 +00006384 if (!BB->isLandingPad()) continue;
6385
6386 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6387 // pad.
6388 for (MachineBasicBlock::iterator
6389 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6390 if (!II->isEHLabel()) continue;
6391
6392 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006393 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00006394
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006395 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6396 for (SmallVectorImpl<unsigned>::iterator
6397 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6398 CSI != CSE; ++CSI) {
6399 CallSiteNumToLPad[*CSI].push_back(BB);
6400 MaxCSNum = std::max(MaxCSNum, *CSI);
6401 }
Bill Wendling202803e2011-10-05 00:02:33 +00006402 break;
6403 }
6404 }
6405
6406 // Get an ordered list of the machine basic blocks for the jump table.
6407 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling883ec972011-10-07 23:18:02 +00006408 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00006409 LPadList.reserve(CallSiteNumToLPad.size());
6410 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6411 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6412 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006413 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00006414 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00006415 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6416 }
Bill Wendling202803e2011-10-05 00:02:33 +00006417 }
6418
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006419 assert(!LPadList.empty() &&
6420 "No landing pad destinations for the dispatch jump table!");
6421
Bill Wendling362c1b02011-10-06 21:29:56 +00006422 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00006423 MachineJumpTableInfo *JTI =
6424 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6425 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
6426 unsigned UId = AFI->createJumpTableUId();
Chad Rosier96603432013-03-01 18:30:38 +00006427 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00006428
Bill Wendling362c1b02011-10-06 21:29:56 +00006429 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00006430
6431 // Shove the dispatch's address into the return slot in the function context.
6432 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6433 DispatchBB->setIsLandingPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00006434
Bill Wendling324be982011-10-05 00:39:32 +00006435 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006436 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00006437 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006438 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00006439 else
6440 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
6441
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006442 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00006443 DispatchBB->addSuccessor(TrapBB);
6444
6445 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6446 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00006447
Bill Wendling510fbcd2011-10-17 21:32:56 +00006448 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00006449 MF->insert(MF->end(), DispatchBB);
6450 MF->insert(MF->end(), DispContBB);
6451 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00006452
Bill Wendling030b58e2011-10-06 22:18:16 +00006453 // Insert code into the entry block that creates and registers the function
6454 // context.
6455 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6456
Bill Wendling030b58e2011-10-06 22:18:16 +00006457 MachineMemOperand *FIMMOLd =
Bill Wendling362c1b02011-10-06 21:29:56 +00006458 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendlingb3d46782011-10-06 23:37:36 +00006459 MachineMemOperand::MOLoad |
6460 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00006461
Chad Rosier1ec8e402012-11-06 23:05:24 +00006462 MachineInstrBuilder MIB;
6463 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6464
6465 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6466 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6467
6468 // Add a register mask with no preserved registers. This results in all
6469 // registers being marked as clobbered.
6470 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00006471
Bill Wendling85833f72011-10-18 22:49:07 +00006472 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00006473 if (Subtarget->isThumb2()) {
6474 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6475 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6476 .addFrameIndex(FI)
6477 .addImm(4)
6478 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006479
Bill Wendling85833f72011-10-18 22:49:07 +00006480 if (NumLPads < 256) {
6481 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6482 .addReg(NewVReg1)
6483 .addImm(LPadList.size()));
6484 } else {
6485 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6486 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006487 .addImm(NumLPads & 0xFFFF));
6488
6489 unsigned VReg2 = VReg1;
6490 if ((NumLPads & 0xFFFF0000) != 0) {
6491 VReg2 = MRI->createVirtualRegister(TRC);
6492 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6493 .addReg(VReg1)
6494 .addImm(NumLPads >> 16));
6495 }
6496
Bill Wendling85833f72011-10-18 22:49:07 +00006497 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6498 .addReg(NewVReg1)
6499 .addReg(VReg2));
6500 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006501
Bill Wendling5626c662011-10-06 22:53:00 +00006502 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6503 .addMBB(TrapBB)
6504 .addImm(ARMCC::HI)
6505 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00006506
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006507 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6508 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006509 .addJumpTableIndex(MJTI)
6510 .addImm(UId));
Bill Wendling202803e2011-10-05 00:02:33 +00006511
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006512 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006513 AddDefaultCC(
6514 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006515 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6516 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00006517 .addReg(NewVReg1)
6518 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6519
6520 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006521 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00006522 .addReg(NewVReg1)
Bill Wendling5626c662011-10-06 22:53:00 +00006523 .addJumpTableIndex(MJTI)
6524 .addImm(UId);
6525 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00006526 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6527 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6528 .addFrameIndex(FI)
6529 .addImm(1)
6530 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00006531
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006532 if (NumLPads < 256) {
6533 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6534 .addReg(NewVReg1)
6535 .addImm(NumLPads));
6536 } else {
6537 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00006538 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6539 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6540
6541 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006542 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006543 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006544 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006545 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006546
6547 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6548 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6549 .addReg(VReg1, RegState::Define)
6550 .addConstantPoolIndex(Idx));
6551 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6552 .addReg(NewVReg1)
6553 .addReg(VReg1));
6554 }
6555
Bill Wendlingb3d46782011-10-06 23:37:36 +00006556 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6557 .addMBB(TrapBB)
6558 .addImm(ARMCC::HI)
6559 .addReg(ARM::CPSR);
6560
6561 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6562 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6563 .addReg(ARM::CPSR, RegState::Define)
6564 .addReg(NewVReg1)
6565 .addImm(2));
6566
6567 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00006568 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendlingb3d46782011-10-06 23:37:36 +00006569 .addJumpTableIndex(MJTI)
6570 .addImm(UId));
6571
6572 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6573 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6574 .addReg(ARM::CPSR, RegState::Define)
6575 .addReg(NewVReg2, RegState::Kill)
6576 .addReg(NewVReg3));
6577
6578 MachineMemOperand *JTMMOLd =
6579 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6580 MachineMemOperand::MOLoad, 4, 4);
6581
6582 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6583 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6584 .addReg(NewVReg4, RegState::Kill)
6585 .addImm(0)
6586 .addMemOperand(JTMMOLd));
6587
Chad Rosier96603432013-03-01 18:30:38 +00006588 unsigned NewVReg6 = NewVReg5;
6589 if (RelocM == Reloc::PIC_) {
6590 NewVReg6 = MRI->createVirtualRegister(TRC);
6591 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6592 .addReg(ARM::CPSR, RegState::Define)
6593 .addReg(NewVReg5, RegState::Kill)
6594 .addReg(NewVReg3));
6595 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00006596
6597 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6598 .addReg(NewVReg6, RegState::Kill)
6599 .addJumpTableIndex(MJTI)
6600 .addImm(UId);
Bill Wendling5626c662011-10-06 22:53:00 +00006601 } else {
6602 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6603 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6604 .addFrameIndex(FI)
6605 .addImm(4)
6606 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00006607
Bill Wendling4969dcd2011-10-18 22:52:20 +00006608 if (NumLPads < 256) {
6609 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6610 .addReg(NewVReg1)
6611 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00006612 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00006613 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6614 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006615 .addImm(NumLPads & 0xFFFF));
6616
6617 unsigned VReg2 = VReg1;
6618 if ((NumLPads & 0xFFFF0000) != 0) {
6619 VReg2 = MRI->createVirtualRegister(TRC);
6620 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6621 .addReg(VReg1)
6622 .addImm(NumLPads >> 16));
6623 }
6624
Bill Wendling4969dcd2011-10-18 22:52:20 +00006625 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6626 .addReg(NewVReg1)
6627 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00006628 } else {
6629 MachineConstantPool *ConstantPool = MF->getConstantPool();
6630 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6631 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6632
6633 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006634 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006635 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006636 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006637 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6638
6639 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6640 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6641 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00006642 .addConstantPoolIndex(Idx)
6643 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00006644 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6645 .addReg(NewVReg1)
6646 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00006647 }
6648
Bill Wendling5626c662011-10-06 22:53:00 +00006649 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6650 .addMBB(TrapBB)
6651 .addImm(ARMCC::HI)
6652 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00006653
Bill Wendling973c8172011-10-18 22:11:18 +00006654 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006655 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00006656 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006657 .addReg(NewVReg1)
6658 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00006659 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6660 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006661 .addJumpTableIndex(MJTI)
6662 .addImm(UId));
6663
6664 MachineMemOperand *JTMMOLd =
6665 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6666 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00006667 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006668 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00006669 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6670 .addReg(NewVReg3, RegState::Kill)
6671 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006672 .addImm(0)
6673 .addMemOperand(JTMMOLd));
6674
Chad Rosier96603432013-03-01 18:30:38 +00006675 if (RelocM == Reloc::PIC_) {
6676 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
6677 .addReg(NewVReg5, RegState::Kill)
6678 .addReg(NewVReg4)
6679 .addJumpTableIndex(MJTI)
6680 .addImm(UId);
6681 } else {
6682 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
6683 .addReg(NewVReg5, RegState::Kill)
6684 .addJumpTableIndex(MJTI)
6685 .addImm(UId);
6686 }
Bill Wendling5626c662011-10-06 22:53:00 +00006687 }
Bill Wendling202803e2011-10-05 00:02:33 +00006688
Bill Wendling324be982011-10-05 00:39:32 +00006689 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006690 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00006691 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006692 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6693 MachineBasicBlock *CurMBB = *I;
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006694 if (SeenMBBs.insert(CurMBB))
Bill Wendling883ec972011-10-07 23:18:02 +00006695 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006696 }
6697
Bill Wendling26d27802011-10-17 05:25:09 +00006698 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00006699 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00006700 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling883ec972011-10-07 23:18:02 +00006701 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6702 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6703 MachineBasicBlock *BB = *I;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006704
6705 // Remove the landing pad successor from the invoke block and replace it
6706 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00006707 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6708 BB->succ_end());
6709 while (!Successors.empty()) {
6710 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling883ec972011-10-07 23:18:02 +00006711 if (SMBB->isLandingPad()) {
6712 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00006713 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006714 }
6715 }
6716
6717 BB->addSuccessor(DispatchBB);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006718
6719 // Find the invoke call and mark all of the callee-saved registers as
6720 // 'implicit defined' so that they're spilled. This prevents code from
6721 // moving instructions to before the EH block, where they will never be
6722 // executed.
6723 for (MachineBasicBlock::reverse_iterator
6724 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00006725 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006726
6727 DenseMap<unsigned, bool> DefRegs;
6728 for (MachineInstr::mop_iterator
6729 OI = II->operands_begin(), OE = II->operands_end();
6730 OI != OE; ++OI) {
6731 if (!OI->isReg()) continue;
6732 DefRegs[OI->getReg()] = true;
6733 }
6734
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00006735 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006736
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006737 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00006738 unsigned Reg = SavedRegs[i];
6739 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00006740 !ARM::tGPRRegClass.contains(Reg) &&
6741 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006742 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006743 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006744 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006745 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006746 continue;
6747 if (!DefRegs[Reg])
6748 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006749 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006750
6751 break;
6752 }
Bill Wendling883ec972011-10-07 23:18:02 +00006753 }
Bill Wendling324be982011-10-05 00:39:32 +00006754
Bill Wendling617075f2011-10-18 18:30:49 +00006755 // Mark all former landing pads as non-landing pads. The dispatch is the only
6756 // landing pad now.
6757 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6758 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6759 (*I)->setIsLandingPad(false);
6760
Bill Wendling324be982011-10-05 00:39:32 +00006761 // The instruction is gone now.
6762 MI->eraseFromParent();
6763
Bill Wendling374ee192011-10-03 21:25:38 +00006764 return MBB;
6765}
6766
Evan Cheng0cc4ad92010-07-13 19:27:42 +00006767static
6768MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6769 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6770 E = MBB->succ_end(); I != E; ++I)
6771 if (*I != Succ)
6772 return *I;
6773 llvm_unreachable("Expecting a BB with two successors!");
6774}
6775
Manman Renb504f492013-10-29 22:27:32 +00006776/// Return the load opcode for a given load size. If load size >= 8,
6777/// neon opcode will be returned.
6778static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
6779 if (LdSize >= 8)
6780 return LdSize == 16 ? ARM::VLD1q32wb_fixed
6781 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
6782 if (IsThumb1)
6783 return LdSize == 4 ? ARM::tLDRi
6784 : LdSize == 2 ? ARM::tLDRHi
6785 : LdSize == 1 ? ARM::tLDRBi : 0;
6786 if (IsThumb2)
6787 return LdSize == 4 ? ARM::t2LDR_POST
6788 : LdSize == 2 ? ARM::t2LDRH_POST
6789 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
6790 return LdSize == 4 ? ARM::LDR_POST_IMM
6791 : LdSize == 2 ? ARM::LDRH_POST
6792 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
6793}
6794
6795/// Return the store opcode for a given store size. If store size >= 8,
6796/// neon opcode will be returned.
6797static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
6798 if (StSize >= 8)
6799 return StSize == 16 ? ARM::VST1q32wb_fixed
6800 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
6801 if (IsThumb1)
6802 return StSize == 4 ? ARM::tSTRi
6803 : StSize == 2 ? ARM::tSTRHi
6804 : StSize == 1 ? ARM::tSTRBi : 0;
6805 if (IsThumb2)
6806 return StSize == 4 ? ARM::t2STR_POST
6807 : StSize == 2 ? ARM::t2STRH_POST
6808 : StSize == 1 ? ARM::t2STRB_POST : 0;
6809 return StSize == 4 ? ARM::STR_POST_IMM
6810 : StSize == 2 ? ARM::STRH_POST
6811 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
6812}
6813
6814/// Emit a post-increment load operation with given size. The instructions
6815/// will be added to BB at Pos.
6816static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
6817 const TargetInstrInfo *TII, DebugLoc dl,
6818 unsigned LdSize, unsigned Data, unsigned AddrIn,
6819 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
6820 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
6821 assert(LdOpc != 0 && "Should have a load opcode");
6822 if (LdSize >= 8) {
6823 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6824 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6825 .addImm(0));
6826 } else if (IsThumb1) {
6827 // load + update AddrIn
6828 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6829 .addReg(AddrIn).addImm(0));
6830 MachineInstrBuilder MIB =
6831 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
6832 MIB = AddDefaultT1CC(MIB);
6833 MIB.addReg(AddrIn).addImm(LdSize);
6834 AddDefaultPred(MIB);
6835 } else if (IsThumb2) {
6836 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6837 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6838 .addImm(LdSize));
6839 } else { // arm
6840 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6841 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6842 .addReg(0).addImm(LdSize));
6843 }
6844}
6845
6846/// Emit a post-increment store operation with given size. The instructions
6847/// will be added to BB at Pos.
6848static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
6849 const TargetInstrInfo *TII, DebugLoc dl,
6850 unsigned StSize, unsigned Data, unsigned AddrIn,
6851 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
6852 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
6853 assert(StOpc != 0 && "Should have a store opcode");
6854 if (StSize >= 8) {
6855 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6856 .addReg(AddrIn).addImm(0).addReg(Data));
6857 } else if (IsThumb1) {
6858 // store + update AddrIn
6859 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
6860 .addReg(AddrIn).addImm(0));
6861 MachineInstrBuilder MIB =
6862 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
6863 MIB = AddDefaultT1CC(MIB);
6864 MIB.addReg(AddrIn).addImm(StSize);
6865 AddDefaultPred(MIB);
6866 } else if (IsThumb2) {
6867 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6868 .addReg(Data).addReg(AddrIn).addImm(StSize));
6869 } else { // arm
6870 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6871 .addReg(Data).addReg(AddrIn).addReg(0)
6872 .addImm(StSize));
6873 }
6874}
6875
David Peixottoc32e24a2013-10-17 19:49:22 +00006876MachineBasicBlock *
6877ARMTargetLowering::EmitStructByval(MachineInstr *MI,
6878 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00006879 // This pseudo instruction has 3 operands: dst, src, size
6880 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6881 // Otherwise, we will generate unrolled scalar copies.
6882 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6883 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6884 MachineFunction::iterator It = BB;
6885 ++It;
6886
6887 unsigned dest = MI->getOperand(0).getReg();
6888 unsigned src = MI->getOperand(1).getReg();
6889 unsigned SizeVal = MI->getOperand(2).getImm();
6890 unsigned Align = MI->getOperand(3).getImm();
6891 DebugLoc dl = MI->getDebugLoc();
6892
Manman Rene8735522012-06-01 19:33:18 +00006893 MachineFunction *MF = BB->getParent();
6894 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00006895 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00006896 const TargetRegisterClass *TRC = nullptr;
6897 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00006898
6899 bool IsThumb1 = Subtarget->isThumb1Only();
6900 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00006901
6902 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00006903 UnitSize = 1;
6904 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00006905 UnitSize = 2;
6906 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00006907 // Check whether we can use NEON instructions.
Bill Wendling698e84f2012-12-30 10:32:01 +00006908 if (!MF->getFunction()->getAttributes().
6909 hasAttribute(AttributeSet::FunctionIndex,
6910 Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00006911 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00006912 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00006913 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00006914 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00006915 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00006916 }
6917 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00006918 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00006919 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00006920 }
Manman Ren6e1fd462012-06-18 22:23:48 +00006921
David Peixottob0653e532013-10-24 16:39:36 +00006922 // Select the correct opcode and register class for unit size load/store
6923 bool IsNeon = UnitSize >= 8;
6924 TRC = (IsThumb1 || IsThumb2) ? (const TargetRegisterClass *)&ARM::tGPRRegClass
6925 : (const TargetRegisterClass *)&ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00006926 if (IsNeon)
David Peixottob0653e532013-10-24 16:39:36 +00006927 VecTRC = UnitSize == 16
6928 ? (const TargetRegisterClass *)&ARM::DPairRegClass
6929 : UnitSize == 8
6930 ? (const TargetRegisterClass *)&ARM::DPRRegClass
Craig Topper062a2ba2014-04-25 05:30:21 +00006931 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00006932
Manman Rene8735522012-06-01 19:33:18 +00006933 unsigned BytesLeft = SizeVal % UnitSize;
6934 unsigned LoopSize = SizeVal - BytesLeft;
6935
6936 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6937 // Use LDR and STR to copy.
6938 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6939 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6940 unsigned srcIn = src;
6941 unsigned destIn = dest;
6942 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00006943 unsigned srcOut = MRI.createVirtualRegister(TRC);
6944 unsigned destOut = MRI.createVirtualRegister(TRC);
6945 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00006946 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
6947 IsThumb1, IsThumb2);
6948 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
6949 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00006950 srcIn = srcOut;
6951 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00006952 }
6953
6954 // Handle the leftover bytes with LDRB and STRB.
6955 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6956 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00006957 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00006958 unsigned srcOut = MRI.createVirtualRegister(TRC);
6959 unsigned destOut = MRI.createVirtualRegister(TRC);
6960 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00006961 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
6962 IsThumb1, IsThumb2);
6963 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
6964 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00006965 srcIn = srcOut;
6966 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00006967 }
6968 MI->eraseFromParent(); // The instruction is gone now.
6969 return BB;
6970 }
6971
6972 // Expand the pseudo op to a loop.
6973 // thisMBB:
6974 // ...
6975 // movw varEnd, # --> with thumb2
6976 // movt varEnd, #
6977 // ldrcp varEnd, idx --> without thumb2
6978 // fallthrough --> loopMBB
6979 // loopMBB:
6980 // PHI varPhi, varEnd, varLoop
6981 // PHI srcPhi, src, srcLoop
6982 // PHI destPhi, dst, destLoop
6983 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6984 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
6985 // subs varLoop, varPhi, #UnitSize
6986 // bne loopMBB
6987 // fallthrough --> exitMBB
6988 // exitMBB:
6989 // epilogue to handle left-over bytes
6990 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6991 // [destOut] = STRB_POST(scratch, destLoop, 1)
6992 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6993 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6994 MF->insert(It, loopMBB);
6995 MF->insert(It, exitMBB);
6996
6997 // Transfer the remainder of BB and its successor edges to exitMBB.
6998 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006999 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007000 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7001
7002 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007003 unsigned varEnd = MRI.createVirtualRegister(TRC);
7004 if (IsThumb2) {
7005 unsigned Vtmp = varEnd;
7006 if ((LoopSize & 0xFFFF0000) != 0)
7007 Vtmp = MRI.createVirtualRegister(TRC);
7008 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), Vtmp)
7009 .addImm(LoopSize & 0xFFFF));
7010
7011 if ((LoopSize & 0xFFFF0000) != 0)
7012 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
7013 .addReg(Vtmp).addImm(LoopSize >> 16));
7014 } else {
7015 MachineConstantPool *ConstantPool = MF->getConstantPool();
7016 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7017 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7018
7019 // MachineConstantPool wants an explicit alignment.
7020 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
7021 if (Align == 0)
7022 Align = getDataLayout()->getTypeAllocSize(C->getType());
7023 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7024
7025 if (IsThumb1)
7026 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7027 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7028 else
7029 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7030 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7031 }
Manman Rene8735522012-06-01 19:33:18 +00007032 BB->addSuccessor(loopMBB);
7033
7034 // Generate the loop body:
7035 // varPhi = PHI(varLoop, varEnd)
7036 // srcPhi = PHI(srcLoop, src)
7037 // destPhi = PHI(destLoop, dst)
7038 MachineBasicBlock *entryBB = BB;
7039 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007040 unsigned varLoop = MRI.createVirtualRegister(TRC);
7041 unsigned varPhi = MRI.createVirtualRegister(TRC);
7042 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7043 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7044 unsigned destLoop = MRI.createVirtualRegister(TRC);
7045 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007046
7047 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7048 .addReg(varLoop).addMBB(loopMBB)
7049 .addReg(varEnd).addMBB(entryBB);
7050 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7051 .addReg(srcLoop).addMBB(loopMBB)
7052 .addReg(src).addMBB(entryBB);
7053 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7054 .addReg(destLoop).addMBB(loopMBB)
7055 .addReg(dest).addMBB(entryBB);
7056
7057 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7058 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007059 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007060 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7061 IsThumb1, IsThumb2);
7062 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7063 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007064
7065 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007066 if (IsThumb1) {
7067 MachineInstrBuilder MIB =
7068 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7069 MIB = AddDefaultT1CC(MIB);
7070 MIB.addReg(varPhi).addImm(UnitSize);
7071 AddDefaultPred(MIB);
7072 } else {
7073 MachineInstrBuilder MIB =
7074 BuildMI(*BB, BB->end(), dl,
7075 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7076 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7077 MIB->getOperand(5).setReg(ARM::CPSR);
7078 MIB->getOperand(5).setIsDef(true);
7079 }
7080 BuildMI(*BB, BB->end(), dl,
7081 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7082 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007083
7084 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7085 BB->addSuccessor(loopMBB);
7086 BB->addSuccessor(exitMBB);
7087
7088 // Add epilogue to handle BytesLeft.
7089 BB = exitMBB;
7090 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007091
7092 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7093 // [destOut] = STRB_POST(scratch, destLoop, 1)
7094 unsigned srcIn = srcLoop;
7095 unsigned destIn = destLoop;
7096 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007097 unsigned srcOut = MRI.createVirtualRegister(TRC);
7098 unsigned destOut = MRI.createVirtualRegister(TRC);
7099 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007100 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7101 IsThumb1, IsThumb2);
7102 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7103 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007104 srcIn = srcOut;
7105 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007106 }
7107
7108 MI->eraseFromParent(); // The instruction is gone now.
7109 return BB;
7110}
7111
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00007112MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00007113ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00007114 MachineBasicBlock *BB) const {
Evan Cheng10043e22007-01-19 07:51:42 +00007115 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00007116 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00007117 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00007118 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00007119 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00007120 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00007121 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00007122 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00007123 // The Thumb2 pre-indexed stores have the same MI operands, they just
7124 // define them differently in the .td files from the isel patterns, so
7125 // they need pseudos.
7126 case ARM::t2STR_preidx:
7127 MI->setDesc(TII->get(ARM::t2STR_PRE));
7128 return BB;
7129 case ARM::t2STRB_preidx:
7130 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7131 return BB;
7132 case ARM::t2STRH_preidx:
7133 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7134 return BB;
7135
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007136 case ARM::STRi_preidx:
7137 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00007138 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007139 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7140 // Decode the offset.
7141 unsigned Offset = MI->getOperand(4).getImm();
7142 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7143 Offset = ARM_AM::getAM2Offset(Offset);
7144 if (isSub)
7145 Offset = -Offset;
7146
Jim Grosbachf402f692011-08-12 21:02:34 +00007147 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00007148 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007149 .addOperand(MI->getOperand(0)) // Rn_wb
7150 .addOperand(MI->getOperand(1)) // Rt
7151 .addOperand(MI->getOperand(2)) // Rn
7152 .addImm(Offset) // offset (skip GPR==zero_reg)
7153 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00007154 .addOperand(MI->getOperand(6))
7155 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007156 MI->eraseFromParent();
7157 return BB;
7158 }
7159 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00007160 case ARM::STRBr_preidx:
7161 case ARM::STRH_preidx: {
7162 unsigned NewOpc;
7163 switch (MI->getOpcode()) {
7164 default: llvm_unreachable("unexpected opcode!");
7165 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7166 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7167 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7168 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007169 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7170 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7171 MIB.addOperand(MI->getOperand(i));
7172 MI->eraseFromParent();
7173 return BB;
7174 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00007175
Evan Chengbb2af352009-08-12 05:17:19 +00007176 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00007177 // To "insert" a SELECT_CC instruction, we actually have to insert the
7178 // diamond control-flow pattern. The incoming instruction knows the
7179 // destination vreg to set, the condition code register to branch on, the
7180 // true/false values to select between, and a branch opcode to use.
7181 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00007182 MachineFunction::iterator It = BB;
Evan Cheng10043e22007-01-19 07:51:42 +00007183 ++It;
7184
7185 // thisMBB:
7186 // ...
7187 // TrueVal = ...
7188 // cmpTY ccX, r1, r2
7189 // bCC copy1MBB
7190 // fallthrough --> copy0MBB
7191 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00007192 MachineFunction *F = BB->getParent();
7193 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7194 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00007195 F->insert(It, copy0MBB);
7196 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007197
7198 // Transfer the remainder of BB and its successor edges to sinkMBB.
7199 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007200 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007201 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7202
Dan Gohmanf4f04102010-07-06 15:49:48 +00007203 BB->addSuccessor(copy0MBB);
7204 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00007205
Dan Gohman34396292010-07-06 20:24:04 +00007206 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7207 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7208
Evan Cheng10043e22007-01-19 07:51:42 +00007209 // copy0MBB:
7210 // %FalseValue = ...
7211 // # fallthrough to sinkMBB
7212 BB = copy0MBB;
7213
7214 // Update machine-CFG edges
7215 BB->addSuccessor(sinkMBB);
7216
7217 // sinkMBB:
7218 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7219 // ...
7220 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007221 BuildMI(*BB, BB->begin(), dl,
7222 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00007223 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7224 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7225
Dan Gohman34396292010-07-06 20:24:04 +00007226 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00007227 return BB;
7228 }
Evan Chengb972e562009-08-07 00:34:42 +00007229
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007230 case ARM::BCCi64:
7231 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00007232 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007233 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00007234
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007235 // Compare both parts that make up the double comparison separately for
7236 // equality.
7237 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7238
7239 unsigned LHS1 = MI->getOperand(1).getReg();
7240 unsigned LHS2 = MI->getOperand(2).getReg();
7241 if (RHSisZero) {
7242 AddDefaultPred(BuildMI(BB, dl,
7243 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7244 .addReg(LHS1).addImm(0));
7245 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7246 .addReg(LHS2).addImm(0)
7247 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7248 } else {
7249 unsigned RHS1 = MI->getOperand(3).getReg();
7250 unsigned RHS2 = MI->getOperand(4).getReg();
7251 AddDefaultPred(BuildMI(BB, dl,
7252 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7253 .addReg(LHS1).addReg(RHS1));
7254 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7255 .addReg(LHS2).addReg(RHS2)
7256 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7257 }
7258
7259 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7260 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7261 if (MI->getOperand(0).getImm() == ARMCC::NE)
7262 std::swap(destMBB, exitMBB);
7263
7264 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7265 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00007266 if (isThumb2)
7267 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7268 else
7269 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007270
7271 MI->eraseFromParent(); // The pseudo instruction is gone now.
7272 return BB;
7273 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007274
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007275 case ARM::Int_eh_sjlj_setjmp:
7276 case ARM::Int_eh_sjlj_setjmp_nofp:
7277 case ARM::tInt_eh_sjlj_setjmp:
7278 case ARM::t2Int_eh_sjlj_setjmp:
7279 case ARM::t2Int_eh_sjlj_setjmp_nofp:
7280 EmitSjLjDispatchBlock(MI, BB);
7281 return BB;
7282
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007283 case ARM::ABS:
7284 case ARM::t2ABS: {
7285 // To insert an ABS instruction, we have to insert the
7286 // diamond control-flow pattern. The incoming instruction knows the
7287 // source vreg to test against 0, the destination vreg to set,
7288 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00007289 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007290 // It transforms
7291 // V1 = ABS V0
7292 // into
7293 // V2 = MOVS V0
7294 // BCC (branch to SinkBB if V0 >= 0)
7295 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00007296 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007297 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7298 MachineFunction::iterator BBI = BB;
7299 ++BBI;
7300 MachineFunction *Fn = BB->getParent();
7301 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7302 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7303 Fn->insert(BBI, RSBBB);
7304 Fn->insert(BBI, SinkBB);
7305
7306 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7307 unsigned int ABSDstReg = MI->getOperand(0).getReg();
7308 bool isThumb2 = Subtarget->isThumb2();
7309 MachineRegisterInfo &MRI = Fn->getRegInfo();
7310 // In Thumb mode S must not be specified if source register is the SP or
7311 // PC and if destination register is the SP, so restrict register class
Craig Topperc7242e02012-04-20 07:30:17 +00007312 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
7313 (const TargetRegisterClass*)&ARM::rGPRRegClass :
7314 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007315
7316 // Transfer the remainder of BB and its successor edges to sinkMBB.
7317 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007318 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007319 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7320
7321 BB->addSuccessor(RSBBB);
7322 BB->addSuccessor(SinkBB);
7323
7324 // fall through to SinkMBB
7325 RSBBB->addSuccessor(SinkBB);
7326
Manman Rene0763c72012-06-15 21:32:12 +00007327 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00007328 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00007329 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7330 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007331
7332 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00007333 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007334 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7335 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7336
7337 // insert rsbri in RSBBB
7338 // Note: BCC and rsbri will be converted into predicated rsbmi
7339 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00007340 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007341 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Rene0763c72012-06-15 21:32:12 +00007342 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007343 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7344
Andrew Trick3f07c422011-10-18 18:40:53 +00007345 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007346 // reuse ABSDstReg to not change uses of ABS instruction
7347 BuildMI(*SinkBB, SinkBB->begin(), dl,
7348 TII->get(ARM::PHI), ABSDstReg)
7349 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00007350 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007351
7352 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00007353 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007354
7355 // return last added BB
7356 return SinkBB;
7357 }
Manman Rene8735522012-06-01 19:33:18 +00007358 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00007359 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00007360 return EmitStructByval(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00007361 }
7362}
7363
Evan Chenge6fba772011-08-30 19:09:48 +00007364void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7365 SDNode *Node) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007366 if (!MI->hasPostISelHook()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007367 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
7368 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
7369 return;
7370 }
7371
Evan Cheng7f8e5632011-12-07 07:15:52 +00007372 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00007373 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7374 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7375 // operand is still set to noreg. If needed, set the optional operand's
7376 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00007377 //
Andrew Trick88b24502011-10-18 19:18:52 +00007378 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00007379
Andrew Trick924123a2011-09-21 02:20:46 +00007380 // Rename pseudo opcodes.
7381 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7382 if (NewOpc) {
7383 const ARMBaseInstrInfo *TII =
7384 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick88b24502011-10-18 19:18:52 +00007385 MCID = &TII->get(NewOpc);
7386
7387 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7388 "converted opcode should be the same except for cc_out");
7389
7390 MI->setDesc(*MCID);
7391
7392 // Add the optional cc_out operand
7393 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00007394 }
Andrew Trick88b24502011-10-18 19:18:52 +00007395 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00007396
7397 // Any ARM instruction that sets the 's' bit should specify an optional
7398 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00007399 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007400 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007401 return;
7402 }
Andrew Trick924123a2011-09-21 02:20:46 +00007403 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7404 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007405 bool definesCPSR = false;
7406 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00007407 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00007408 i != e; ++i) {
7409 const MachineOperand &MO = MI->getOperand(i);
7410 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7411 definesCPSR = true;
7412 if (MO.isDead())
7413 deadCPSR = true;
7414 MI->RemoveOperand(i);
7415 break;
Evan Chenge6fba772011-08-30 19:09:48 +00007416 }
7417 }
Andrew Trick8586e622011-09-20 03:17:40 +00007418 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00007419 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007420 return;
7421 }
7422 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00007423 if (deadCPSR) {
7424 assert(!MI->getOperand(ccOutIdx).getReg() &&
7425 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00007426 return;
Andrew Trick924123a2011-09-21 02:20:46 +00007427 }
Andrew Trick8586e622011-09-20 03:17:40 +00007428
Andrew Trick924123a2011-09-21 02:20:46 +00007429 // If this instruction was defined with an optional CPSR def and its dag node
7430 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007431 MachineOperand &MO = MI->getOperand(ccOutIdx);
7432 MO.setReg(ARM::CPSR);
7433 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00007434}
7435
Evan Cheng10043e22007-01-19 07:51:42 +00007436//===----------------------------------------------------------------------===//
7437// ARM Optimization Hooks
7438//===----------------------------------------------------------------------===//
7439
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007440// Helper function that checks if N is a null or all ones constant.
7441static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7442 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7443 if (!C)
7444 return false;
7445 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7446}
7447
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007448// Return true if N is conditionally 0 or all ones.
7449// Detects these expressions where cc is an i1 value:
7450//
7451// (select cc 0, y) [AllOnes=0]
7452// (select cc y, 0) [AllOnes=0]
7453// (zext cc) [AllOnes=0]
7454// (sext cc) [AllOnes=0/1]
7455// (select cc -1, y) [AllOnes=1]
7456// (select cc y, -1) [AllOnes=1]
7457//
7458// Invert is set when N is the null/all ones constant when CC is false.
7459// OtherOp is set to the alternative value of N.
7460static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7461 SDValue &CC, bool &Invert,
7462 SDValue &OtherOp,
7463 SelectionDAG &DAG) {
7464 switch (N->getOpcode()) {
7465 default: return false;
7466 case ISD::SELECT: {
7467 CC = N->getOperand(0);
7468 SDValue N1 = N->getOperand(1);
7469 SDValue N2 = N->getOperand(2);
7470 if (isZeroOrAllOnes(N1, AllOnes)) {
7471 Invert = false;
7472 OtherOp = N2;
7473 return true;
7474 }
7475 if (isZeroOrAllOnes(N2, AllOnes)) {
7476 Invert = true;
7477 OtherOp = N1;
7478 return true;
7479 }
7480 return false;
7481 }
7482 case ISD::ZERO_EXTEND:
7483 // (zext cc) can never be the all ones value.
7484 if (AllOnes)
7485 return false;
7486 // Fall through.
7487 case ISD::SIGN_EXTEND: {
7488 EVT VT = N->getValueType(0);
7489 CC = N->getOperand(0);
7490 if (CC.getValueType() != MVT::i1)
7491 return false;
7492 Invert = !AllOnes;
7493 if (AllOnes)
7494 // When looking for an AllOnes constant, N is an sext, and the 'other'
7495 // value is 0.
7496 OtherOp = DAG.getConstant(0, VT);
7497 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7498 // When looking for a 0 constant, N can be zext or sext.
7499 OtherOp = DAG.getConstant(1, VT);
7500 else
7501 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7502 return true;
7503 }
7504 }
7505}
7506
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007507// Combine a constant select operand into its use:
7508//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007509// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7510// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7511// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7512// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7513// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007514//
7515// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007516// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007517//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007518// Also recognize sext/zext from i1:
7519//
7520// (add (zext cc), x) -> (select cc (add x, 1), x)
7521// (add (sext cc), x) -> (select cc (add x, -1), x)
7522//
7523// These transformations eventually create predicated instructions.
7524//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007525// @param N The node to transform.
7526// @param Slct The N operand that is a select.
7527// @param OtherOp The other N operand (x above).
7528// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007529// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007530// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00007531static
7532SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007533 TargetLowering::DAGCombinerInfo &DCI,
7534 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00007535 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00007536 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007537 SDValue NonConstantVal;
7538 SDValue CCOp;
7539 bool SwapSelectOps;
7540 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7541 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007542 return SDValue();
7543
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007544 // Slct is now know to be the desired identity constant when CC is true.
7545 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007546 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007547 OtherOp, NonConstantVal);
7548 // Unless SwapSelectOps says CC should be false.
7549 if (SwapSelectOps)
7550 std::swap(TrueVal, FalseVal);
7551
Andrew Trickef9de2a2013-05-25 02:42:55 +00007552 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007553 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00007554}
7555
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007556// Attempt combineSelectAndUse on each operand of a commutative operator N.
7557static
7558SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7559 TargetLowering::DAGCombinerInfo &DCI) {
7560 SDValue N0 = N->getOperand(0);
7561 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007562 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007563 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7564 if (Result.getNode())
7565 return Result;
7566 }
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007567 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007568 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7569 if (Result.getNode())
7570 return Result;
7571 }
7572 return SDValue();
7573}
7574
Eric Christopher1b8b94192011-06-29 21:10:36 +00007575// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00007576// (only after legalization).
7577static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7578 TargetLowering::DAGCombinerInfo &DCI,
7579 const ARMSubtarget *Subtarget) {
7580
7581 // Only perform optimization if after legalize, and if NEON is available. We
7582 // also expected both operands to be BUILD_VECTORs.
7583 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7584 || N0.getOpcode() != ISD::BUILD_VECTOR
7585 || N1.getOpcode() != ISD::BUILD_VECTOR)
7586 return SDValue();
7587
7588 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7589 EVT VT = N->getValueType(0);
7590 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7591 return SDValue();
7592
7593 // Check that the vector operands are of the right form.
7594 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7595 // operands, where N is the size of the formed vector.
7596 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7597 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007598
7599 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00007600 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00007601 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00007602 SDValue Vec = N0->getOperand(0)->getOperand(0);
7603 SDNode *V = Vec.getNode();
7604 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00007605
Eric Christopher1b8b94192011-06-29 21:10:36 +00007606 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007607 // check to see if each of their operands are an EXTRACT_VECTOR with
7608 // the same vector and appropriate index.
7609 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7610 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7611 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00007612
Tanya Lattnere9e67052011-06-14 23:48:48 +00007613 SDValue ExtVec0 = N0->getOperand(i);
7614 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007615
Tanya Lattnere9e67052011-06-14 23:48:48 +00007616 // First operand is the vector, verify its the same.
7617 if (V != ExtVec0->getOperand(0).getNode() ||
7618 V != ExtVec1->getOperand(0).getNode())
7619 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00007620
Tanya Lattnere9e67052011-06-14 23:48:48 +00007621 // Second is the constant, verify its correct.
7622 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7623 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00007624
Tanya Lattnere9e67052011-06-14 23:48:48 +00007625 // For the constant, we want to see all the even or all the odd.
7626 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7627 || C1->getZExtValue() != nextIndex+1)
7628 return SDValue();
7629
7630 // Increment index.
7631 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00007632 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00007633 return SDValue();
7634 }
7635
7636 // Create VPADDL node.
7637 SelectionDAG &DAG = DCI.DAG;
7638 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00007639
7640 // Build operand list.
7641 SmallVector<SDValue, 8> Ops;
7642 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7643 TLI.getPointerTy()));
7644
7645 // Input is the vector.
7646 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007647
Tanya Lattnere9e67052011-06-14 23:48:48 +00007648 // Get widened type and narrowed type.
7649 MVT widenType;
7650 unsigned numElem = VT.getVectorNumElements();
Silviu Barangaa3106e62014-04-03 10:44:27 +00007651
7652 EVT inputLaneType = Vec.getValueType().getVectorElementType();
7653 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00007654 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7655 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7656 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7657 default:
Craig Toppere55c5562012-02-07 02:50:20 +00007658 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00007659 }
7660
Craig Topper48d114b2014-04-26 18:35:24 +00007661 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N), widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00007662 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
7663 return DAG.getNode(ExtOp, SDLoc(N), VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00007664}
7665
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007666static SDValue findMUL_LOHI(SDValue V) {
7667 if (V->getOpcode() == ISD::UMUL_LOHI ||
7668 V->getOpcode() == ISD::SMUL_LOHI)
7669 return V;
7670 return SDValue();
7671}
7672
7673static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7674 TargetLowering::DAGCombinerInfo &DCI,
7675 const ARMSubtarget *Subtarget) {
7676
7677 if (Subtarget->isThumb1Only()) return SDValue();
7678
7679 // Only perform the checks after legalize when the pattern is available.
7680 if (DCI.isBeforeLegalize()) return SDValue();
7681
7682 // Look for multiply add opportunities.
7683 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7684 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7685 // a glue link from the first add to the second add.
7686 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
7687 // a S/UMLAL instruction.
7688 // loAdd UMUL_LOHI
7689 // \ / :lo \ :hi
7690 // \ / \ [no multiline comment]
7691 // ADDC | hiAdd
7692 // \ :glue / /
7693 // \ / /
7694 // ADDE
7695 //
7696 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
7697 SDValue AddcOp0 = AddcNode->getOperand(0);
7698 SDValue AddcOp1 = AddcNode->getOperand(1);
7699
7700 // Check if the two operands are from the same mul_lohi node.
7701 if (AddcOp0.getNode() == AddcOp1.getNode())
7702 return SDValue();
7703
7704 assert(AddcNode->getNumValues() == 2 &&
7705 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00007706 "Expect ADDC with two result values. First: i32");
7707
7708 // Check that we have a glued ADDC node.
7709 if (AddcNode->getValueType(1) != MVT::Glue)
7710 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007711
7712 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
7713 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
7714 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
7715 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
7716 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
7717 return SDValue();
7718
7719 // Look for the glued ADDE.
7720 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00007721 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007722 return SDValue();
7723
7724 // Make sure it is really an ADDE.
7725 if (AddeNode->getOpcode() != ISD::ADDE)
7726 return SDValue();
7727
7728 assert(AddeNode->getNumOperands() == 3 &&
7729 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
7730 "ADDE node has the wrong inputs");
7731
7732 // Check for the triangle shape.
7733 SDValue AddeOp0 = AddeNode->getOperand(0);
7734 SDValue AddeOp1 = AddeNode->getOperand(1);
7735
7736 // Make sure that the ADDE operands are not coming from the same node.
7737 if (AddeOp0.getNode() == AddeOp1.getNode())
7738 return SDValue();
7739
7740 // Find the MUL_LOHI node walking up ADDE's operands.
7741 bool IsLeftOperandMUL = false;
7742 SDValue MULOp = findMUL_LOHI(AddeOp0);
7743 if (MULOp == SDValue())
7744 MULOp = findMUL_LOHI(AddeOp1);
7745 else
7746 IsLeftOperandMUL = true;
7747 if (MULOp == SDValue())
7748 return SDValue();
7749
7750 // Figure out the right opcode.
7751 unsigned Opc = MULOp->getOpcode();
7752 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
7753
7754 // Figure out the high and low input values to the MLAL node.
7755 SDValue* HiMul = &MULOp;
Craig Topper062a2ba2014-04-25 05:30:21 +00007756 SDValue* HiAdd = nullptr;
7757 SDValue* LoMul = nullptr;
7758 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007759
7760 if (IsLeftOperandMUL)
7761 HiAdd = &AddeOp1;
7762 else
7763 HiAdd = &AddeOp0;
7764
7765
7766 if (AddcOp0->getOpcode() == Opc) {
7767 LoMul = &AddcOp0;
7768 LowAdd = &AddcOp1;
7769 }
7770 if (AddcOp1->getOpcode() == Opc) {
7771 LoMul = &AddcOp1;
7772 LowAdd = &AddcOp0;
7773 }
7774
Craig Topper062a2ba2014-04-25 05:30:21 +00007775 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007776 return SDValue();
7777
7778 if (LoMul->getNode() != HiMul->getNode())
7779 return SDValue();
7780
7781 // Create the merged node.
7782 SelectionDAG &DAG = DCI.DAG;
7783
7784 // Build operand list.
7785 SmallVector<SDValue, 8> Ops;
7786 Ops.push_back(LoMul->getOperand(0));
7787 Ops.push_back(LoMul->getOperand(1));
7788 Ops.push_back(*LowAdd);
7789 Ops.push_back(*HiAdd);
7790
Andrew Trickef9de2a2013-05-25 02:42:55 +00007791 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00007792 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007793
7794 // Replace the ADDs' nodes uses by the MLA node's values.
7795 SDValue HiMLALResult(MLALNode.getNode(), 1);
7796 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
7797
7798 SDValue LoMLALResult(MLALNode.getNode(), 0);
7799 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
7800
7801 // Return original node to notify the driver to stop replacing.
7802 SDValue resNode(AddcNode, 0);
7803 return resNode;
7804}
7805
7806/// PerformADDCCombine - Target-specific dag combine transform from
7807/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
7808static SDValue PerformADDCCombine(SDNode *N,
7809 TargetLowering::DAGCombinerInfo &DCI,
7810 const ARMSubtarget *Subtarget) {
7811
7812 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
7813
7814}
7815
Bob Wilson728eb292010-07-29 20:34:14 +00007816/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7817/// operands N0 and N1. This is a helper for PerformADDCombine that is
7818/// called with the default operands, and if that fails, with commuted
7819/// operands.
7820static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007821 TargetLowering::DAGCombinerInfo &DCI,
7822 const ARMSubtarget *Subtarget){
7823
7824 // Attempt to create vpaddl for this add.
7825 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7826 if (Result.getNode())
7827 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00007828
Chris Lattner4147f082009-03-12 06:52:53 +00007829 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007830 if (N0.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00007831 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7832 if (Result.getNode()) return Result;
7833 }
Chris Lattner4147f082009-03-12 06:52:53 +00007834 return SDValue();
7835}
7836
Bob Wilson728eb292010-07-29 20:34:14 +00007837/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7838///
7839static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007840 TargetLowering::DAGCombinerInfo &DCI,
7841 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00007842 SDValue N0 = N->getOperand(0);
7843 SDValue N1 = N->getOperand(1);
7844
7845 // First try with the default operand order.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007846 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00007847 if (Result.getNode())
7848 return Result;
7849
7850 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007851 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00007852}
7853
Chris Lattner4147f082009-03-12 06:52:53 +00007854/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00007855///
Chris Lattner4147f082009-03-12 06:52:53 +00007856static SDValue PerformSUBCombine(SDNode *N,
7857 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00007858 SDValue N0 = N->getOperand(0);
7859 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00007860
Chris Lattner4147f082009-03-12 06:52:53 +00007861 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007862 if (N1.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00007863 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7864 if (Result.getNode()) return Result;
7865 }
Bob Wilson7117a912009-03-20 22:42:55 +00007866
Chris Lattner4147f082009-03-12 06:52:53 +00007867 return SDValue();
7868}
7869
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007870/// PerformVMULCombine
7871/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7872/// special multiplier accumulator forwarding.
7873/// vmul d3, d0, d2
7874/// vmla d3, d1, d2
7875/// is faster than
7876/// vadd d3, d0, d1
7877/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00007878// However, for (A + B) * (A + B),
7879// vadd d2, d0, d1
7880// vmul d3, d0, d2
7881// vmla d3, d1, d2
7882// is slower than
7883// vadd d2, d0, d1
7884// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007885static SDValue PerformVMULCombine(SDNode *N,
7886 TargetLowering::DAGCombinerInfo &DCI,
7887 const ARMSubtarget *Subtarget) {
7888 if (!Subtarget->hasVMLxForwarding())
7889 return SDValue();
7890
7891 SelectionDAG &DAG = DCI.DAG;
7892 SDValue N0 = N->getOperand(0);
7893 SDValue N1 = N->getOperand(1);
7894 unsigned Opcode = N0.getOpcode();
7895 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7896 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00007897 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007898 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7899 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7900 return SDValue();
7901 std::swap(N0, N1);
7902 }
7903
Weiming Zhao2052f482013-09-25 23:12:06 +00007904 if (N0 == N1)
7905 return SDValue();
7906
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007907 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00007908 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007909 SDValue N00 = N0->getOperand(0);
7910 SDValue N01 = N0->getOperand(1);
7911 return DAG.getNode(Opcode, DL, VT,
7912 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7913 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7914}
7915
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007916static SDValue PerformMULCombine(SDNode *N,
7917 TargetLowering::DAGCombinerInfo &DCI,
7918 const ARMSubtarget *Subtarget) {
7919 SelectionDAG &DAG = DCI.DAG;
7920
7921 if (Subtarget->isThumb1Only())
7922 return SDValue();
7923
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007924 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7925 return SDValue();
7926
7927 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007928 if (VT.is64BitVector() || VT.is128BitVector())
7929 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007930 if (VT != MVT::i32)
7931 return SDValue();
7932
7933 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
7934 if (!C)
7935 return SDValue();
7936
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00007937 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00007938 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00007939
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007940 ShiftAmt = ShiftAmt & (32 - 1);
7941 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00007942 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007943
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00007944 SDValue Res;
7945 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00007946
7947 if (MulAmt >= 0) {
7948 if (isPowerOf2_32(MulAmt - 1)) {
7949 // (mul x, 2^N + 1) => (add (shl x, N), x)
7950 Res = DAG.getNode(ISD::ADD, DL, VT,
7951 V,
7952 DAG.getNode(ISD::SHL, DL, VT,
7953 V,
7954 DAG.getConstant(Log2_32(MulAmt - 1),
7955 MVT::i32)));
7956 } else if (isPowerOf2_32(MulAmt + 1)) {
7957 // (mul x, 2^N - 1) => (sub (shl x, N), x)
7958 Res = DAG.getNode(ISD::SUB, DL, VT,
7959 DAG.getNode(ISD::SHL, DL, VT,
7960 V,
7961 DAG.getConstant(Log2_32(MulAmt + 1),
7962 MVT::i32)),
7963 V);
7964 } else
7965 return SDValue();
7966 } else {
7967 uint64_t MulAmtAbs = -MulAmt;
7968 if (isPowerOf2_32(MulAmtAbs + 1)) {
7969 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
7970 Res = DAG.getNode(ISD::SUB, DL, VT,
7971 V,
7972 DAG.getNode(ISD::SHL, DL, VT,
7973 V,
7974 DAG.getConstant(Log2_32(MulAmtAbs + 1),
7975 MVT::i32)));
7976 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
7977 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
7978 Res = DAG.getNode(ISD::ADD, DL, VT,
7979 V,
7980 DAG.getNode(ISD::SHL, DL, VT,
7981 V,
7982 DAG.getConstant(Log2_32(MulAmtAbs-1),
7983 MVT::i32)));
7984 Res = DAG.getNode(ISD::SUB, DL, VT,
7985 DAG.getConstant(0, MVT::i32),Res);
7986
7987 } else
7988 return SDValue();
7989 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00007990
7991 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00007992 Res = DAG.getNode(ISD::SHL, DL, VT,
7993 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007994
7995 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00007996 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007997 return SDValue();
7998}
7999
Owen Anderson30c48922010-11-05 19:27:46 +00008000static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00008001 TargetLowering::DAGCombinerInfo &DCI,
8002 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00008003
Owen Anderson30c48922010-11-05 19:27:46 +00008004 // Attempt to use immediate-form VBIC
8005 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008006 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00008007 EVT VT = N->getValueType(0);
8008 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008009
Tanya Lattner266792a2011-04-07 15:24:20 +00008010 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8011 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008012
Owen Anderson30c48922010-11-05 19:27:46 +00008013 APInt SplatBits, SplatUndef;
8014 unsigned SplatBitSize;
8015 bool HasAnyUndefs;
8016 if (BVN &&
8017 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8018 if (SplatBitSize <= 64) {
8019 EVT VbicVT;
8020 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8021 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00008022 DAG, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008023 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00008024 if (Val.getNode()) {
8025 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008026 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00008027 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008028 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00008029 }
8030 }
8031 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008032
Evan Chenge87681c2012-02-23 01:19:06 +00008033 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008034 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8035 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8036 if (Result.getNode())
8037 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008038 }
8039
Owen Anderson30c48922010-11-05 19:27:46 +00008040 return SDValue();
8041}
8042
Jim Grosbach11013ed2010-07-16 23:05:05 +00008043/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8044static SDValue PerformORCombine(SDNode *N,
8045 TargetLowering::DAGCombinerInfo &DCI,
8046 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008047 // Attempt to use immediate-form VORR
8048 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008049 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008050 EVT VT = N->getValueType(0);
8051 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008052
Tanya Lattner266792a2011-04-07 15:24:20 +00008053 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8054 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008055
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008056 APInt SplatBits, SplatUndef;
8057 unsigned SplatBitSize;
8058 bool HasAnyUndefs;
8059 if (BVN && Subtarget->hasNEON() &&
8060 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8061 if (SplatBitSize <= 64) {
8062 EVT VorrVT;
8063 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8064 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00008065 DAG, VorrVT, VT.is128BitVector(),
8066 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008067 if (Val.getNode()) {
8068 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008069 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008070 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008071 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008072 }
8073 }
8074 }
8075
Evan Chenge87681c2012-02-23 01:19:06 +00008076 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008077 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8078 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8079 if (Result.getNode())
8080 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008081 }
8082
Nadav Rotem3a94c542012-08-13 18:52:44 +00008083 // The code below optimizes (or (and X, Y), Z).
8084 // The AND operand needs to have a single user to make these optimizations
8085 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008086 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00008087 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008088 return SDValue();
8089 SDValue N1 = N->getOperand(1);
8090
8091 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8092 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8093 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8094 APInt SplatUndef;
8095 unsigned SplatBitSize;
8096 bool HasAnyUndefs;
8097
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008098 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008099 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008100 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8101 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008102 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008103 HasAnyUndefs) && !HasAnyUndefs) {
8104 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8105 HasAnyUndefs) && !HasAnyUndefs) {
8106 // Ensure that the bit width of the constants are the same and that
8107 // the splat arguments are logical inverses as per the pattern we
8108 // are trying to simplify.
8109 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
8110 SplatBits0 == ~SplatBits1) {
8111 // Canonicalize the vector type to make instruction selection
8112 // simpler.
8113 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8114 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8115 N0->getOperand(1),
8116 N0->getOperand(0),
8117 N1->getOperand(0));
8118 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8119 }
8120 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008121 }
8122 }
8123
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008124 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8125 // reasonable.
8126
Jim Grosbach11013ed2010-07-16 23:05:05 +00008127 // BFI is only available on V6T2+
8128 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8129 return SDValue();
8130
Andrew Trickef9de2a2013-05-25 02:42:55 +00008131 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008132 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008133 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008134 //
8135 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008136 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008137 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008138 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008139 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008140 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008141
Jim Grosbach11013ed2010-07-16 23:05:05 +00008142 if (VT != MVT::i32)
8143 return SDValue();
8144
Evan Cheng2e51bb42010-12-13 20:32:54 +00008145 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008146
Jim Grosbach11013ed2010-07-16 23:05:05 +00008147 // The value and the mask need to be constants so we can verify this is
8148 // actually a bitfield set. If the mask is 0xffff, we can do better
8149 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00008150 SDValue MaskOp = N0.getOperand(1);
8151 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8152 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008153 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008154 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008155 if (Mask == 0xffff)
8156 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008157 SDValue Res;
8158 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008159 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8160 if (N1C) {
8161 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00008162 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008163 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008164
Evan Cheng34345752010-12-11 04:11:38 +00008165 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008166 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00008167
Evan Cheng2e51bb42010-12-13 20:32:54 +00008168 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Cheng34345752010-12-11 04:11:38 +00008169 DAG.getConstant(Val, MVT::i32),
8170 DAG.getConstant(Mask, MVT::i32));
8171
8172 // Do not add new nodes to DAG combiner worklist.
8173 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008174 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00008175 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008176 } else if (N1.getOpcode() == ISD::AND) {
8177 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008178 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8179 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008180 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008181 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008182
Eric Christopherd5530962011-03-26 01:21:03 +00008183 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8184 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008185 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008186 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008187 // The pack halfword instruction works better for masks that fit it,
8188 // so use that when it's available.
8189 if (Subtarget->hasT2ExtractPack() &&
8190 (Mask == 0xffff || Mask == 0xffff0000))
8191 return SDValue();
8192 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008193 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008194 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopherd5530962011-03-26 01:21:03 +00008195 DAG.getConstant(amt, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008196 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008197 DAG.getConstant(Mask, MVT::i32));
8198 // Do not add new nodes to DAG combiner worklist.
8199 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008200 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008201 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008202 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008203 // The pack halfword instruction works better for masks that fit it,
8204 // so use that when it's available.
8205 if (Subtarget->hasT2ExtractPack() &&
8206 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8207 return SDValue();
8208 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008209 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008210 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008211 DAG.getConstant(lsb, MVT::i32));
8212 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopherd5530962011-03-26 01:21:03 +00008213 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008214 // Do not add new nodes to DAG combiner worklist.
8215 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008216 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008217 }
8218 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008219
Evan Cheng2e51bb42010-12-13 20:32:54 +00008220 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8221 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8222 ARM::isBitFieldInvertedMask(~Mask)) {
8223 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8224 // where lsb(mask) == #shamt and masked bits of B are known zero.
8225 SDValue ShAmt = N00.getOperand(1);
8226 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008227 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008228 if (ShAmtC != LSB)
8229 return SDValue();
8230
8231 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
8232 DAG.getConstant(~Mask, MVT::i32));
8233
8234 // Do not add new nodes to DAG combiner worklist.
8235 DCI.CombineTo(N, Res, false);
8236 }
8237
Jim Grosbach11013ed2010-07-16 23:05:05 +00008238 return SDValue();
8239}
8240
Evan Chenge87681c2012-02-23 01:19:06 +00008241static SDValue PerformXORCombine(SDNode *N,
8242 TargetLowering::DAGCombinerInfo &DCI,
8243 const ARMSubtarget *Subtarget) {
8244 EVT VT = N->getValueType(0);
8245 SelectionDAG &DAG = DCI.DAG;
8246
8247 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8248 return SDValue();
8249
8250 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008251 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8252 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8253 if (Result.getNode())
8254 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008255 }
8256
8257 return SDValue();
8258}
8259
Evan Cheng6d02d902011-06-15 01:12:31 +00008260/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8261/// the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00008262static SDValue PerformBFICombine(SDNode *N,
8263 TargetLowering::DAGCombinerInfo &DCI) {
8264 SDValue N1 = N->getOperand(1);
8265 if (N1.getOpcode() == ISD::AND) {
8266 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8267 if (!N11C)
8268 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008269 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008270 unsigned LSB = countTrailingZeros(~InvMask);
8271 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Evan Cheng6d02d902011-06-15 01:12:31 +00008272 unsigned Mask = (1 << Width)-1;
Evan Chengc1778132010-12-14 03:22:07 +00008273 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008274 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008275 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00008276 N->getOperand(0), N1.getOperand(0),
8277 N->getOperand(2));
8278 }
8279 return SDValue();
8280}
8281
Bob Wilson22806742010-09-22 22:09:21 +00008282/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8283/// ARMISD::VMOVRRD.
8284static SDValue PerformVMOVRRDCombine(SDNode *N,
8285 TargetLowering::DAGCombinerInfo &DCI) {
8286 // vmovrrd(vmovdrr x, y) -> x,y
8287 SDValue InDouble = N->getOperand(0);
8288 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
8289 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008290
8291 // vmovrrd(load f64) -> (load i32), (load i32)
8292 SDNode *InNode = InDouble.getNode();
8293 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8294 InNode->getValueType(0) == MVT::f64 &&
8295 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8296 !cast<LoadSDNode>(InNode)->isVolatile()) {
8297 // TODO: Should this be done for non-FrameIndex operands?
8298 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8299
8300 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008301 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008302 SDValue BasePtr = LD->getBasePtr();
8303 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8304 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008305 LD->isNonTemporal(), LD->isInvariant(),
8306 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008307
8308 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8309 DAG.getConstant(4, MVT::i32));
8310 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8311 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008312 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008313 std::min(4U, LD->getAlignment() / 2));
8314
8315 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
8316 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
8317 DCI.RemoveFromWorklist(LD);
8318 DAG.DeleteNode(LD);
8319 return Result;
8320 }
8321
Bob Wilson22806742010-09-22 22:09:21 +00008322 return SDValue();
8323}
8324
8325/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8326/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8327static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8328 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8329 SDValue Op0 = N->getOperand(0);
8330 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00008331 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008332 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008333 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008334 Op1 = Op1.getOperand(0);
8335 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8336 Op0.getNode() == Op1.getNode() &&
8337 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008338 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00008339 N->getValueType(0), Op0.getOperand(0));
8340 return SDValue();
8341}
8342
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008343/// PerformSTORECombine - Target-specific dag combine xforms for
8344/// ISD::STORE.
8345static SDValue PerformSTORECombine(SDNode *N,
8346 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008347 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008348 if (St->isVolatile())
8349 return SDValue();
8350
Andrew Trickbc325162012-07-18 18:34:24 +00008351 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosiere0e38f62012-04-09 20:32:02 +00008352 // pack all of the elements in one place. Next, store to memory in fewer
8353 // chunks.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008354 SDValue StVal = St->getValue();
Chad Rosiere0e38f62012-04-09 20:32:02 +00008355 EVT VT = StVal.getValueType();
8356 if (St->isTruncatingStore() && VT.isVector()) {
8357 SelectionDAG &DAG = DCI.DAG;
8358 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8359 EVT StVT = St->getMemoryVT();
8360 unsigned NumElems = VT.getVectorNumElements();
8361 assert(StVT != VT && "Cannot truncate to the same type");
8362 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
8363 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
8364
8365 // From, To sizes and ElemCount must be pow of two
8366 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
8367
8368 // We are going to use the original vector elt for storing.
8369 // Accumulated smaller vector elements must be a multiple of the store size.
8370 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
8371
8372 unsigned SizeRatio = FromEltSz / ToEltSz;
8373 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
8374
8375 // Create a type on which we perform the shuffle.
8376 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
8377 NumElems*SizeRatio);
8378 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
8379
Andrew Trickef9de2a2013-05-25 02:42:55 +00008380 SDLoc DL(St);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008381 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
8382 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
8383 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
8384
8385 // Can't shuffle using an illegal type.
8386 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
8387
8388 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
8389 DAG.getUNDEF(WideVec.getValueType()),
8390 ShuffleVec.data());
8391 // At this point all of the data is stored at the bottom of the
8392 // register. We now need to save it to mem.
8393
8394 // Find the largest store unit
8395 MVT StoreType = MVT::i8;
8396 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
8397 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
8398 MVT Tp = (MVT::SimpleValueType)tp;
8399 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
8400 StoreType = Tp;
8401 }
8402 // Didn't find a legal store type.
8403 if (!TLI.isTypeLegal(StoreType))
8404 return SDValue();
8405
8406 // Bitcast the original vector into a vector of store-size units
8407 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
8408 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
8409 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
8410 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
8411 SmallVector<SDValue, 8> Chains;
8412 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
8413 TLI.getPointerTy());
8414 SDValue BasePtr = St->getBasePtr();
8415
8416 // Perform one or more big stores into memory.
8417 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
8418 for (unsigned I = 0; I < E; I++) {
8419 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
8420 StoreType, ShuffWide,
8421 DAG.getIntPtrConstant(I));
8422 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
8423 St->getPointerInfo(), St->isVolatile(),
8424 St->isNonTemporal(), St->getAlignment());
8425 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
8426 Increment);
8427 Chains.push_back(Ch);
8428 }
Craig Topper48d114b2014-04-26 18:35:24 +00008429 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008430 }
8431
8432 if (!ISD::isNormalStore(St))
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008433 return SDValue();
8434
Chad Rosier99cbde92012-04-09 19:38:15 +00008435 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
8436 // ARM stores of arguments in the same cache line.
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008437 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier99cbde92012-04-09 19:38:15 +00008438 StVal.getNode()->hasOneUse()) {
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008439 SelectionDAG &DAG = DCI.DAG;
Christian Pirkerb5728192014-05-08 14:06:24 +00008440 bool isBigEndian = DAG.getTargetLoweringInfo().isBigEndian();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008441 SDLoc DL(St);
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008442 SDValue BasePtr = St->getBasePtr();
8443 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
Christian Pirkerb5728192014-05-08 14:06:24 +00008444 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
8445 BasePtr, St->getPointerInfo(), St->isVolatile(),
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008446 St->isNonTemporal(), St->getAlignment());
8447
8448 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8449 DAG.getConstant(4, MVT::i32));
Christian Pirkerb5728192014-05-08 14:06:24 +00008450 return DAG.getStore(NewST1.getValue(0), DL,
8451 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008452 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
8453 St->isNonTemporal(),
8454 std::min(4U, St->getAlignment() / 2));
8455 }
8456
8457 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008458 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8459 return SDValue();
8460
Chad Rosier99cbde92012-04-09 19:38:15 +00008461 // Bitcast an i64 store extracted from a vector to f64.
8462 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008463 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008464 SDLoc dl(StVal);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008465 SDValue IntVec = StVal.getOperand(0);
8466 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8467 IntVec.getValueType().getVectorNumElements());
8468 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
8469 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
8470 Vec, StVal.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008471 dl = SDLoc(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008472 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
8473 // Make the DAGCombiner fold the bitcasts.
8474 DCI.AddToWorklist(Vec.getNode());
8475 DCI.AddToWorklist(ExtElt.getNode());
8476 DCI.AddToWorklist(V.getNode());
8477 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
8478 St->getPointerInfo(), St->isVolatile(),
8479 St->isNonTemporal(), St->getAlignment(),
8480 St->getTBAAInfo());
8481}
8482
8483/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8484/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8485/// i64 vector to have f64 elements, since the value can then be loaded
8486/// directly into a VFP register.
8487static bool hasNormalLoadOperand(SDNode *N) {
8488 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8489 for (unsigned i = 0; i < NumElts; ++i) {
8490 SDNode *Elt = N->getOperand(i).getNode();
8491 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8492 return true;
8493 }
8494 return false;
8495}
8496
Bob Wilsoncb6db982010-09-17 22:59:05 +00008497/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8498/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008499static SDValue PerformBUILD_VECTORCombine(SDNode *N,
8500 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilsoncb6db982010-09-17 22:59:05 +00008501 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8502 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8503 // into a pair of GPRs, which is fine when the value is used as a scalar,
8504 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008505 SelectionDAG &DAG = DCI.DAG;
8506 if (N->getNumOperands() == 2) {
8507 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8508 if (RV.getNode())
8509 return RV;
8510 }
Bob Wilsoncb6db982010-09-17 22:59:05 +00008511
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008512 // Load i64 elements as f64 values so that type legalization does not split
8513 // them up into i32 values.
8514 EVT VT = N->getValueType(0);
8515 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8516 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008517 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008518 SmallVector<SDValue, 8> Ops;
8519 unsigned NumElts = VT.getVectorNumElements();
8520 for (unsigned i = 0; i < NumElts; ++i) {
8521 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8522 Ops.push_back(V);
8523 // Make the DAGCombiner fold the bitcast.
8524 DCI.AddToWorklist(V.getNode());
8525 }
8526 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00008527 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008528 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8529}
8530
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008531/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
8532static SDValue
8533PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8534 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
8535 // At that time, we may have inserted bitcasts from integer to float.
8536 // If these bitcasts have survived DAGCombine, change the lowering of this
8537 // BUILD_VECTOR in something more vector friendly, i.e., that does not
8538 // force to use floating point types.
8539
8540 // Make sure we can change the type of the vector.
8541 // This is possible iff:
8542 // 1. The vector is only used in a bitcast to a integer type. I.e.,
8543 // 1.1. Vector is used only once.
8544 // 1.2. Use is a bit convert to an integer type.
8545 // 2. The size of its operands are 32-bits (64-bits are not legal).
8546 EVT VT = N->getValueType(0);
8547 EVT EltVT = VT.getVectorElementType();
8548
8549 // Check 1.1. and 2.
8550 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
8551 return SDValue();
8552
8553 // By construction, the input type must be float.
8554 assert(EltVT == MVT::f32 && "Unexpected type!");
8555
8556 // Check 1.2.
8557 SDNode *Use = *N->use_begin();
8558 if (Use->getOpcode() != ISD::BITCAST ||
8559 Use->getValueType(0).isFloatingPoint())
8560 return SDValue();
8561
8562 // Check profitability.
8563 // Model is, if more than half of the relevant operands are bitcast from
8564 // i32, turn the build_vector into a sequence of insert_vector_elt.
8565 // Relevant operands are everything that is not statically
8566 // (i.e., at compile time) bitcasted.
8567 unsigned NumOfBitCastedElts = 0;
8568 unsigned NumElts = VT.getVectorNumElements();
8569 unsigned NumOfRelevantElts = NumElts;
8570 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
8571 SDValue Elt = N->getOperand(Idx);
8572 if (Elt->getOpcode() == ISD::BITCAST) {
8573 // Assume only bit cast to i32 will go away.
8574 if (Elt->getOperand(0).getValueType() == MVT::i32)
8575 ++NumOfBitCastedElts;
8576 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt))
8577 // Constants are statically casted, thus do not count them as
8578 // relevant operands.
8579 --NumOfRelevantElts;
8580 }
8581
8582 // Check if more than half of the elements require a non-free bitcast.
8583 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
8584 return SDValue();
8585
8586 SelectionDAG &DAG = DCI.DAG;
8587 // Create the new vector type.
8588 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
8589 // Check if the type is legal.
8590 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8591 if (!TLI.isTypeLegal(VecVT))
8592 return SDValue();
8593
8594 // Combine:
8595 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
8596 // => BITCAST INSERT_VECTOR_ELT
8597 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
8598 // (BITCAST EN), N.
8599 SDValue Vec = DAG.getUNDEF(VecVT);
8600 SDLoc dl(N);
8601 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
8602 SDValue V = N->getOperand(Idx);
8603 if (V.getOpcode() == ISD::UNDEF)
8604 continue;
8605 if (V.getOpcode() == ISD::BITCAST &&
8606 V->getOperand(0).getValueType() == MVT::i32)
8607 // Fold obvious case.
8608 V = V.getOperand(0);
8609 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00008610 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008611 // Make the DAGCombiner fold the bitcasts.
8612 DCI.AddToWorklist(V.getNode());
8613 }
8614 SDValue LaneIdx = DAG.getConstant(Idx, MVT::i32);
8615 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
8616 }
8617 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
8618 // Make the DAGCombiner fold the bitcasts.
8619 DCI.AddToWorklist(Vec.getNode());
8620 return Vec;
8621}
8622
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008623/// PerformInsertEltCombine - Target-specific dag combine xforms for
8624/// ISD::INSERT_VECTOR_ELT.
8625static SDValue PerformInsertEltCombine(SDNode *N,
8626 TargetLowering::DAGCombinerInfo &DCI) {
8627 // Bitcast an i64 load inserted into a vector to f64.
8628 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8629 EVT VT = N->getValueType(0);
8630 SDNode *Elt = N->getOperand(1).getNode();
8631 if (VT.getVectorElementType() != MVT::i64 ||
8632 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8633 return SDValue();
8634
8635 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008636 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008637 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8638 VT.getVectorNumElements());
8639 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8640 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8641 // Make the DAGCombiner fold the bitcasts.
8642 DCI.AddToWorklist(Vec.getNode());
8643 DCI.AddToWorklist(V.getNode());
8644 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8645 Vec, V, N->getOperand(2));
8646 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00008647}
8648
Bob Wilsonc7334a12010-10-27 20:38:28 +00008649/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8650/// ISD::VECTOR_SHUFFLE.
8651static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8652 // The LLVM shufflevector instruction does not require the shuffle mask
8653 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8654 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8655 // operands do not match the mask length, they are extended by concatenating
8656 // them with undef vectors. That is probably the right thing for other
8657 // targets, but for NEON it is better to concatenate two double-register
8658 // size vector operands into a single quad-register size vector. Do that
8659 // transformation here:
8660 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8661 // shuffle(concat(v1, v2), undef)
8662 SDValue Op0 = N->getOperand(0);
8663 SDValue Op1 = N->getOperand(1);
8664 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8665 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8666 Op0.getNumOperands() != 2 ||
8667 Op1.getNumOperands() != 2)
8668 return SDValue();
8669 SDValue Concat0Op1 = Op0.getOperand(1);
8670 SDValue Concat1Op1 = Op1.getOperand(1);
8671 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8672 Concat1Op1.getOpcode() != ISD::UNDEF)
8673 return SDValue();
8674 // Skip the transformation if any of the types are illegal.
8675 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8676 EVT VT = N->getValueType(0);
8677 if (!TLI.isTypeLegal(VT) ||
8678 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8679 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8680 return SDValue();
8681
Andrew Trickef9de2a2013-05-25 02:42:55 +00008682 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008683 Op0.getOperand(0), Op1.getOperand(0));
8684 // Translate the shuffle mask.
8685 SmallVector<int, 16> NewMask;
8686 unsigned NumElts = VT.getVectorNumElements();
8687 unsigned HalfElts = NumElts/2;
8688 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8689 for (unsigned n = 0; n < NumElts; ++n) {
8690 int MaskElt = SVN->getMaskElt(n);
8691 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00008692 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00008693 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00008694 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00008695 NewElt = HalfElts + MaskElt - NumElts;
8696 NewMask.push_back(NewElt);
8697 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00008698 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008699 DAG.getUNDEF(VT), NewMask.data());
8700}
8701
Bob Wilson06fce872011-02-07 17:43:21 +00008702/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
8703/// NEON load/store intrinsics to merge base address updates.
8704static SDValue CombineBaseUpdate(SDNode *N,
8705 TargetLowering::DAGCombinerInfo &DCI) {
8706 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8707 return SDValue();
8708
8709 SelectionDAG &DAG = DCI.DAG;
8710 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8711 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
8712 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
8713 SDValue Addr = N->getOperand(AddrOpIdx);
8714
8715 // Search for a use of the address operand that is an increment.
8716 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8717 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8718 SDNode *User = *UI;
8719 if (User->getOpcode() != ISD::ADD ||
8720 UI.getUse().getResNo() != Addr.getResNo())
8721 continue;
8722
8723 // Check that the add is independent of the load/store. Otherwise, folding
8724 // it would create a cycle.
8725 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8726 continue;
8727
8728 // Find the new opcode for the updating load/store.
8729 bool isLoad = true;
8730 bool isLaneOp = false;
8731 unsigned NewOpc = 0;
8732 unsigned NumVecs = 0;
8733 if (isIntrinsic) {
8734 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8735 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00008736 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008737 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8738 NumVecs = 1; break;
8739 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8740 NumVecs = 2; break;
8741 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8742 NumVecs = 3; break;
8743 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8744 NumVecs = 4; break;
8745 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8746 NumVecs = 2; isLaneOp = true; break;
8747 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8748 NumVecs = 3; isLaneOp = true; break;
8749 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8750 NumVecs = 4; isLaneOp = true; break;
8751 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
8752 NumVecs = 1; isLoad = false; break;
8753 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
8754 NumVecs = 2; isLoad = false; break;
8755 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8756 NumVecs = 3; isLoad = false; break;
8757 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8758 NumVecs = 4; isLoad = false; break;
8759 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8760 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8761 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8762 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8763 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8764 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8765 }
8766 } else {
8767 isLaneOp = true;
8768 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00008769 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008770 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8771 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8772 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8773 }
8774 }
8775
8776 // Find the size of memory referenced by the load/store.
8777 EVT VecTy;
8778 if (isLoad)
8779 VecTy = N->getValueType(0);
Owen Anderson77aa2662011-04-05 21:48:57 +00008780 else
Bob Wilson06fce872011-02-07 17:43:21 +00008781 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8782 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8783 if (isLaneOp)
8784 NumBytes /= VecTy.getVectorNumElements();
8785
8786 // If the increment is a constant, it must match the memory ref size.
8787 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8788 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8789 uint64_t IncVal = CInc->getZExtValue();
8790 if (IncVal != NumBytes)
8791 continue;
8792 } else if (NumBytes >= 3 * 16) {
8793 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8794 // separate instructions that make it harder to use a non-constant update.
8795 continue;
8796 }
8797
8798 // Create the new updating load/store node.
8799 EVT Tys[6];
8800 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8801 unsigned n;
8802 for (n = 0; n < NumResultVecs; ++n)
8803 Tys[n] = VecTy;
8804 Tys[n++] = MVT::i32;
8805 Tys[n] = MVT::Other;
Craig Topperabb4ac72014-04-16 06:10:51 +00008806 SDVTList SDTys = DAG.getVTList(ArrayRef<EVT>(Tys, NumResultVecs+2));
Bob Wilson06fce872011-02-07 17:43:21 +00008807 SmallVector<SDValue, 8> Ops;
8808 Ops.push_back(N->getOperand(0)); // incoming chain
8809 Ops.push_back(N->getOperand(AddrOpIdx));
8810 Ops.push_back(Inc);
8811 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8812 Ops.push_back(N->getOperand(i));
8813 }
8814 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008815 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, SDLoc(N), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00008816 Ops, MemInt->getMemoryVT(),
Bob Wilson06fce872011-02-07 17:43:21 +00008817 MemInt->getMemOperand());
8818
8819 // Update the uses.
8820 std::vector<SDValue> NewResults;
8821 for (unsigned i = 0; i < NumResultVecs; ++i) {
8822 NewResults.push_back(SDValue(UpdN.getNode(), i));
8823 }
8824 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8825 DCI.CombineTo(N, NewResults);
8826 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8827
8828 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00008829 }
Bob Wilson06fce872011-02-07 17:43:21 +00008830 return SDValue();
8831}
8832
Bob Wilson2d790df2010-11-28 06:51:26 +00008833/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8834/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8835/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8836/// return true.
8837static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8838 SelectionDAG &DAG = DCI.DAG;
8839 EVT VT = N->getValueType(0);
8840 // vldN-dup instructions only support 64-bit vectors for N > 1.
8841 if (!VT.is64BitVector())
8842 return false;
8843
8844 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8845 SDNode *VLD = N->getOperand(0).getNode();
8846 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8847 return false;
8848 unsigned NumVecs = 0;
8849 unsigned NewOpc = 0;
8850 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8851 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8852 NumVecs = 2;
8853 NewOpc = ARMISD::VLD2DUP;
8854 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8855 NumVecs = 3;
8856 NewOpc = ARMISD::VLD3DUP;
8857 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8858 NumVecs = 4;
8859 NewOpc = ARMISD::VLD4DUP;
8860 } else {
8861 return false;
8862 }
8863
8864 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8865 // numbers match the load.
8866 unsigned VLDLaneNo =
8867 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8868 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8869 UI != UE; ++UI) {
8870 // Ignore uses of the chain result.
8871 if (UI.getUse().getResNo() == NumVecs)
8872 continue;
8873 SDNode *User = *UI;
8874 if (User->getOpcode() != ARMISD::VDUPLANE ||
8875 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8876 return false;
8877 }
8878
8879 // Create the vldN-dup node.
8880 EVT Tys[5];
8881 unsigned n;
8882 for (n = 0; n < NumVecs; ++n)
8883 Tys[n] = VT;
8884 Tys[n] = MVT::Other;
Craig Topperabb4ac72014-04-16 06:10:51 +00008885 SDVTList SDTys = DAG.getVTList(ArrayRef<EVT>(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00008886 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8887 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008888 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00008889 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00008890 VLDMemInt->getMemOperand());
8891
8892 // Update the uses.
8893 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8894 UI != UE; ++UI) {
8895 unsigned ResNo = UI.getUse().getResNo();
8896 // Ignore uses of the chain result.
8897 if (ResNo == NumVecs)
8898 continue;
8899 SDNode *User = *UI;
8900 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8901 }
8902
8903 // Now the vldN-lane intrinsic is dead except for its chain result.
8904 // Update uses of the chain.
8905 std::vector<SDValue> VLDDupResults;
8906 for (unsigned n = 0; n < NumVecs; ++n)
8907 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8908 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8909 DCI.CombineTo(VLD, VLDDupResults);
8910
8911 return true;
8912}
8913
Bob Wilson103a0dc2010-07-14 01:22:12 +00008914/// PerformVDUPLANECombine - Target-specific dag combine xforms for
8915/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00008916static SDValue PerformVDUPLANECombine(SDNode *N,
8917 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00008918 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00008919
Bob Wilson2d790df2010-11-28 06:51:26 +00008920 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
8921 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
8922 if (CombineVLDDUP(N, DCI))
8923 return SDValue(N, 0);
8924
8925 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
8926 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00008927 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00008928 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00008929 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00008930 return SDValue();
8931
8932 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
8933 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
8934 // The canonical VMOV for a zero vector uses a 32-bit element size.
8935 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8936 unsigned EltBits;
8937 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
8938 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00008939 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00008940 if (EltSize > VT.getVectorElementType().getSizeInBits())
8941 return SDValue();
8942
Andrew Trickef9de2a2013-05-25 02:42:55 +00008943 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00008944}
8945
Eric Christopher1b8b94192011-06-29 21:10:36 +00008946// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosierfa8d8932011-06-24 19:23:04 +00008947// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
8948static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
8949{
Chad Rosier6b610b32011-06-28 17:26:57 +00008950 integerPart cN;
8951 integerPart c0 = 0;
Chad Rosierfa8d8932011-06-24 19:23:04 +00008952 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
8953 I != E; I++) {
8954 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
8955 if (!C)
8956 return false;
8957
Eric Christopher1b8b94192011-06-29 21:10:36 +00008958 bool isExact;
Chad Rosierfa8d8932011-06-24 19:23:04 +00008959 APFloat APF = C->getValueAPF();
8960 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
8961 != APFloat::opOK || !isExact)
8962 return false;
8963
8964 c0 = (I == 0) ? cN : c0;
8965 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
8966 return false;
8967 }
8968 C = c0;
8969 return true;
8970}
8971
8972/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
8973/// can replace combinations of VMUL and VCVT (floating-point to integer)
8974/// when the VMUL has a constant operand that is a power of 2.
8975///
8976/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8977/// vmul.f32 d16, d17, d16
8978/// vcvt.s32.f32 d16, d16
8979/// becomes:
8980/// vcvt.s32.f32 d16, d16, #3
8981static SDValue PerformVCVTCombine(SDNode *N,
8982 TargetLowering::DAGCombinerInfo &DCI,
8983 const ARMSubtarget *Subtarget) {
8984 SelectionDAG &DAG = DCI.DAG;
8985 SDValue Op = N->getOperand(0);
8986
8987 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
8988 Op.getOpcode() != ISD::FMUL)
8989 return SDValue();
8990
8991 uint64_t C;
8992 SDValue N0 = Op->getOperand(0);
8993 SDValue ConstVec = Op->getOperand(1);
8994 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
8995
Eric Christopher1b8b94192011-06-29 21:10:36 +00008996 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosierfa8d8932011-06-24 19:23:04 +00008997 !isConstVecPow2(ConstVec, isSigned, C))
8998 return SDValue();
8999
Tim Northover7cbc2152013-06-28 15:29:25 +00009000 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
9001 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
9002 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9003 // These instructions only exist converting from f32 to i32. We can handle
9004 // smaller integers by generating an extra truncate, but larger ones would
9005 // be lossy.
9006 return SDValue();
9007 }
9008
Chad Rosierfa8d8932011-06-24 19:23:04 +00009009 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
9010 Intrinsic::arm_neon_vcvtfp2fxu;
Tim Northover7cbc2152013-06-28 15:29:25 +00009011 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9012 SDValue FixConv = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
9013 NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9014 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
9015 DAG.getConstant(Log2_64(C), MVT::i32));
9016
9017 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9018 FixConv = DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), FixConv);
9019
9020 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009021}
9022
9023/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
9024/// can replace combinations of VCVT (integer to floating-point) and VDIV
9025/// when the VDIV has a constant operand that is a power of 2.
9026///
9027/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9028/// vcvt.f32.s32 d16, d16
9029/// vdiv.f32 d16, d17, d16
9030/// becomes:
9031/// vcvt.f32.s32 d16, d16, #3
9032static SDValue PerformVDIVCombine(SDNode *N,
9033 TargetLowering::DAGCombinerInfo &DCI,
9034 const ARMSubtarget *Subtarget) {
9035 SelectionDAG &DAG = DCI.DAG;
9036 SDValue Op = N->getOperand(0);
9037 unsigned OpOpcode = Op.getNode()->getOpcode();
9038
9039 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
9040 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
9041 return SDValue();
9042
9043 uint64_t C;
9044 SDValue ConstVec = N->getOperand(1);
9045 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
9046
9047 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
9048 !isConstVecPow2(ConstVec, isSigned, C))
9049 return SDValue();
9050
Tim Northover7cbc2152013-06-28 15:29:25 +00009051 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
9052 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
9053 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9054 // These instructions only exist converting from i32 to f32. We can handle
9055 // smaller integers by generating an extra extend, but larger ones would
9056 // be lossy.
9057 return SDValue();
9058 }
9059
9060 SDValue ConvInput = Op.getOperand(0);
9061 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9062 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9063 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
9064 SDLoc(N), NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9065 ConvInput);
9066
Eric Christopher1b8b94192011-06-29 21:10:36 +00009067 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +00009068 Intrinsic::arm_neon_vcvtfxu2fp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009069 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
Chad Rosierfa8d8932011-06-24 19:23:04 +00009070 Op.getValueType(),
Eric Christopher1b8b94192011-06-29 21:10:36 +00009071 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Tim Northover7cbc2152013-06-28 15:29:25 +00009072 ConvInput, DAG.getConstant(Log2_64(C), MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +00009073}
9074
9075/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +00009076/// operand of a vector shift operation, where all the elements of the
9077/// build_vector must have the same constant integer value.
9078static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
9079 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +00009080 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00009081 Op = Op.getOperand(0);
9082 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
9083 APInt SplatBits, SplatUndef;
9084 unsigned SplatBitSize;
9085 bool HasAnyUndefs;
9086 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
9087 HasAnyUndefs, ElementBits) ||
9088 SplatBitSize > ElementBits)
9089 return false;
9090 Cnt = SplatBits.getSExtValue();
9091 return true;
9092}
9093
9094/// isVShiftLImm - Check if this is a valid build_vector for the immediate
9095/// operand of a vector shift left operation. That value must be in the range:
9096/// 0 <= Value < ElementBits for a left shift; or
9097/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009098static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009099 assert(VT.isVector() && "vector shift count is not a vector type");
9100 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9101 if (! getVShiftImm(Op, ElementBits, Cnt))
9102 return false;
9103 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
9104}
9105
9106/// isVShiftRImm - Check if this is a valid build_vector for the immediate
9107/// operand of a vector shift right operation. For a shift opcode, the value
9108/// is positive, but for an intrinsic the value count must be negative. The
9109/// absolute value must be in the range:
9110/// 1 <= |Value| <= ElementBits for a right shift; or
9111/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009112static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +00009113 int64_t &Cnt) {
9114 assert(VT.isVector() && "vector shift count is not a vector type");
9115 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9116 if (! getVShiftImm(Op, ElementBits, Cnt))
9117 return false;
9118 if (isIntrinsic)
9119 Cnt = -Cnt;
9120 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
9121}
9122
9123/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
9124static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
9125 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
9126 switch (IntNo) {
9127 default:
9128 // Don't do anything for most intrinsics.
9129 break;
9130
9131 // Vector shifts: check for immediate versions and lower them.
9132 // Note: This is done during DAG combining instead of DAG legalizing because
9133 // the build_vectors for 64-bit vector element shift counts are generally
9134 // not legal, and it is hard to see their values after they get legalized to
9135 // loads from a constant pool.
9136 case Intrinsic::arm_neon_vshifts:
9137 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +00009138 case Intrinsic::arm_neon_vrshifts:
9139 case Intrinsic::arm_neon_vrshiftu:
9140 case Intrinsic::arm_neon_vrshiftn:
9141 case Intrinsic::arm_neon_vqshifts:
9142 case Intrinsic::arm_neon_vqshiftu:
9143 case Intrinsic::arm_neon_vqshiftsu:
9144 case Intrinsic::arm_neon_vqshiftns:
9145 case Intrinsic::arm_neon_vqshiftnu:
9146 case Intrinsic::arm_neon_vqshiftnsu:
9147 case Intrinsic::arm_neon_vqrshiftns:
9148 case Intrinsic::arm_neon_vqrshiftnu:
9149 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009150 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009151 int64_t Cnt;
9152 unsigned VShiftOpc = 0;
9153
9154 switch (IntNo) {
9155 case Intrinsic::arm_neon_vshifts:
9156 case Intrinsic::arm_neon_vshiftu:
9157 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
9158 VShiftOpc = ARMISD::VSHL;
9159 break;
9160 }
9161 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
9162 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
9163 ARMISD::VSHRs : ARMISD::VSHRu);
9164 break;
9165 }
9166 return SDValue();
9167
Bob Wilson2e076c42009-06-22 23:27:02 +00009168 case Intrinsic::arm_neon_vrshifts:
9169 case Intrinsic::arm_neon_vrshiftu:
9170 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9171 break;
9172 return SDValue();
9173
9174 case Intrinsic::arm_neon_vqshifts:
9175 case Intrinsic::arm_neon_vqshiftu:
9176 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9177 break;
9178 return SDValue();
9179
9180 case Intrinsic::arm_neon_vqshiftsu:
9181 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9182 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +00009183 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009184
Bob Wilson2e076c42009-06-22 23:27:02 +00009185 case Intrinsic::arm_neon_vrshiftn:
9186 case Intrinsic::arm_neon_vqshiftns:
9187 case Intrinsic::arm_neon_vqshiftnu:
9188 case Intrinsic::arm_neon_vqshiftnsu:
9189 case Intrinsic::arm_neon_vqrshiftns:
9190 case Intrinsic::arm_neon_vqrshiftnu:
9191 case Intrinsic::arm_neon_vqrshiftnsu:
9192 // Narrowing shifts require an immediate right shift.
9193 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9194 break;
Jim Grosbach84511e12010-06-02 21:53:11 +00009195 llvm_unreachable("invalid shift count for narrowing vector shift "
9196 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009197
9198 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00009199 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00009200 }
9201
9202 switch (IntNo) {
9203 case Intrinsic::arm_neon_vshifts:
9204 case Intrinsic::arm_neon_vshiftu:
9205 // Opcode already set above.
9206 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00009207 case Intrinsic::arm_neon_vrshifts:
9208 VShiftOpc = ARMISD::VRSHRs; break;
9209 case Intrinsic::arm_neon_vrshiftu:
9210 VShiftOpc = ARMISD::VRSHRu; break;
9211 case Intrinsic::arm_neon_vrshiftn:
9212 VShiftOpc = ARMISD::VRSHRN; break;
9213 case Intrinsic::arm_neon_vqshifts:
9214 VShiftOpc = ARMISD::VQSHLs; break;
9215 case Intrinsic::arm_neon_vqshiftu:
9216 VShiftOpc = ARMISD::VQSHLu; break;
9217 case Intrinsic::arm_neon_vqshiftsu:
9218 VShiftOpc = ARMISD::VQSHLsu; break;
9219 case Intrinsic::arm_neon_vqshiftns:
9220 VShiftOpc = ARMISD::VQSHRNs; break;
9221 case Intrinsic::arm_neon_vqshiftnu:
9222 VShiftOpc = ARMISD::VQSHRNu; break;
9223 case Intrinsic::arm_neon_vqshiftnsu:
9224 VShiftOpc = ARMISD::VQSHRNsu; break;
9225 case Intrinsic::arm_neon_vqrshiftns:
9226 VShiftOpc = ARMISD::VQRSHRNs; break;
9227 case Intrinsic::arm_neon_vqrshiftnu:
9228 VShiftOpc = ARMISD::VQRSHRNu; break;
9229 case Intrinsic::arm_neon_vqrshiftnsu:
9230 VShiftOpc = ARMISD::VQRSHRNsu; break;
9231 }
9232
Andrew Trickef9de2a2013-05-25 02:42:55 +00009233 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009234 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009235 }
9236
9237 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009238 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009239 int64_t Cnt;
9240 unsigned VShiftOpc = 0;
9241
9242 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9243 VShiftOpc = ARMISD::VSLI;
9244 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9245 VShiftOpc = ARMISD::VSRI;
9246 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009247 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009248 }
9249
Andrew Trickef9de2a2013-05-25 02:42:55 +00009250 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +00009251 N->getOperand(1), N->getOperand(2),
Owen Anderson9f944592009-08-11 20:47:22 +00009252 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009253 }
9254
9255 case Intrinsic::arm_neon_vqrshifts:
9256 case Intrinsic::arm_neon_vqrshiftu:
9257 // No immediate versions of these to check for.
9258 break;
9259 }
9260
9261 return SDValue();
9262}
9263
9264/// PerformShiftCombine - Checks for immediate versions of vector shifts and
9265/// lowers them. As with the vector shift intrinsics, this is done during DAG
9266/// combining instead of DAG legalizing because the build_vectors for 64-bit
9267/// vector element shift counts are generally not legal, and it is hard to see
9268/// their values after they get legalized to loads from a constant pool.
9269static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9270 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009271 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +00009272 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9273 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9274 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9275 SDValue N1 = N->getOperand(1);
9276 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9277 SDValue N0 = N->getOperand(0);
9278 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9279 DAG.MaskedValueIsZero(N0.getOperand(0),
9280 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009281 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +00009282 }
9283 }
Bob Wilson2e076c42009-06-22 23:27:02 +00009284
9285 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +00009286 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9287 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +00009288 return SDValue();
9289
9290 assert(ST->hasNEON() && "unexpected vector shift");
9291 int64_t Cnt;
9292
9293 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009294 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009295
9296 case ISD::SHL:
9297 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009298 return DAG.getNode(ARMISD::VSHL, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009299 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009300 break;
9301
9302 case ISD::SRA:
9303 case ISD::SRL:
9304 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
9305 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
9306 ARMISD::VSHRs : ARMISD::VSHRu);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009307 return DAG.getNode(VShiftOpc, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009308 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009309 }
9310 }
9311 return SDValue();
9312}
9313
9314/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
9315/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
9316static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
9317 const ARMSubtarget *ST) {
9318 SDValue N0 = N->getOperand(0);
9319
9320 // Check for sign- and zero-extensions of vector extract operations of 8-
9321 // and 16-bit vector elements. NEON supports these directly. They are
9322 // handled during DAG combining because type legalization will promote them
9323 // to 32-bit types and it is messy to recognize the operations after that.
9324 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9325 SDValue Vec = N0.getOperand(0);
9326 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00009327 EVT VT = N->getValueType(0);
9328 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009329 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9330
Owen Anderson9f944592009-08-11 20:47:22 +00009331 if (VT == MVT::i32 &&
9332 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +00009333 TLI.isTypeLegal(Vec.getValueType()) &&
9334 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009335
9336 unsigned Opc = 0;
9337 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009338 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009339 case ISD::SIGN_EXTEND:
9340 Opc = ARMISD::VGETLANEs;
9341 break;
9342 case ISD::ZERO_EXTEND:
9343 case ISD::ANY_EXTEND:
9344 Opc = ARMISD::VGETLANEu;
9345 break;
9346 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009347 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +00009348 }
9349 }
9350
9351 return SDValue();
9352}
9353
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009354/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9355/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9356static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9357 const ARMSubtarget *ST) {
9358 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng55f0c6b2010-07-15 22:07:12 +00009359 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009360 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9361 // a NaN; only do the transformation when it matches that behavior.
9362
9363 // For now only do this when using NEON for FP operations; if using VFP, it
9364 // is not obvious that the benefit outweighs the cost of switching to the
9365 // NEON pipeline.
9366 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9367 N->getValueType(0) != MVT::f32)
9368 return SDValue();
9369
9370 SDValue CondLHS = N->getOperand(0);
9371 SDValue CondRHS = N->getOperand(1);
9372 SDValue LHS = N->getOperand(2);
9373 SDValue RHS = N->getOperand(3);
9374 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9375
9376 unsigned Opcode = 0;
9377 bool IsReversed;
Bob Wilsonba8ac742010-02-24 22:15:53 +00009378 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009379 IsReversed = false; // x CC y ? x : y
Bob Wilsonba8ac742010-02-24 22:15:53 +00009380 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009381 IsReversed = true ; // x CC y ? y : x
9382 } else {
9383 return SDValue();
9384 }
9385
Bob Wilsonba8ac742010-02-24 22:15:53 +00009386 bool IsUnordered;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009387 switch (CC) {
9388 default: break;
9389 case ISD::SETOLT:
9390 case ISD::SETOLE:
9391 case ISD::SETLT:
9392 case ISD::SETLE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009393 case ISD::SETULT:
9394 case ISD::SETULE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009395 // If LHS is NaN, an ordered comparison will be false and the result will
9396 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
9397 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9398 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
9399 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9400 break;
9401 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
9402 // will return -0, so vmin can only be used for unsafe math or if one of
9403 // the operands is known to be nonzero.
9404 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009405 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009406 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9407 break;
9408 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009409 break;
9410
9411 case ISD::SETOGT:
9412 case ISD::SETOGE:
9413 case ISD::SETGT:
9414 case ISD::SETGE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009415 case ISD::SETUGT:
9416 case ISD::SETUGE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009417 // If LHS is NaN, an ordered comparison will be false and the result will
9418 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
9419 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9420 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
9421 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9422 break;
9423 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
9424 // will return +0, so vmax can only be used for unsafe math or if one of
9425 // the operands is known to be nonzero.
9426 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009427 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009428 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9429 break;
9430 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009431 break;
9432 }
9433
9434 if (!Opcode)
9435 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009436 return DAG.getNode(Opcode, SDLoc(N), N->getValueType(0), LHS, RHS);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009437}
9438
Evan Chengf863e3f2011-07-13 00:42:17 +00009439/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
9440SDValue
9441ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
9442 SDValue Cmp = N->getOperand(4);
9443 if (Cmp.getOpcode() != ARMISD::CMPZ)
9444 // Only looking at EQ and NE cases.
9445 return SDValue();
9446
9447 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009448 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +00009449 SDValue LHS = Cmp.getOperand(0);
9450 SDValue RHS = Cmp.getOperand(1);
9451 SDValue FalseVal = N->getOperand(0);
9452 SDValue TrueVal = N->getOperand(1);
9453 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +00009454 ARMCC::CondCodes CC =
9455 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +00009456
9457 // Simplify
9458 // mov r1, r0
9459 // cmp r1, x
9460 // mov r0, y
9461 // moveq r0, x
9462 // to
9463 // cmp r0, x
9464 // movne r0, y
9465 //
9466 // mov r1, r0
9467 // cmp r1, x
9468 // mov r0, x
9469 // movne r0, y
9470 // to
9471 // cmp r0, x
9472 // movne r0, y
9473 /// FIXME: Turn this into a target neutral optimization?
9474 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +00009475 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +00009476 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
9477 N->getOperand(3), Cmp);
9478 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
9479 SDValue ARMcc;
9480 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
9481 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
9482 N->getOperand(3), NewCmp);
9483 }
9484
9485 if (Res.getNode()) {
9486 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00009487 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +00009488 // Capture demanded bits information that would be otherwise lost.
9489 if (KnownZero == 0xfffffffe)
9490 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9491 DAG.getValueType(MVT::i1));
9492 else if (KnownZero == 0xffffff00)
9493 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9494 DAG.getValueType(MVT::i8));
9495 else if (KnownZero == 0xffff0000)
9496 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9497 DAG.getValueType(MVT::i16));
9498 }
9499
9500 return Res;
9501}
9502
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009503SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +00009504 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009505 switch (N->getOpcode()) {
9506 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009507 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +00009508 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009509 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009510 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +00009511 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +00009512 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
9513 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +00009514 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00009515 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson22806742010-09-22 22:09:21 +00009516 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009517 case ISD::STORE: return PerformSTORECombine(N, DCI);
9518 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
9519 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +00009520 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +00009521 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +00009522 case ISD::FP_TO_SINT:
9523 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
9524 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009525 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00009526 case ISD::SHL:
9527 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009528 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00009529 case ISD::SIGN_EXTEND:
9530 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009531 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
9532 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +00009533 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson06fce872011-02-07 17:43:21 +00009534 case ARMISD::VLD2DUP:
9535 case ARMISD::VLD3DUP:
9536 case ARMISD::VLD4DUP:
9537 return CombineBaseUpdate(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009538 case ARMISD::BUILD_VECTOR:
9539 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +00009540 case ISD::INTRINSIC_VOID:
9541 case ISD::INTRINSIC_W_CHAIN:
9542 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9543 case Intrinsic::arm_neon_vld1:
9544 case Intrinsic::arm_neon_vld2:
9545 case Intrinsic::arm_neon_vld3:
9546 case Intrinsic::arm_neon_vld4:
9547 case Intrinsic::arm_neon_vld2lane:
9548 case Intrinsic::arm_neon_vld3lane:
9549 case Intrinsic::arm_neon_vld4lane:
9550 case Intrinsic::arm_neon_vst1:
9551 case Intrinsic::arm_neon_vst2:
9552 case Intrinsic::arm_neon_vst3:
9553 case Intrinsic::arm_neon_vst4:
9554 case Intrinsic::arm_neon_vst2lane:
9555 case Intrinsic::arm_neon_vst3lane:
9556 case Intrinsic::arm_neon_vst4lane:
9557 return CombineBaseUpdate(N, DCI);
9558 default: break;
9559 }
9560 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009561 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009562 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009563}
9564
Evan Chengd42641c2011-02-02 01:06:55 +00009565bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9566 EVT VT) const {
9567 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9568}
9569
Matt Arsenault25793a32014-02-05 23:15:53 +00009570bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT, unsigned,
9571 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009572 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +00009573 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009574
9575 switch (VT.getSimpleVT().SimpleTy) {
9576 default:
9577 return false;
9578 case MVT::i8:
9579 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009580 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009581 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +00009582 if (AllowsUnaligned) {
9583 if (Fast)
9584 *Fast = Subtarget->hasV7Ops();
9585 return true;
9586 }
9587 return false;
9588 }
Evan Chengeec6bc62012-08-15 17:44:53 +00009589 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009590 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009591 // For any little-endian targets with neon, we can support unaligned ld/st
9592 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +00009593 // A big-endian target may also explicitly support unaligned accesses
Evan Cheng79e2ca92012-12-10 23:21:26 +00009594 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) {
9595 if (Fast)
9596 *Fast = true;
9597 return true;
9598 }
9599 return false;
9600 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009601 }
9602}
9603
Lang Hames9929c422011-11-02 22:52:45 +00009604static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
9605 unsigned AlignCheck) {
9606 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
9607 (DstAlign == 0 || DstAlign % AlignCheck == 0));
9608}
9609
9610EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
9611 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00009612 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +00009613 bool MemcpyStrSrc,
9614 MachineFunction &MF) const {
9615 const Function *F = MF.getFunction();
9616
9617 // See if we can use NEON instructions for this...
Evan Cheng962711e2012-12-12 02:34:41 +00009618 if ((!IsMemset || ZeroMemset) &&
Evan Cheng79e2ca92012-12-10 23:21:26 +00009619 Subtarget->hasNEON() &&
Bill Wendling698e84f2012-12-30 10:32:01 +00009620 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
9621 Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009622 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +00009623 if (Size >= 16 &&
9624 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault25793a32014-02-05 23:15:53 +00009625 (allowsUnalignedMemoryAccesses(MVT::v2f64, 0, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009626 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +00009627 } else if (Size >= 8 &&
9628 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault25793a32014-02-05 23:15:53 +00009629 (allowsUnalignedMemoryAccesses(MVT::f64, 0, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009630 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +00009631 }
9632 }
9633
Lang Hamesb85fcd02011-11-08 18:56:23 +00009634 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +00009635 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +00009636 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +00009637 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +00009638 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +00009639
Lang Hames9929c422011-11-02 22:52:45 +00009640 // Let the target-independent logic figure it out.
9641 return MVT::Other;
9642}
9643
Evan Cheng9ec512d2012-12-06 19:13:27 +00009644bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
9645 if (Val.getOpcode() != ISD::LOAD)
9646 return false;
9647
9648 EVT VT1 = Val.getValueType();
9649 if (!VT1.isSimple() || !VT1.isInteger() ||
9650 !VT2.isSimple() || !VT2.isInteger())
9651 return false;
9652
9653 switch (VT1.getSimpleVT().SimpleTy) {
9654 default: break;
9655 case MVT::i1:
9656 case MVT::i8:
9657 case MVT::i16:
9658 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
9659 return true;
9660 }
9661
9662 return false;
9663}
9664
Tim Northovercc2e9032013-08-06 13:58:03 +00009665bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
9666 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
9667 return false;
9668
9669 if (!isTypeLegal(EVT::getEVT(Ty1)))
9670 return false;
9671
9672 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
9673
9674 // Assuming the caller doesn't have a zeroext or signext return parameter,
9675 // truncation all the way down to i1 is valid.
9676 return true;
9677}
9678
9679
Evan Chengdc49a8d2009-08-14 20:09:37 +00009680static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
9681 if (V < 0)
9682 return false;
9683
9684 unsigned Scale = 1;
9685 switch (VT.getSimpleVT().SimpleTy) {
9686 default: return false;
9687 case MVT::i1:
9688 case MVT::i8:
9689 // Scale == 1;
9690 break;
9691 case MVT::i16:
9692 // Scale == 2;
9693 Scale = 2;
9694 break;
9695 case MVT::i32:
9696 // Scale == 4;
9697 Scale = 4;
9698 break;
9699 }
9700
9701 if ((V & (Scale - 1)) != 0)
9702 return false;
9703 V /= Scale;
9704 return V == (V & ((1LL << 5) - 1));
9705}
9706
9707static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
9708 const ARMSubtarget *Subtarget) {
9709 bool isNeg = false;
9710 if (V < 0) {
9711 isNeg = true;
9712 V = - V;
9713 }
9714
9715 switch (VT.getSimpleVT().SimpleTy) {
9716 default: return false;
9717 case MVT::i1:
9718 case MVT::i8:
9719 case MVT::i16:
9720 case MVT::i32:
9721 // + imm12 or - imm8
9722 if (isNeg)
9723 return V == (V & ((1LL << 8) - 1));
9724 return V == (V & ((1LL << 12) - 1));
9725 case MVT::f32:
9726 case MVT::f64:
9727 // Same as ARM mode. FIXME: NEON?
9728 if (!Subtarget->hasVFP2())
9729 return false;
9730 if ((V & 3) != 0)
9731 return false;
9732 V >>= 2;
9733 return V == (V & ((1LL << 8) - 1));
9734 }
9735}
9736
Evan Cheng2150b922007-03-12 23:30:29 +00009737/// isLegalAddressImmediate - Return true if the integer value can be used
9738/// as the offset of the target addressing mode for load / store of the
9739/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009740static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009741 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +00009742 if (V == 0)
9743 return true;
9744
Evan Chengce5dfb62009-03-09 19:15:00 +00009745 if (!VT.isSimple())
9746 return false;
9747
Evan Chengdc49a8d2009-08-14 20:09:37 +00009748 if (Subtarget->isThumb1Only())
9749 return isLegalT1AddressImmediate(V, VT);
9750 else if (Subtarget->isThumb2())
9751 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +00009752
Evan Chengdc49a8d2009-08-14 20:09:37 +00009753 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +00009754 if (V < 0)
9755 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +00009756 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +00009757 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +00009758 case MVT::i1:
9759 case MVT::i8:
9760 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +00009761 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009762 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +00009763 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +00009764 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009765 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +00009766 case MVT::f32:
9767 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009768 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +00009769 return false;
Evan Chengbef131de2007-05-03 02:00:18 +00009770 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +00009771 return false;
9772 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009773 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +00009774 }
Evan Cheng10043e22007-01-19 07:51:42 +00009775}
9776
Evan Chengdc49a8d2009-08-14 20:09:37 +00009777bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
9778 EVT VT) const {
9779 int Scale = AM.Scale;
9780 if (Scale < 0)
9781 return false;
9782
9783 switch (VT.getSimpleVT().SimpleTy) {
9784 default: return false;
9785 case MVT::i1:
9786 case MVT::i8:
9787 case MVT::i16:
9788 case MVT::i32:
9789 if (Scale == 1)
9790 return true;
9791 // r + r << imm
9792 Scale = Scale & ~1;
9793 return Scale == 2 || Scale == 4 || Scale == 8;
9794 case MVT::i64:
9795 // r + r
9796 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
9797 return true;
9798 return false;
9799 case MVT::isVoid:
9800 // Note, we allow "void" uses (basically, uses that aren't loads or
9801 // stores), because arm allows folding a scale into many arithmetic
9802 // operations. This should be made more precise and revisited later.
9803
9804 // Allow r << imm, but the imm has to be a multiple of two.
9805 if (Scale & 1) return false;
9806 return isPowerOf2_32(Scale);
9807 }
9808}
9809
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009810/// isLegalAddressingMode - Return true if the addressing mode represented
9811/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson7117a912009-03-20 22:42:55 +00009812bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +00009813 Type *Ty) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009814 EVT VT = getValueType(Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +00009815 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +00009816 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009817
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009818 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +00009819 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009820 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009821
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009822 switch (AM.Scale) {
9823 case 0: // no scale reg, must be "r+i" or "r", or "i".
9824 break;
9825 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009826 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009827 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +00009828 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009829 default:
Chris Lattner502c3f42007-04-13 06:50:55 +00009830 // ARM doesn't support any R+R*scale+imm addr modes.
9831 if (AM.BaseOffs)
9832 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009833
Bob Wilson866c1742009-04-08 17:55:28 +00009834 if (!VT.isSimple())
9835 return false;
9836
Evan Chengdc49a8d2009-08-14 20:09:37 +00009837 if (Subtarget->isThumb2())
9838 return isLegalT2ScaledAddressingMode(AM, VT);
9839
Chris Lattner9b6d69e2007-04-10 03:48:29 +00009840 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +00009841 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009842 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +00009843 case MVT::i1:
9844 case MVT::i8:
9845 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +00009846 if (Scale < 0) Scale = -Scale;
9847 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009848 return true;
9849 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +00009850 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +00009851 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009852 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009853 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +00009854 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009855 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +00009856 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009857
Owen Anderson9f944592009-08-11 20:47:22 +00009858 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009859 // Note, we allow "void" uses (basically, uses that aren't loads or
9860 // stores), because arm allows folding a scale into many arithmetic
9861 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +00009862
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009863 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +00009864 if (Scale & 1) return false;
9865 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009866 }
Evan Cheng2150b922007-03-12 23:30:29 +00009867 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009868 return true;
Evan Cheng2150b922007-03-12 23:30:29 +00009869}
9870
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009871/// isLegalICmpImmediate - Return true if the specified immediate is legal
9872/// icmp immediate, that is the target has icmp instructions which can compare
9873/// a register against the immediate without having to materialize the
9874/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +00009875bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +00009876 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009877 if (!Subtarget->isThumb())
Chandler Carruth8a102c22012-04-06 20:10:52 +00009878 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009879 if (Subtarget->isThumb2())
Chandler Carruth8a102c22012-04-06 20:10:52 +00009880 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +00009881 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +00009882 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009883}
9884
Andrew Tricka22cdb72012-07-18 18:34:27 +00009885/// isLegalAddImmediate - Return true if the specified immediate is a legal add
9886/// *or sub* immediate, that is the target has add or sub instructions which can
9887/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +00009888/// immediate into a register.
9889bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +00009890 // Same encoding for add/sub, just flip the sign.
9891 int64_t AbsImm = llvm::abs64(Imm);
9892 if (!Subtarget->isThumb())
9893 return ARM_AM::getSOImmVal(AbsImm) != -1;
9894 if (Subtarget->isThumb2())
9895 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
9896 // Thumb1 only has 8-bit unsigned immediate.
9897 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +00009898}
9899
Owen Anderson53aa7a92009-08-10 22:56:29 +00009900static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +00009901 bool isSEXTLoad, SDValue &Base,
9902 SDValue &Offset, bool &isInc,
9903 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +00009904 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9905 return false;
9906
Owen Anderson9f944592009-08-11 20:47:22 +00009907 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +00009908 // AddressingMode 3
9909 Base = Ptr->getOperand(0);
9910 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00009911 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +00009912 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +00009913 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +00009914 isInc = false;
9915 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9916 return true;
9917 }
9918 }
9919 isInc = (Ptr->getOpcode() == ISD::ADD);
9920 Offset = Ptr->getOperand(1);
9921 return true;
Owen Anderson9f944592009-08-11 20:47:22 +00009922 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +00009923 // AddressingMode 2
9924 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00009925 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +00009926 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +00009927 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +00009928 isInc = false;
9929 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9930 Base = Ptr->getOperand(0);
9931 return true;
9932 }
9933 }
9934
9935 if (Ptr->getOpcode() == ISD::ADD) {
9936 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +00009937 ARM_AM::ShiftOpc ShOpcVal=
9938 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +00009939 if (ShOpcVal != ARM_AM::no_shift) {
9940 Base = Ptr->getOperand(1);
9941 Offset = Ptr->getOperand(0);
9942 } else {
9943 Base = Ptr->getOperand(0);
9944 Offset = Ptr->getOperand(1);
9945 }
9946 return true;
9947 }
9948
9949 isInc = (Ptr->getOpcode() == ISD::ADD);
9950 Base = Ptr->getOperand(0);
9951 Offset = Ptr->getOperand(1);
9952 return true;
9953 }
9954
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00009955 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +00009956 return false;
9957}
9958
Owen Anderson53aa7a92009-08-10 22:56:29 +00009959static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +00009960 bool isSEXTLoad, SDValue &Base,
9961 SDValue &Offset, bool &isInc,
9962 SelectionDAG &DAG) {
9963 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9964 return false;
9965
9966 Base = Ptr->getOperand(0);
9967 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
9968 int RHSC = (int)RHS->getZExtValue();
9969 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
9970 assert(Ptr->getOpcode() == ISD::ADD);
9971 isInc = false;
9972 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9973 return true;
9974 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
9975 isInc = Ptr->getOpcode() == ISD::ADD;
9976 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
9977 return true;
9978 }
9979 }
9980
9981 return false;
9982}
9983
Evan Cheng10043e22007-01-19 07:51:42 +00009984/// getPreIndexedAddressParts - returns true by value, base pointer and
9985/// offset pointer and addressing mode by reference if the node's address
9986/// can be legally represented as pre-indexed load / store address.
9987bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009988ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
9989 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +00009990 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +00009991 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +00009992 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +00009993 return false;
9994
Owen Anderson53aa7a92009-08-10 22:56:29 +00009995 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009996 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +00009997 bool isSEXTLoad = false;
9998 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
9999 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010000 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010001 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10002 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
10003 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010004 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010005 } else
10006 return false;
10007
10008 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010009 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010010 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010011 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
10012 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010013 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010014 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000010015 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000010016 if (!isLegal)
10017 return false;
10018
10019 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
10020 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010021}
10022
10023/// getPostIndexedAddressParts - returns true by value, base pointer and
10024/// offset pointer and addressing mode by reference if this node can be
10025/// combined with a load / store to form a post-indexed load / store.
10026bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010027 SDValue &Base,
10028 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010029 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010030 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010031 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010032 return false;
10033
Owen Anderson53aa7a92009-08-10 22:56:29 +000010034 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010035 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010036 bool isSEXTLoad = false;
10037 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010038 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010039 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010040 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10041 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010042 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010043 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010044 } else
10045 return false;
10046
10047 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010048 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010049 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010050 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000010051 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010052 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010053 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
10054 isInc, DAG);
10055 if (!isLegal)
10056 return false;
10057
Evan Chengf19384d2010-05-18 21:31:17 +000010058 if (Ptr != Base) {
10059 // Swap base ptr and offset to catch more post-index load / store when
10060 // it's legal. In Thumb2 mode, offset must be an immediate.
10061 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
10062 !Subtarget->isThumb2())
10063 std::swap(Base, Offset);
10064
10065 // Post-indexed load / store update the base pointer.
10066 if (Ptr != Base)
10067 return false;
10068 }
10069
Evan Cheng84c6cda2009-07-02 07:28:31 +000010070 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
10071 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010072}
10073
Jay Foada0653a32014-05-14 21:14:37 +000010074void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
10075 APInt &KnownZero,
10076 APInt &KnownOne,
10077 const SelectionDAG &DAG,
10078 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000010079 unsigned BitWidth = KnownOne.getBitWidth();
10080 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000010081 switch (Op.getOpcode()) {
10082 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000010083 case ARMISD::ADDC:
10084 case ARMISD::ADDE:
10085 case ARMISD::SUBC:
10086 case ARMISD::SUBE:
10087 // These nodes' second result is a boolean
10088 if (Op.getResNo() == 0)
10089 break;
10090 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
10091 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010092 case ARMISD::CMOV: {
10093 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000010094 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010095 if (KnownZero == 0 && KnownOne == 0) return;
10096
Dan Gohmanf990faf2008-02-13 00:35:47 +000010097 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000010098 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010099 KnownZero &= KnownZeroRHS;
10100 KnownOne &= KnownOneRHS;
10101 return;
10102 }
Tim Northover01b4aa92014-04-03 15:10:35 +000010103 case ISD::INTRINSIC_W_CHAIN: {
10104 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
10105 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
10106 switch (IntID) {
10107 default: return;
10108 case Intrinsic::arm_ldaex:
10109 case Intrinsic::arm_ldrex: {
10110 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
10111 unsigned MemBits = VT.getScalarType().getSizeInBits();
10112 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
10113 return;
10114 }
10115 }
10116 }
Evan Cheng10043e22007-01-19 07:51:42 +000010117 }
10118}
10119
10120//===----------------------------------------------------------------------===//
10121// ARM Inline Assembly Support
10122//===----------------------------------------------------------------------===//
10123
Evan Cheng078b0b02011-01-08 01:24:27 +000010124bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
10125 // Looking for "rev" which is V6+.
10126 if (!Subtarget->hasV6Ops())
10127 return false;
10128
10129 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10130 std::string AsmStr = IA->getAsmString();
10131 SmallVector<StringRef, 4> AsmPieces;
10132 SplitString(AsmStr, AsmPieces, ";\n");
10133
10134 switch (AsmPieces.size()) {
10135 default: return false;
10136 case 1:
10137 AsmStr = AsmPieces[0];
10138 AsmPieces.clear();
10139 SplitString(AsmStr, AsmPieces, " \t,");
10140
10141 // rev $0, $1
10142 if (AsmPieces.size() == 3 &&
10143 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
10144 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000010145 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000010146 if (Ty && Ty->getBitWidth() == 32)
10147 return IntrinsicLowering::LowerToByteSwap(CI);
10148 }
10149 break;
10150 }
10151
10152 return false;
10153}
10154
Evan Cheng10043e22007-01-19 07:51:42 +000010155/// getConstraintType - Given a constraint letter, return the type of
10156/// constraint it is for this target.
10157ARMTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +000010158ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
10159 if (Constraint.size() == 1) {
10160 switch (Constraint[0]) {
10161 default: break;
10162 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000010163 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000010164 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000010165 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000010166 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000010167 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000010168 // An address with a single base register. Due to the way we
10169 // currently handle addresses it is the same as an 'r' memory constraint.
10170 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000010171 }
Eric Christophere256cd02011-06-21 22:10:57 +000010172 } else if (Constraint.size() == 2) {
10173 switch (Constraint[0]) {
10174 default: break;
10175 // All 'U+' constraints are addresses.
10176 case 'U': return C_Memory;
10177 }
Evan Cheng10043e22007-01-19 07:51:42 +000010178 }
Chris Lattnerd6855142007-03-25 02:14:49 +000010179 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000010180}
10181
John Thompsone8360b72010-10-29 17:29:13 +000010182/// Examine constraint type and operand type and determine a weight value.
10183/// This object must already have been set up with the operand type
10184/// and the current alternative constraint selected.
10185TargetLowering::ConstraintWeight
10186ARMTargetLowering::getSingleConstraintMatchWeight(
10187 AsmOperandInfo &info, const char *constraint) const {
10188 ConstraintWeight weight = CW_Invalid;
10189 Value *CallOperandVal = info.CallOperandVal;
10190 // If we don't have a value, we can't do a match,
10191 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000010192 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000010193 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000010194 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000010195 // Look at the constraint type.
10196 switch (*constraint) {
10197 default:
10198 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10199 break;
10200 case 'l':
10201 if (type->isIntegerTy()) {
10202 if (Subtarget->isThumb())
10203 weight = CW_SpecificReg;
10204 else
10205 weight = CW_Register;
10206 }
10207 break;
10208 case 'w':
10209 if (type->isFloatingPointTy())
10210 weight = CW_Register;
10211 break;
10212 }
10213 return weight;
10214}
10215
Eric Christophercf2007c2011-06-30 23:50:52 +000010216typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
10217RCPair
Evan Cheng10043e22007-01-19 07:51:42 +000010218ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +000010219 MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000010220 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010221 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000010222 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000010223 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010224 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010225 return RCPair(0U, &ARM::tGPRRegClass);
10226 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000010227 case 'h': // High regs or no regs.
10228 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010229 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000010230 break;
Chris Lattner6223e832007-04-02 17:24:08 +000010231 case 'r':
Craig Topperc7242e02012-04-20 07:30:17 +000010232 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010233 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000010234 if (VT == MVT::Other)
10235 break;
Owen Anderson9f944592009-08-11 20:47:22 +000010236 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010237 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000010238 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010239 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000010240 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010241 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010242 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010243 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000010244 if (VT == MVT::Other)
10245 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010246 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010247 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010248 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010249 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010250 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010251 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010252 break;
Eric Christopherc011d312011-07-01 00:30:46 +000010253 case 't':
10254 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010255 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000010256 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010257 }
10258 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000010259 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000010260 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000010261
Evan Cheng10043e22007-01-19 07:51:42 +000010262 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
10263}
10264
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010265/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10266/// vector. If it is invalid, don't add anything to Ops.
10267void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000010268 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010269 std::vector<SDValue>&Ops,
10270 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000010271 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010272
Eric Christopherde9399b2011-06-02 23:16:42 +000010273 // Currently only support length 1 constraints.
10274 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000010275
Eric Christopherde9399b2011-06-02 23:16:42 +000010276 char ConstraintLetter = Constraint[0];
10277 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010278 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000010279 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010280 case 'I': case 'J': case 'K': case 'L':
10281 case 'M': case 'N': case 'O':
10282 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10283 if (!C)
10284 return;
10285
10286 int64_t CVal64 = C->getSExtValue();
10287 int CVal = (int) CVal64;
10288 // None of these constraints allow values larger than 32 bits. Check
10289 // that the value fits in an int.
10290 if (CVal != CVal64)
10291 return;
10292
Eric Christopherde9399b2011-06-02 23:16:42 +000010293 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000010294 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000010295 // Constant suitable for movw, must be between 0 and
10296 // 65535.
10297 if (Subtarget->hasV6T2Ops())
10298 if (CVal >= 0 && CVal <= 65535)
10299 break;
10300 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010301 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000010302 if (Subtarget->isThumb1Only()) {
10303 // This must be a constant between 0 and 255, for ADD
10304 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010305 if (CVal >= 0 && CVal <= 255)
10306 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010307 } else if (Subtarget->isThumb2()) {
10308 // A constant that can be used as an immediate value in a
10309 // data-processing instruction.
10310 if (ARM_AM::getT2SOImmVal(CVal) != -1)
10311 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010312 } else {
10313 // A constant that can be used as an immediate value in a
10314 // data-processing instruction.
10315 if (ARM_AM::getSOImmVal(CVal) != -1)
10316 break;
10317 }
10318 return;
10319
10320 case 'J':
David Goodwin22c2fba2009-07-08 23:10:31 +000010321 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010322 // This must be a constant between -255 and -1, for negated ADD
10323 // immediates. This can be used in GCC with an "n" modifier that
10324 // prints the negated value, for use with SUB instructions. It is
10325 // not useful otherwise but is implemented for compatibility.
10326 if (CVal >= -255 && CVal <= -1)
10327 break;
10328 } else {
10329 // This must be a constant between -4095 and 4095. It is not clear
10330 // what this constraint is intended for. Implemented for
10331 // compatibility with GCC.
10332 if (CVal >= -4095 && CVal <= 4095)
10333 break;
10334 }
10335 return;
10336
10337 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000010338 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010339 // A 32-bit value where only one byte has a nonzero value. Exclude
10340 // zero to match GCC. This constraint is used by GCC internally for
10341 // constants that can be loaded with a move/shift combination.
10342 // It is not useful otherwise but is implemented for compatibility.
10343 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
10344 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010345 } else if (Subtarget->isThumb2()) {
10346 // A constant whose bitwise inverse can be used as an immediate
10347 // value in a data-processing instruction. This can be used in GCC
10348 // with a "B" modifier that prints the inverted value, for use with
10349 // BIC and MVN instructions. It is not useful otherwise but is
10350 // implemented for compatibility.
10351 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
10352 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010353 } else {
10354 // A constant whose bitwise inverse can be used as an immediate
10355 // value in a data-processing instruction. This can be used in GCC
10356 // with a "B" modifier that prints the inverted value, for use with
10357 // BIC and MVN instructions. It is not useful otherwise but is
10358 // implemented for compatibility.
10359 if (ARM_AM::getSOImmVal(~CVal) != -1)
10360 break;
10361 }
10362 return;
10363
10364 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000010365 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010366 // This must be a constant between -7 and 7,
10367 // for 3-operand ADD/SUB immediate instructions.
10368 if (CVal >= -7 && CVal < 7)
10369 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010370 } else if (Subtarget->isThumb2()) {
10371 // A constant whose negation can be used as an immediate value in a
10372 // data-processing instruction. This can be used in GCC with an "n"
10373 // modifier that prints the negated value, for use with SUB
10374 // instructions. It is not useful otherwise but is implemented for
10375 // compatibility.
10376 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
10377 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010378 } else {
10379 // A constant whose negation can be used as an immediate value in a
10380 // data-processing instruction. This can be used in GCC with an "n"
10381 // modifier that prints the negated value, for use with SUB
10382 // instructions. It is not useful otherwise but is implemented for
10383 // compatibility.
10384 if (ARM_AM::getSOImmVal(-CVal) != -1)
10385 break;
10386 }
10387 return;
10388
10389 case 'M':
David Goodwin22c2fba2009-07-08 23:10:31 +000010390 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010391 // This must be a multiple of 4 between 0 and 1020, for
10392 // ADD sp + immediate.
10393 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
10394 break;
10395 } else {
10396 // A power of two or a constant between 0 and 32. This is used in
10397 // GCC for the shift amount on shifted register operands, but it is
10398 // useful in general for any shift amounts.
10399 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
10400 break;
10401 }
10402 return;
10403
10404 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000010405 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010406 // This must be a constant between 0 and 31, for shift amounts.
10407 if (CVal >= 0 && CVal <= 31)
10408 break;
10409 }
10410 return;
10411
10412 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000010413 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010414 // This must be a multiple of 4 between -508 and 508, for
10415 // ADD/SUB sp = sp + immediate.
10416 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
10417 break;
10418 }
10419 return;
10420 }
10421 Result = DAG.getTargetConstant(CVal, Op.getValueType());
10422 break;
10423 }
10424
10425 if (Result.getNode()) {
10426 Ops.push_back(Result);
10427 return;
10428 }
Dale Johannesence97d552010-06-25 21:55:36 +000010429 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010430}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010431
Renato Golin87610692013-07-16 09:32:17 +000010432SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
10433 assert(Subtarget->isTargetAEABI() && "Register-based DivRem lowering only");
10434 unsigned Opcode = Op->getOpcode();
10435 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
10436 "Invalid opcode for Div/Rem lowering");
10437 bool isSigned = (Opcode == ISD::SDIVREM);
10438 EVT VT = Op->getValueType(0);
10439 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
10440
10441 RTLIB::Libcall LC;
10442 switch (VT.getSimpleVT().SimpleTy) {
10443 default: llvm_unreachable("Unexpected request for libcall!");
10444 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
10445 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
10446 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
10447 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
10448 }
10449
10450 SDValue InChain = DAG.getEntryNode();
10451
10452 TargetLowering::ArgListTy Args;
10453 TargetLowering::ArgListEntry Entry;
10454 for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) {
10455 EVT ArgVT = Op->getOperand(i).getValueType();
10456 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
10457 Entry.Node = Op->getOperand(i);
10458 Entry.Ty = ArgTy;
10459 Entry.isSExt = isSigned;
10460 Entry.isZExt = !isSigned;
10461 Args.push_back(Entry);
10462 }
10463
10464 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
10465 getPointerTy());
10466
10467 Type *RetTy = (Type*)StructType::get(Ty, Ty, NULL);
10468
10469 SDLoc dl(Op);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010470 TargetLowering::CallLoweringInfo CLI(DAG);
10471 CLI.setDebugLoc(dl).setChain(InChain)
10472 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, &Args, 0)
10473 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000010474
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010475 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000010476 return CallInfo.first;
10477}
10478
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010479bool
10480ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
10481 // The ARM target isn't yet aware of offsets.
10482 return false;
10483}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010484
Jim Grosbach11013ed2010-07-16 23:05:05 +000010485bool ARM::isBitFieldInvertedMask(unsigned v) {
10486 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010487 return false;
10488
Jim Grosbach11013ed2010-07-16 23:05:05 +000010489 // there can be 1's on either or both "outsides", all the "inside"
10490 // bits must be 0's
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010491 unsigned TO = CountTrailingOnes_32(v);
10492 unsigned LO = CountLeadingOnes_32(v);
10493 v = (v >> TO) << TO;
10494 v = (v << LO) >> LO;
10495 return v == 0;
Jim Grosbach11013ed2010-07-16 23:05:05 +000010496}
10497
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010498/// isFPImmLegal - Returns true if the target can instruction select the
10499/// specified FP immediate natively. If false, the legalizer will
10500/// materialize the FP immediate as a load from a constant pool.
10501bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
10502 if (!Subtarget->hasVFP3())
10503 return false;
10504 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000010505 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010506 if (VT == MVT::f64)
Jim Grosbachefc761a2011-09-30 00:50:06 +000010507 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010508 return false;
10509}
Bob Wilson5549d492010-09-21 17:56:22 +000010510
Wesley Peck527da1b2010-11-23 03:31:01 +000010511/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000010512/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
10513/// specified in the intrinsic calls.
10514bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
10515 const CallInst &I,
10516 unsigned Intrinsic) const {
10517 switch (Intrinsic) {
10518 case Intrinsic::arm_neon_vld1:
10519 case Intrinsic::arm_neon_vld2:
10520 case Intrinsic::arm_neon_vld3:
10521 case Intrinsic::arm_neon_vld4:
10522 case Intrinsic::arm_neon_vld2lane:
10523 case Intrinsic::arm_neon_vld3lane:
10524 case Intrinsic::arm_neon_vld4lane: {
10525 Info.opc = ISD::INTRINSIC_W_CHAIN;
10526 // Conservatively set memVT to the entire set of vectors loaded.
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010527 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010528 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10529 Info.ptrVal = I.getArgOperand(0);
10530 Info.offset = 0;
10531 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10532 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10533 Info.vol = false; // volatile loads with NEON intrinsics not supported
10534 Info.readMem = true;
10535 Info.writeMem = false;
10536 return true;
10537 }
10538 case Intrinsic::arm_neon_vst1:
10539 case Intrinsic::arm_neon_vst2:
10540 case Intrinsic::arm_neon_vst3:
10541 case Intrinsic::arm_neon_vst4:
10542 case Intrinsic::arm_neon_vst2lane:
10543 case Intrinsic::arm_neon_vst3lane:
10544 case Intrinsic::arm_neon_vst4lane: {
10545 Info.opc = ISD::INTRINSIC_VOID;
10546 // Conservatively set memVT to the entire set of vectors stored.
10547 unsigned NumElts = 0;
10548 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000010549 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000010550 if (!ArgTy->isVectorTy())
10551 break;
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010552 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010553 }
10554 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10555 Info.ptrVal = I.getArgOperand(0);
10556 Info.offset = 0;
10557 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10558 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10559 Info.vol = false; // volatile stores with NEON intrinsics not supported
10560 Info.readMem = false;
10561 Info.writeMem = true;
10562 return true;
10563 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010564 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000010565 case Intrinsic::arm_ldrex: {
10566 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
10567 Info.opc = ISD::INTRINSIC_W_CHAIN;
10568 Info.memVT = MVT::getVT(PtrTy->getElementType());
10569 Info.ptrVal = I.getArgOperand(0);
10570 Info.offset = 0;
10571 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10572 Info.vol = true;
10573 Info.readMem = true;
10574 Info.writeMem = false;
10575 return true;
10576 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010577 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000010578 case Intrinsic::arm_strex: {
10579 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
10580 Info.opc = ISD::INTRINSIC_W_CHAIN;
10581 Info.memVT = MVT::getVT(PtrTy->getElementType());
10582 Info.ptrVal = I.getArgOperand(1);
10583 Info.offset = 0;
10584 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10585 Info.vol = true;
10586 Info.readMem = false;
10587 Info.writeMem = true;
10588 return true;
10589 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010590 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010591 case Intrinsic::arm_strexd: {
10592 Info.opc = ISD::INTRINSIC_W_CHAIN;
10593 Info.memVT = MVT::i64;
10594 Info.ptrVal = I.getArgOperand(2);
10595 Info.offset = 0;
10596 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000010597 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010598 Info.readMem = false;
10599 Info.writeMem = true;
10600 return true;
10601 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010602 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010603 case Intrinsic::arm_ldrexd: {
10604 Info.opc = ISD::INTRINSIC_W_CHAIN;
10605 Info.memVT = MVT::i64;
10606 Info.ptrVal = I.getArgOperand(0);
10607 Info.offset = 0;
10608 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000010609 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010610 Info.readMem = true;
10611 Info.writeMem = false;
10612 return true;
10613 }
Bob Wilson5549d492010-09-21 17:56:22 +000010614 default:
10615 break;
10616 }
10617
10618 return false;
10619}
Juergen Ributzka659ce002014-01-28 01:20:14 +000010620
10621/// \brief Returns true if it is beneficial to convert a load of a constant
10622/// to just the constant itself.
10623bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
10624 Type *Ty) const {
10625 assert(Ty->isIntegerTy());
10626
10627 unsigned Bits = Ty->getPrimitiveSizeInBits();
10628 if (Bits == 0 || Bits > 32)
10629 return false;
10630 return true;
10631}
Tim Northover037f26f22014-04-17 18:22:47 +000010632
10633bool ARMTargetLowering::shouldExpandAtomicInIR(Instruction *Inst) const {
10634 // Loads and stores less than 64-bits are already atomic; ones above that
10635 // are doomed anyway, so defer to the default libcall and blame the OS when
10636 // things go wrong:
10637 if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
10638 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() == 64;
10639 else if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
10640 return LI->getType()->getPrimitiveSizeInBits() == 64;
10641
10642 // For the real atomic operations, we have ldrex/strex up to 64 bits.
10643 return Inst->getType()->getPrimitiveSizeInBits() <= 64;
10644}
10645
10646Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
10647 AtomicOrdering Ord) const {
10648 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
10649 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
10650 bool IsAcquire =
10651 Ord == Acquire || Ord == AcquireRelease || Ord == SequentiallyConsistent;
10652
10653 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
10654 // intrinsic must return {i32, i32} and we have to recombine them into a
10655 // single i64 here.
10656 if (ValTy->getPrimitiveSizeInBits() == 64) {
10657 Intrinsic::ID Int =
10658 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
10659 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
10660
10661 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
10662 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
10663
10664 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
10665 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000010666 if (!Subtarget->isLittle())
10667 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000010668 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
10669 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
10670 return Builder.CreateOr(
10671 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
10672 }
10673
10674 Type *Tys[] = { Addr->getType() };
10675 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
10676 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
10677
10678 return Builder.CreateTruncOrBitCast(
10679 Builder.CreateCall(Ldrex, Addr),
10680 cast<PointerType>(Addr->getType())->getElementType());
10681}
10682
10683Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
10684 Value *Addr,
10685 AtomicOrdering Ord) const {
10686 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
10687 bool IsRelease =
10688 Ord == Release || Ord == AcquireRelease || Ord == SequentiallyConsistent;
10689
10690 // Since the intrinsics must have legal type, the i64 intrinsics take two
10691 // parameters: "i32, i32". We must marshal Val into the appropriate form
10692 // before the call.
10693 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
10694 Intrinsic::ID Int =
10695 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
10696 Function *Strex = Intrinsic::getDeclaration(M, Int);
10697 Type *Int32Ty = Type::getInt32Ty(M->getContext());
10698
10699 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
10700 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000010701 if (!Subtarget->isLittle())
10702 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000010703 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
10704 return Builder.CreateCall3(Strex, Lo, Hi, Addr);
10705 }
10706
10707 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
10708 Type *Tys[] = { Addr->getType() };
10709 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
10710
10711 return Builder.CreateCall2(
10712 Strex, Builder.CreateZExtOrBitCast(
10713 Val, Strex->getFunctionType()->getParamType(0)),
10714 Addr);
10715}
Oliver Stannardc24f2172014-05-09 14:01:47 +000010716
10717enum HABaseType {
10718 HA_UNKNOWN = 0,
10719 HA_FLOAT,
10720 HA_DOUBLE,
10721 HA_VECT64,
10722 HA_VECT128
10723};
10724
10725static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
10726 uint64_t &Members) {
10727 if (const StructType *ST = dyn_cast<StructType>(Ty)) {
10728 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
10729 uint64_t SubMembers = 0;
10730 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
10731 return false;
10732 Members += SubMembers;
10733 }
10734 } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
10735 uint64_t SubMembers = 0;
10736 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
10737 return false;
10738 Members += SubMembers * AT->getNumElements();
10739 } else if (Ty->isFloatTy()) {
10740 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
10741 return false;
10742 Members = 1;
10743 Base = HA_FLOAT;
10744 } else if (Ty->isDoubleTy()) {
10745 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
10746 return false;
10747 Members = 1;
10748 Base = HA_DOUBLE;
10749 } else if (const VectorType *VT = dyn_cast<VectorType>(Ty)) {
10750 Members = 1;
10751 switch (Base) {
10752 case HA_FLOAT:
10753 case HA_DOUBLE:
10754 return false;
10755 case HA_VECT64:
10756 return VT->getBitWidth() == 64;
10757 case HA_VECT128:
10758 return VT->getBitWidth() == 128;
10759 case HA_UNKNOWN:
10760 switch (VT->getBitWidth()) {
10761 case 64:
10762 Base = HA_VECT64;
10763 return true;
10764 case 128:
10765 Base = HA_VECT128;
10766 return true;
10767 default:
10768 return false;
10769 }
10770 }
10771 }
10772
10773 return (Members > 0 && Members <= 4);
10774}
10775
10776/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate.
10777bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
10778 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
10779 if (getEffectiveCallingConv(CallConv, isVarArg) ==
10780 CallingConv::ARM_AAPCS_VFP) {
10781 HABaseType Base = HA_UNKNOWN;
10782 uint64_t Members = 0;
10783 bool result = isHomogeneousAggregate(Ty, Base, Members);
10784 DEBUG(dbgs() << "isHA: " << result << " "; Ty->dump(); dbgs() << "\n");
10785 return result;
10786 } else {
10787 return false;
10788 }
10789}