blob: f9fc641b12d61c0620e22e1bc3dec98b89cc4132 [file] [log] [blame]
Evan Cheng10043e22007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Cheng10043e22007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Craig Topper188ed9d2012-03-17 07:33:42 +000015#include "ARMISelLowering.h"
Eric Christopher1c069172010-09-10 22:42:06 +000016#include "ARMCallingConv.h"
Evan Cheng10043e22007-01-19 07:51:42 +000017#include "ARMConstantPoolValue.h"
Evan Cheng10043e22007-01-19 07:51:42 +000018#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov9a232f42009-08-21 12:41:24 +000019#include "ARMPerfectShuffle.h"
Evan Cheng10043e22007-01-19 07:51:42 +000020#include "ARMSubtarget.h"
21#include "ARMTargetMachine.h"
Chris Lattner4e7dfaf2009-08-02 00:34:36 +000022#include "ARMTargetObjectFile.h"
Evan Chenga20cde32011-07-20 23:34:39 +000023#include "MCTargetDesc/ARMAddressingModes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/ADT/Statistic.h"
25#include "llvm/ADT/StringExtras.h"
Bob Wilsona4c22902009-04-17 19:07:39 +000026#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng078b0b02011-01-08 01:24:27 +000027#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Cheng10043e22007-01-19 07:51:42 +000028#include "llvm/CodeGen/MachineBasicBlock.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Bill Wendling202803e2011-10-05 00:02:33 +000032#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng10043e22007-01-19 07:51:42 +000034#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/CallingConv.h"
36#include "llvm/IR/Constants.h"
37#include "llvm/IR/Function.h"
38#include "llvm/IR/GlobalValue.h"
Tim Northover037f26f22014-04-17 18:22:47 +000039#include "llvm/IR/IRBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000040#include "llvm/IR/Instruction.h"
41#include "llvm/IR/Instructions.h"
42#include "llvm/IR/Intrinsics.h"
43#include "llvm/IR/Type.h"
Bill Wendling46ffefc2010-03-09 02:46:12 +000044#include "llvm/MC/MCSectionMachO.h"
Jim Grosbach32bb3622010-04-14 22:28:31 +000045#include "llvm/Support/CommandLine.h"
Oliver Stannardc24f2172014-05-09 14:01:47 +000046#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000047#include "llvm/Support/ErrorHandling.h"
Evan Cheng2150b922007-03-12 23:30:29 +000048#include "llvm/Support/MathExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000049#include "llvm/Target/TargetOptions.h"
David Peixottoc32e24a2013-10-17 19:49:22 +000050#include <utility>
Evan Cheng10043e22007-01-19 07:51:42 +000051using namespace llvm;
52
Chandler Carruth84e68b22014-04-22 02:41:26 +000053#define DEBUG_TYPE "arm-isel"
54
Dale Johannesend679ff72010-06-03 21:09:53 +000055STATISTIC(NumTailCalls, "Number of tail calls");
Evan Cheng68aec142011-01-19 02:16:49 +000056STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Manman Ren9f911162012-06-01 02:44:42 +000057STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
Dale Johannesend679ff72010-06-03 21:09:53 +000058
Eric Christopher347f4c32010-12-15 23:47:29 +000059cl::opt<bool>
Jim Grosbach32bb3622010-04-14 22:28:31 +000060EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng25f93642010-07-08 02:08:50 +000061 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbach32bb3622010-04-14 22:28:31 +000062 cl::init(false));
63
Evan Chengf128bdc2010-06-16 07:35:02 +000064static cl::opt<bool>
65ARMInterworking("arm-interworking", cl::Hidden,
66 cl::desc("Enable / disable ARM interworking (for debugging only)"),
67 cl::init(true));
68
Benjamin Kramer7ba71be2011-11-26 23:01:57 +000069namespace {
Cameron Zwarich89019782011-06-10 20:59:24 +000070 class ARMCCState : public CCState {
71 public:
72 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
Craig Topperb94011f2013-07-14 04:42:23 +000073 const TargetMachine &TM, SmallVectorImpl<CCValAssign> &locs,
Cameron Zwarich89019782011-06-10 20:59:24 +000074 LLVMContext &C, ParmContext PC)
75 : CCState(CC, isVarArg, MF, TM, locs, C) {
76 assert(((PC == Call) || (PC == Prologue)) &&
77 "ARMCCState users must specify whether their context is call"
78 "or prologue generation.");
79 CallOrPrologue = PC;
80 }
81 };
82}
83
Stuart Hastings45fe3c32011-04-20 16:47:52 +000084// The APCS parameter registers.
Craig Topper840beec2014-04-04 05:16:06 +000085static const MCPhysReg GPRArgRegs[] = {
Stuart Hastings45fe3c32011-04-20 16:47:52 +000086 ARM::R0, ARM::R1, ARM::R2, ARM::R3
87};
88
Craig Topper4fa625f2012-08-12 03:16:37 +000089void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT,
90 MVT PromotedBitwiseVT) {
Bob Wilson2e076c42009-06-22 23:27:02 +000091 if (VT != PromotedLdStVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +000092 setOperationAction(ISD::LOAD, VT, Promote);
93 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000094
Craig Topper4fa625f2012-08-12 03:16:37 +000095 setOperationAction(ISD::STORE, VT, Promote);
96 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000097 }
98
Craig Topper4fa625f2012-08-12 03:16:37 +000099 MVT ElemTy = VT.getVectorElementType();
Owen Anderson9f944592009-08-11 20:47:22 +0000100 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Craig Topper4fa625f2012-08-12 03:16:37 +0000101 setOperationAction(ISD::SETCC, VT, Custom);
102 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
103 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000104 if (ElemTy == MVT::i32) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000105 setOperationAction(ISD::SINT_TO_FP, VT, Custom);
106 setOperationAction(ISD::UINT_TO_FP, VT, Custom);
107 setOperationAction(ISD::FP_TO_SINT, VT, Custom);
108 setOperationAction(ISD::FP_TO_UINT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000109 } else {
Craig Topper4fa625f2012-08-12 03:16:37 +0000110 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
111 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
112 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
113 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Bob Wilson5d8cfb22009-09-16 20:20:44 +0000114 }
Craig Topper4fa625f2012-08-12 03:16:37 +0000115 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
116 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
117 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal);
118 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
119 setOperationAction(ISD::SELECT, VT, Expand);
120 setOperationAction(ISD::SELECT_CC, VT, Expand);
Jim Grosbach30af4422012-10-12 22:59:21 +0000121 setOperationAction(ISD::VSELECT, VT, Expand);
Craig Topper4fa625f2012-08-12 03:16:37 +0000122 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000123 if (VT.isInteger()) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000124 setOperationAction(ISD::SHL, VT, Custom);
125 setOperationAction(ISD::SRA, VT, Custom);
126 setOperationAction(ISD::SRL, VT, Custom);
Bob Wilson2e076c42009-06-22 23:27:02 +0000127 }
128
129 // Promote all bit-wise operations.
130 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000131 setOperationAction(ISD::AND, VT, Promote);
132 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
133 setOperationAction(ISD::OR, VT, Promote);
134 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT);
135 setOperationAction(ISD::XOR, VT, Promote);
136 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
Bob Wilson2e076c42009-06-22 23:27:02 +0000137 }
Bob Wilson4ed397c2009-09-16 00:17:28 +0000138
139 // Neon does not support vector divide/remainder operations.
Craig Topper4fa625f2012-08-12 03:16:37 +0000140 setOperationAction(ISD::SDIV, VT, Expand);
141 setOperationAction(ISD::UDIV, VT, Expand);
142 setOperationAction(ISD::FDIV, VT, Expand);
143 setOperationAction(ISD::SREM, VT, Expand);
144 setOperationAction(ISD::UREM, VT, Expand);
145 setOperationAction(ISD::FREM, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000146}
147
Craig Topper4fa625f2012-08-12 03:16:37 +0000148void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topperc7242e02012-04-20 07:30:17 +0000149 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000150 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000151}
152
Craig Topper4fa625f2012-08-12 03:16:37 +0000153void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Jakob Stoklund Olesen20912062014-01-14 06:18:34 +0000154 addRegisterClass(VT, &ARM::DPairRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000155 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000156}
157
Eric Christopher89958332014-05-31 00:07:32 +0000158static TargetLoweringObjectFile *createTLOF(const Triple &TT) {
159 if (TT.isOSBinFormatMachO())
Bill Wendlingbbcaa402010-03-15 21:09:38 +0000160 return new TargetLoweringObjectFileMachO();
Eric Christopher89958332014-05-31 00:07:32 +0000161 if (TT.isOSWindows())
Saleem Abdulrasool46fed302014-05-17 04:28:08 +0000162 return new TargetLoweringObjectFileCOFF();
Chris Lattner4e7dfaf2009-08-02 00:34:36 +0000163 return new ARMElfTargetObjectFile();
Chris Lattner5e693ed2009-07-28 03:13:23 +0000164}
165
Evan Cheng10043e22007-01-19 07:51:42 +0000166ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Eric Christopher89958332014-05-31 00:07:32 +0000167 : TargetLowering(TM, createTLOF(Triple(TM.getTargetTriple()))) {
Evan Cheng10043e22007-01-19 07:51:42 +0000168 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Chengdf907f42010-07-23 22:39:59 +0000169 RegInfo = TM.getRegisterInfo();
Evan Chengbf407072010-09-10 01:29:16 +0000170 Itins = TM.getInstrItineraryData();
Evan Cheng10043e22007-01-19 07:51:42 +0000171
Duncan Sandsf2641e12011-09-06 19:07:46 +0000172 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
173
Tim Northoverd6a729b2014-01-06 14:28:05 +0000174 if (Subtarget->isTargetMachO()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000175 // Uses VFP for Thumb libfuncs if available.
Jim Grosbach1d1d6d42013-10-24 23:07:11 +0000176 if (Subtarget->isThumb() && Subtarget->hasVFP2() &&
Tim Northover978d25f2014-04-22 10:10:09 +0000177 Subtarget->hasARMOps() && !TM.Options.UseSoftFloat) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000178 // Single-precision floating-point arithmetic.
179 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
180 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
181 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
182 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000183
Evan Chengc9f22fd12007-04-27 08:15:43 +0000184 // Double-precision floating-point arithmetic.
185 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
186 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
187 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
188 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng143576d2007-01-31 09:30:58 +0000189
Evan Chengc9f22fd12007-04-27 08:15:43 +0000190 // Single-precision comparisons.
191 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
192 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
193 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
194 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
195 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
196 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
197 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
198 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000199
Evan Chengc9f22fd12007-04-27 08:15:43 +0000200 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
201 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
202 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
203 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
204 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
205 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
206 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng143576d2007-01-31 09:30:58 +0000208
Evan Chengc9f22fd12007-04-27 08:15:43 +0000209 // Double-precision comparisons.
210 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
211 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
212 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
213 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
214 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
215 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
216 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
217 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000218
Evan Chengc9f22fd12007-04-27 08:15:43 +0000219 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
220 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
221 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
222 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
223 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
224 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
225 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
226 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Cheng10043e22007-01-19 07:51:42 +0000227
Evan Chengc9f22fd12007-04-27 08:15:43 +0000228 // Floating-point to integer conversions.
229 // i64 conversions are done via library routines even when generating VFP
230 // instructions, so use the same ones.
231 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
232 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
233 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
234 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000235
Evan Chengc9f22fd12007-04-27 08:15:43 +0000236 // Conversions between floating types.
237 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
238 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
239
240 // Integer to floating-point conversions.
241 // i64 conversions are done via library routines even when generating VFP
242 // instructions, so use the same ones.
Bob Wilsondc40d5a2009-03-20 23:16:43 +0000243 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
244 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengc9f22fd12007-04-27 08:15:43 +0000245 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
246 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
247 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
248 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
249 }
Evan Cheng10043e22007-01-19 07:51:42 +0000250 }
251
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000252 // These libcalls are not available in 32-bit.
Craig Topper062a2ba2014-04-25 05:30:21 +0000253 setLibcallName(RTLIB::SHL_I128, nullptr);
254 setLibcallName(RTLIB::SRL_I128, nullptr);
255 setLibcallName(RTLIB::SRA_I128, nullptr);
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000256
Saleem Abdulrasoolcd130822014-04-02 20:32:05 +0000257 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetMachO() &&
258 !Subtarget->isTargetWindows()) {
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000259 static const struct {
260 const RTLIB::Libcall Op;
261 const char * const Name;
262 const CallingConv::ID CC;
263 const ISD::CondCode Cond;
264 } LibraryCalls[] = {
265 // Double-precision floating-point arithmetic helper functions
266 // RTABI chapter 4.1.2, Table 2
267 { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
268 { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
269 { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
270 { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000271
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000272 // Double-precision floating-point comparison helper functions
273 // RTABI chapter 4.1.2, Table 3
274 { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
275 { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
276 { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
277 { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
278 { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
279 { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
280 { RTLIB::UO_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
281 { RTLIB::O_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000282
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000283 // Single-precision floating-point arithmetic helper functions
284 // RTABI chapter 4.1.2, Table 4
285 { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
286 { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
287 { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
288 { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000289
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000290 // Single-precision floating-point comparison helper functions
291 // RTABI chapter 4.1.2, Table 5
292 { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
293 { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
294 { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
295 { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
296 { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
297 { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
298 { RTLIB::UO_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
299 { RTLIB::O_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000300
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000301 // Floating-point to integer conversions.
302 // RTABI chapter 4.1.2, Table 6
303 { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
304 { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
305 { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
306 { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
307 { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
308 { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
309 { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
310 { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000311
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000312 // Conversions between floating types.
313 // RTABI chapter 4.1.2, Table 7
314 { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
315 { RTLIB::FPEXT_F32_F64, "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000316
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000317 // Integer to floating-point conversions.
318 // RTABI chapter 4.1.2, Table 8
319 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
320 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
321 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
322 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
323 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
324 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
325 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
326 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000327
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000328 // Long long helper functions
329 // RTABI chapter 4.2, Table 9
330 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
331 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
332 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
333 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000334
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000335 // Integer division functions
336 // RTABI chapter 4.3.1
337 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
338 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
339 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
340 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
341 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
342 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
343 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
344 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Renato Golin4cd51872011-05-22 21:41:23 +0000345
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000346 // Memory operations
347 // RTABI chapter 4.3.4
348 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
349 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
350 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
351 };
352
353 for (const auto &LC : LibraryCalls) {
354 setLibcallName(LC.Op, LC.Name);
355 setLibcallCallingConv(LC.Op, LC.CC);
356 if (LC.Cond != ISD::SETCC_INVALID)
357 setCmpLibcallCC(LC.Op, LC.Cond);
358 }
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +0000359 }
360
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000361 if (Subtarget->isTargetWindows()) {
362 static const struct {
363 const RTLIB::Libcall Op;
364 const char * const Name;
365 const CallingConv::ID CC;
366 } LibraryCalls[] = {
367 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP },
368 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP },
369 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP },
370 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP },
371 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP },
372 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP },
373 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP },
374 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP },
375 };
376
377 for (const auto &LC : LibraryCalls) {
378 setLibcallName(LC.Op, LC.Name);
379 setLibcallCallingConv(LC.Op, LC.CC);
380 }
381 }
382
Bob Wilsonbc158992011-10-07 16:59:21 +0000383 // Use divmod compiler-rt calls for iOS 5.0 and later.
Cameron Esfahani943908b2013-08-29 20:23:14 +0000384 if (Subtarget->getTargetTriple().isiOS() &&
Bob Wilsonbc158992011-10-07 16:59:21 +0000385 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
386 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
387 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
388 }
389
David Goodwin22c2fba2009-07-08 23:10:31 +0000390 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000391 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000392 else
Craig Topperc7242e02012-04-20 07:30:17 +0000393 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000394 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
395 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000396 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Jim Grosbach4d5dc3e2010-08-11 15:44:15 +0000397 if (!Subtarget->isFPOnlySP())
Craig Topperc7242e02012-04-20 07:30:17 +0000398 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Bob Wilson7117a912009-03-20 22:42:55 +0000399
Owen Anderson9f944592009-08-11 20:47:22 +0000400 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000401 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000402
Eli Friedman6f84fed2011-11-08 01:43:53 +0000403 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
404 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
405 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
406 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
407 setTruncStoreAction((MVT::SimpleValueType)VT,
408 (MVT::SimpleValueType)InnerVT, Expand);
409 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
410 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
411 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Benjamin Kramer4dae5982014-04-26 12:06:28 +0000412
413 setOperationAction(ISD::MULHS, (MVT::SimpleValueType)VT, Expand);
414 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
415 setOperationAction(ISD::MULHU, (MVT::SimpleValueType)VT, Expand);
416 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000417
418 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Eli Friedman6f84fed2011-11-08 01:43:53 +0000419 }
420
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000421 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000422 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000423
Bob Wilson2e076c42009-06-22 23:27:02 +0000424 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000425 addDRTypeForNEON(MVT::v2f32);
426 addDRTypeForNEON(MVT::v8i8);
427 addDRTypeForNEON(MVT::v4i16);
428 addDRTypeForNEON(MVT::v2i32);
429 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000430
Owen Anderson9f944592009-08-11 20:47:22 +0000431 addQRTypeForNEON(MVT::v4f32);
432 addQRTypeForNEON(MVT::v2f64);
433 addQRTypeForNEON(MVT::v16i8);
434 addQRTypeForNEON(MVT::v8i16);
435 addQRTypeForNEON(MVT::v4i32);
436 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000437
Bob Wilson194a2512009-09-15 23:55:57 +0000438 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
439 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000440 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
441 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000442 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
443 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
444 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000445 // FIXME: Code duplication: FDIV and FREM are expanded always, see
446 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000447 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
448 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000449 // FIXME: Create unittest.
450 // In another words, find a way when "copysign" appears in DAG with vector
451 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000452 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000453 // FIXME: Code duplication: SETCC has custom operation action, see
454 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000455 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000456 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000457 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
458 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
459 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
460 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
461 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
462 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
463 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
464 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
465 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
466 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
467 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
468 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000469 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000470 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
471 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
472 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
473 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
474 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000475 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000476
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000477 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
478 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
479 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
480 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
481 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
482 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
483 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
484 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
485 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
486 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000487 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
488 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
489 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
490 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000491 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000492
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000493 // Mark v2f32 intrinsics.
494 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
495 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
496 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
497 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
498 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
499 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
500 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
501 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
502 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
503 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
504 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
505 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
506 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
507 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
508 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
509
Bob Wilson6cc46572009-09-16 00:32:15 +0000510 // Neon does not support some operations on v1i64 and v2i64 types.
511 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000512 // Custom handling for some quad-vector types to detect VMULL.
513 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
514 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
515 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000516 // Custom handling for some vector types to avoid expensive expansions
517 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
518 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
519 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
520 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000521 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
522 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000523 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000524 // a destination type that is wider than the source, and nor does
525 // it have a FP_TO_[SU]INT instruction with a narrower destination than
526 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000527 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
528 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000529 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
530 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000531
Eli Friedmane6385e62012-11-15 22:44:27 +0000532 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000533 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000534
Evan Chengb4eae132012-12-04 22:41:50 +0000535 // NEON does not have single instruction CTPOP for vectors with element
536 // types wider than 8-bits. However, custom lowering can leverage the
537 // v8i8/v16i8 vcnt instruction.
538 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
539 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
540 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
541 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
542
Jim Grosbach5f215872013-02-27 21:31:12 +0000543 // NEON only has FMA instructions as of VFP4.
544 if (!Subtarget->hasVFP4()) {
545 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
546 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
547 }
548
Bob Wilson06fce872011-02-07 17:43:21 +0000549 setTargetDAGCombine(ISD::INTRINSIC_VOID);
550 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000551 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
552 setTargetDAGCombine(ISD::SHL);
553 setTargetDAGCombine(ISD::SRL);
554 setTargetDAGCombine(ISD::SRA);
555 setTargetDAGCombine(ISD::SIGN_EXTEND);
556 setTargetDAGCombine(ISD::ZERO_EXTEND);
557 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsonc6c13a32010-02-18 06:05:53 +0000558 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000559 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000560 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000561 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
562 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000563 setTargetDAGCombine(ISD::FP_TO_SINT);
564 setTargetDAGCombine(ISD::FP_TO_UINT);
565 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem097106b2011-10-15 20:03:12 +0000566
James Molloy547d4c02012-02-20 09:24:05 +0000567 // It is legal to extload from v4i8 to v4i16 or v4i32.
568 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
569 MVT::v4i16, MVT::v2i16,
570 MVT::v2i32};
571 for (unsigned i = 0; i < 6; ++i) {
572 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
573 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
574 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
575 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000576 }
577
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000578 // ARM and Thumb2 support UMLAL/SMLAL.
579 if (!Subtarget->isThumb1Only())
580 setTargetDAGCombine(ISD::ADDC);
581
582
Evan Cheng6addd652007-05-18 00:19:34 +0000583 computeRegisterProperties();
Evan Cheng10043e22007-01-19 07:51:42 +0000584
585 // ARM does not have f32 extending load.
Owen Anderson9f944592009-08-11 20:47:22 +0000586 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000587
Duncan Sands95d46ef2008-01-23 20:39:46 +0000588 // ARM does not have i1 sign extending load.
Owen Anderson9f944592009-08-11 20:47:22 +0000589 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000590
Evan Cheng10043e22007-01-19 07:51:42 +0000591 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000592 if (!Subtarget->isThumb1Only()) {
593 for (unsigned im = (unsigned)ISD::PRE_INC;
594 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000595 setIndexedLoadAction(im, MVT::i1, Legal);
596 setIndexedLoadAction(im, MVT::i8, Legal);
597 setIndexedLoadAction(im, MVT::i16, Legal);
598 setIndexedLoadAction(im, MVT::i32, Legal);
599 setIndexedStoreAction(im, MVT::i1, Legal);
600 setIndexedStoreAction(im, MVT::i8, Legal);
601 setIndexedStoreAction(im, MVT::i16, Legal);
602 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000603 }
Evan Cheng10043e22007-01-19 07:51:42 +0000604 }
605
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000606 setOperationAction(ISD::SADDO, MVT::i32, Custom);
607 setOperationAction(ISD::UADDO, MVT::i32, Custom);
608 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
609 setOperationAction(ISD::USUBO, MVT::i32, Custom);
610
Evan Cheng10043e22007-01-19 07:51:42 +0000611 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000612 setOperationAction(ISD::MUL, MVT::i64, Expand);
613 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000614 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000615 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
616 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000617 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000618 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
619 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000620 setOperationAction(ISD::MULHS, MVT::i32, Expand);
621
Jim Grosbach5d994042009-10-31 19:38:01 +0000622 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000623 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000624 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000625 setOperationAction(ISD::SRL, MVT::i64, Custom);
626 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000627
Evan Chenge8916542011-08-30 01:34:54 +0000628 if (!Subtarget->isThumb1Only()) {
629 // FIXME: We should do this for Thumb1 as well.
630 setOperationAction(ISD::ADDC, MVT::i32, Custom);
631 setOperationAction(ISD::ADDE, MVT::i32, Custom);
632 setOperationAction(ISD::SUBC, MVT::i32, Custom);
633 setOperationAction(ISD::SUBE, MVT::i32, Custom);
634 }
635
Evan Cheng10043e22007-01-19 07:51:42 +0000636 // ARM does not have ROTL.
Owen Anderson9f944592009-08-11 20:47:22 +0000637 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach8546ec92010-01-18 19:58:49 +0000638 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000639 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000640 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000641 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000642
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000643 // These just redirect to CTTZ and CTLZ on ARM.
644 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
645 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
646
Tim Northoverbc933082013-05-23 19:11:20 +0000647 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
648
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000649 // Only ARMv6 has BSWAP.
650 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000651 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000652
Bob Wilsone8a549c2012-09-29 21:43:49 +0000653 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
654 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
655 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbach92d999002010-05-05 20:44:35 +0000656 setOperationAction(ISD::SDIV, MVT::i32, Expand);
657 setOperationAction(ISD::UDIV, MVT::i32, Expand);
658 }
Renato Golin87610692013-07-16 09:32:17 +0000659
660 // FIXME: Also set divmod for SREM on EABI
Owen Anderson9f944592009-08-11 20:47:22 +0000661 setOperationAction(ISD::SREM, MVT::i32, Expand);
662 setOperationAction(ISD::UREM, MVT::i32, Expand);
Renato Golin87610692013-07-16 09:32:17 +0000663 // Register based DivRem for AEABI (RTABI 4.2)
664 if (Subtarget->isTargetAEABI()) {
665 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
666 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
667 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
668 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
669 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
670 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
671 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
672 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
673
674 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
675 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
676 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
677 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
678 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
679 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
680 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
681 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
682
683 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
684 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
685 } else {
686 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
687 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
688 }
Bob Wilson7117a912009-03-20 22:42:55 +0000689
Owen Anderson9f944592009-08-11 20:47:22 +0000690 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
691 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
692 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
693 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000694 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000695
Evan Cheng74d92c12011-04-08 21:37:21 +0000696 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000697
Evan Cheng10043e22007-01-19 07:51:42 +0000698 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000699 setOperationAction(ISD::VASTART, MVT::Other, Custom);
700 setOperationAction(ISD::VAARG, MVT::Other, Expand);
701 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
702 setOperationAction(ISD::VAEND, MVT::Other, Expand);
703 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
704 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000705
Tim Northoverd6a729b2014-01-06 14:28:05 +0000706 if (!Subtarget->isTargetMachO()) {
707 // Non-MachO platforms may return values in these registers via the
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000708 // personality function.
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000709 setExceptionPointerRegister(ARM::R0);
710 setExceptionSelectorRegister(ARM::R1);
711 }
Anton Korobeynikovf3a62312011-01-24 22:38:45 +0000712
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +0000713 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
714 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
715 else
716 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
717
Evan Cheng6e809de2010-08-11 06:22:01 +0000718 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
719 // the default expansion.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000720 if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000721 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
722 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000723 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000724
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000725 // On v8, we have particularly efficient implementations of atomic fences
726 // if they can be combined with nearby atomic loads and stores.
727 if (!Subtarget->hasV8Ops()) {
728 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
729 setInsertFencesForAtomic(true);
730 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000731 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000732 // If there's anything we can use as a barrier, go through custom lowering
733 // for ATOMIC_FENCE.
734 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
735 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
736
Jim Grosbach6860bb72010-06-18 22:35:32 +0000737 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000738 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000739 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000740 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000741 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000742 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000743 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000744 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000745 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000746 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000747 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000748 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000749 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000750 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
751 // Unordered/Monotonic case.
752 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
753 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000754 }
Evan Cheng10043e22007-01-19 07:51:42 +0000755
Evan Cheng21acf9f2010-11-04 05:19:35 +0000756 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000757
Eli Friedman8cfa7712010-06-26 04:36:50 +0000758 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
759 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000760 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
761 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000762 }
Owen Anderson9f944592009-08-11 20:47:22 +0000763 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000764
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000765 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
766 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000767 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000768 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000769 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000770 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
771 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000772
773 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000774 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbach31984832010-07-07 00:07:57 +0000775 if (Subtarget->isTargetDarwin()) {
776 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
777 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall7d84ece2011-05-29 19:50:32 +0000778 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbach31984832010-07-07 00:07:57 +0000779 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000780
Owen Anderson9f944592009-08-11 20:47:22 +0000781 setOperationAction(ISD::SETCC, MVT::i32, Expand);
782 setOperationAction(ISD::SETCC, MVT::f32, Expand);
783 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000784 setOperationAction(ISD::SELECT, MVT::i32, Custom);
785 setOperationAction(ISD::SELECT, MVT::f32, Custom);
786 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000787 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
788 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
789 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000790
Owen Anderson9f944592009-08-11 20:47:22 +0000791 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
792 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
793 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
794 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
795 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000796
Dan Gohman482732a2007-10-11 23:21:31 +0000797 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000798 setOperationAction(ISD::FSIN, MVT::f64, Expand);
799 setOperationAction(ISD::FSIN, MVT::f32, Expand);
800 setOperationAction(ISD::FCOS, MVT::f32, Expand);
801 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000802 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
803 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000804 setOperationAction(ISD::FREM, MVT::f64, Expand);
805 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000806 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
807 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000808 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
809 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000810 }
Owen Anderson9f944592009-08-11 20:47:22 +0000811 setOperationAction(ISD::FPOW, MVT::f64, Expand);
812 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000813
Evan Chengd0007f32012-04-10 21:40:28 +0000814 if (!Subtarget->hasVFP4()) {
815 setOperationAction(ISD::FMA, MVT::f64, Expand);
816 setOperationAction(ISD::FMA, MVT::f32, Expand);
817 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000818
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000819 // Various VFP goodness
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000820 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilsone4191e72010-03-19 22:51:32 +0000821 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
822 if (Subtarget->hasVFP2()) {
823 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
824 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
825 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
826 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
827 }
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000828 // Special handling for half-precision FP.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000829 if (!Subtarget->hasFP16()) {
830 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
831 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000832 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000833 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000834
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000835 // Combine sin / cos into one node or libcall if possible.
836 if (Subtarget->hasSinCos()) {
837 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
838 setLibcallName(RTLIB::SINCOS_F64, "sincos");
839 if (Subtarget->getTargetTriple().getOS() == Triple::IOS) {
840 // For iOS, we don't want to the normal expansion of a libcall to
841 // sincos. We want to issue a libcall to __sincos_stret.
842 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
843 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
844 }
845 }
Evan Cheng10043e22007-01-19 07:51:42 +0000846
Chris Lattnerf3f4ad92007-11-27 22:36:16 +0000847 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000848 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +0000849 setTargetDAGCombine(ISD::ADD);
850 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +0000851 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +0000852 setTargetDAGCombine(ISD::AND);
853 setTargetDAGCombine(ISD::OR);
854 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +0000855
Evan Chengf258a152012-02-23 02:58:19 +0000856 if (Subtarget->hasV6Ops())
857 setTargetDAGCombine(ISD::SRL);
858
Evan Cheng10043e22007-01-19 07:51:42 +0000859 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +0000860
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000861 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
862 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +0000863 setSchedulingPreference(Sched::RegPressure);
864 else
865 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +0000866
Evan Cheng3ae2b792011-01-06 06:52:41 +0000867 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000868 MaxStoresPerMemset = 8;
869 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
870 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
871 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
872 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
873 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
Evan Chengb71233f2010-06-26 01:52:05 +0000874
Rafael Espindolaa76eccf2010-07-11 04:01:49 +0000875 // On ARM arguments smaller than 4 bytes are extended, so all arguments
876 // are at least 4 bytes aligned.
877 setMinStackArgumentAlignment(4);
878
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000879 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000880 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000881
Eli Friedman2518f832011-05-06 20:34:06 +0000882 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +0000883}
884
Andrew Trick43f25632011-01-19 02:35:27 +0000885// FIXME: It might make sense to define the representative register class as the
886// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
887// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
888// SPR's representative would be DPR_VFP2. This should work well if register
889// pressure tracking were modified such that a register use would increment the
890// pressure of the register class's representative and all of it's super
891// classes' representatives transitively. We have not implemented this because
892// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000893// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +0000894// and extractions.
Evan Chenga77f3d32010-07-21 06:09:07 +0000895std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000896ARMTargetLowering::findRepresentativeClass(MVT VT) const{
Craig Topper062a2ba2014-04-25 05:30:21 +0000897 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +0000898 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000899 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +0000900 default:
Evan Chenga77f3d32010-07-21 06:09:07 +0000901 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng28590382010-07-21 23:53:58 +0000902 // Use DPR as representative register class for all floating point
903 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
904 // the cost is 1 for both f32 and f64.
905 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +0000906 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +0000907 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +0000908 // When NEON is used for SP, only half of the register file is available
909 // because operations that define both SP and DP results will be constrained
910 // to the VFP2 class (D0-D15). We currently model this constraint prior to
911 // coalescing by double-counting the SP regs. See the FIXME above.
912 if (Subtarget->useNEONForSinglePrecisionFP())
913 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000914 break;
915 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
916 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +0000917 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000918 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000919 break;
920 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +0000921 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000922 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +0000923 break;
924 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +0000925 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000926 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +0000927 break;
Evan Cheng10f99a32010-07-19 22:15:08 +0000928 }
Evan Chenga77f3d32010-07-21 06:09:07 +0000929 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +0000930}
931
Evan Cheng10043e22007-01-19 07:51:42 +0000932const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
933 switch (Opcode) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000934 default: return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +0000935 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +0000936 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +0000937 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
938 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +0000939 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +0000940 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
941 case ARMISD::tCALL: return "ARMISD::tCALL";
942 case ARMISD::BRCOND: return "ARMISD::BRCOND";
943 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +0000944 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +0000945 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +0000946 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +0000947 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
948 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +0000949 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +0000950 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +0000951 case ARMISD::CMPFP: return "ARMISD::CMPFP";
952 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +0000953 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +0000954 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +0000955
Evan Cheng10043e22007-01-19 07:51:42 +0000956 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +0000957
Jim Grosbach8546ec92010-01-18 19:58:49 +0000958 case ARMISD::RBIT: return "ARMISD::RBIT";
959
Bob Wilsone4191e72010-03-19 22:51:32 +0000960 case ARMISD::FTOSI: return "ARMISD::FTOSI";
961 case ARMISD::FTOUI: return "ARMISD::FTOUI";
962 case ARMISD::SITOF: return "ARMISD::SITOF";
963 case ARMISD::UITOF: return "ARMISD::UITOF";
964
Evan Cheng10043e22007-01-19 07:51:42 +0000965 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
966 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
967 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +0000968
Evan Chenge8916542011-08-30 01:34:54 +0000969 case ARMISD::ADDC: return "ARMISD::ADDC";
970 case ARMISD::ADDE: return "ARMISD::ADDE";
971 case ARMISD::SUBC: return "ARMISD::SUBC";
972 case ARMISD::SUBE: return "ARMISD::SUBE";
973
Bob Wilson22806742010-09-22 22:09:21 +0000974 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
975 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +0000976
Evan Chengec6d7c92009-10-28 06:55:03 +0000977 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
978 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
979
Dale Johannesend679ff72010-06-03 21:09:53 +0000980 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +0000981
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +0000982 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +0000983
Evan Chengb972e562009-08-07 00:34:42 +0000984 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
985
Bob Wilson7ed59712010-10-30 00:54:37 +0000986 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +0000987
Evan Cheng8740ee32010-11-03 06:34:55 +0000988 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
989
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +0000990 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK";
991
Bob Wilson2e076c42009-06-22 23:27:02 +0000992 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +0000993 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +0000994 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +0000995 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
996 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +0000997 case ARMISD::VCGEU: return "ARMISD::VCGEU";
998 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +0000999 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1000 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001001 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1002 case ARMISD::VTST: return "ARMISD::VTST";
1003
1004 case ARMISD::VSHL: return "ARMISD::VSHL";
1005 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1006 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001007 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1008 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1009 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1010 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1011 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1012 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1013 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1014 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1015 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1016 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1017 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1018 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
1019 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1020 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001021 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001022 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001023 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001024 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001025 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001026 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001027 case ARMISD::VREV64: return "ARMISD::VREV64";
1028 case ARMISD::VREV32: return "ARMISD::VREV32";
1029 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001030 case ARMISD::VZIP: return "ARMISD::VZIP";
1031 case ARMISD::VUZP: return "ARMISD::VUZP";
1032 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001033 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1034 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001035 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1036 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001037 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1038 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001039 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilsonc6c13a32010-02-18 06:05:53 +00001040 case ARMISD::FMAX: return "ARMISD::FMAX";
1041 case ARMISD::FMIN: return "ARMISD::FMIN";
Joey Goulye3dd6842013-08-23 12:01:13 +00001042 case ARMISD::VMAXNM: return "ARMISD::VMAX";
1043 case ARMISD::VMINNM: return "ARMISD::VMIN";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001044 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001045 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1046 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001047 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilson2d790df2010-11-28 06:51:26 +00001048 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1049 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1050 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001051 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1052 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1053 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1054 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1055 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1056 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1057 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1058 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1059 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1060 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1061 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1062 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1063 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1064 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1065 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1066 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1067 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001068 }
1069}
1070
Matt Arsenault758659232013-05-18 00:21:46 +00001071EVT ARMTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Duncan Sandsf2641e12011-09-06 19:07:46 +00001072 if (!VT.isVector()) return getPointerTy();
1073 return VT.changeVectorElementTypeToInteger();
1074}
1075
Evan Cheng4cad68e2010-05-15 02:18:07 +00001076/// getRegClassFor - Return the register class that should be used for the
1077/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001078const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001079 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1080 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1081 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001082 if (Subtarget->hasNEON()) {
1083 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001084 return &ARM::QQPRRegClass;
1085 if (VT == MVT::v8i64)
1086 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001087 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001088 return TargetLowering::getRegClassFor(VT);
1089}
1090
Eric Christopher84bdfd82010-07-21 22:26:11 +00001091// Create a fast isel object.
1092FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001093ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1094 const TargetLibraryInfo *libInfo) const {
1095 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001096}
1097
Anton Korobeynikov19edda02010-07-24 21:52:08 +00001098/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1099/// be used for loads / stores from the global.
1100unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1101 return (Subtarget->isThumb1Only() ? 127 : 4095);
1102}
1103
Evan Cheng4401f882010-05-20 23:26:43 +00001104Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001105 unsigned NumVals = N->getNumValues();
1106 if (!NumVals)
1107 return Sched::RegPressure;
1108
1109 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001110 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001111 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001112 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001113 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001114 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001115 }
Evan Chengbf914992010-05-28 23:25:23 +00001116
1117 if (!N->isMachineOpcode())
1118 return Sched::RegPressure;
1119
1120 // Load are scheduled for latency even if there instruction itinerary
1121 // is not available.
1122 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001123 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001124
Evan Cheng6cc775f2011-06-28 19:10:37 +00001125 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001126 return Sched::RegPressure;
1127 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001128 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001129 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001130
Evan Cheng4401f882010-05-20 23:26:43 +00001131 return Sched::RegPressure;
1132}
1133
Evan Cheng10043e22007-01-19 07:51:42 +00001134//===----------------------------------------------------------------------===//
1135// Lowering Code
1136//===----------------------------------------------------------------------===//
1137
Evan Cheng10043e22007-01-19 07:51:42 +00001138/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1139static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1140 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001141 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001142 case ISD::SETNE: return ARMCC::NE;
1143 case ISD::SETEQ: return ARMCC::EQ;
1144 case ISD::SETGT: return ARMCC::GT;
1145 case ISD::SETGE: return ARMCC::GE;
1146 case ISD::SETLT: return ARMCC::LT;
1147 case ISD::SETLE: return ARMCC::LE;
1148 case ISD::SETUGT: return ARMCC::HI;
1149 case ISD::SETUGE: return ARMCC::HS;
1150 case ISD::SETULT: return ARMCC::LO;
1151 case ISD::SETULE: return ARMCC::LS;
1152 }
1153}
1154
Bob Wilsona2e83332009-09-09 23:14:54 +00001155/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1156static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001157 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001158 CondCode2 = ARMCC::AL;
1159 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001160 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001161 case ISD::SETEQ:
1162 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1163 case ISD::SETGT:
1164 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1165 case ISD::SETGE:
1166 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1167 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001168 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001169 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1170 case ISD::SETO: CondCode = ARMCC::VC; break;
1171 case ISD::SETUO: CondCode = ARMCC::VS; break;
1172 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1173 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1174 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1175 case ISD::SETLT:
1176 case ISD::SETULT: CondCode = ARMCC::LT; break;
1177 case ISD::SETLE:
1178 case ISD::SETULE: CondCode = ARMCC::LE; break;
1179 case ISD::SETNE:
1180 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1181 }
Evan Cheng10043e22007-01-19 07:51:42 +00001182}
1183
Bob Wilsona4c22902009-04-17 19:07:39 +00001184//===----------------------------------------------------------------------===//
1185// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001186//===----------------------------------------------------------------------===//
1187
1188#include "ARMGenCallingConv.inc"
1189
Oliver Stannardc24f2172014-05-09 14:01:47 +00001190/// getEffectiveCallingConv - Get the effective calling convention, taking into
1191/// account presence of floating point hardware and calling convention
1192/// limitations, such as support for variadic functions.
1193CallingConv::ID
1194ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1195 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001196 switch (CC) {
1197 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001198 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001199 case CallingConv::ARM_AAPCS:
1200 case CallingConv::ARM_APCS:
1201 case CallingConv::GHC:
1202 return CC;
1203 case CallingConv::ARM_AAPCS_VFP:
1204 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1205 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001206 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001207 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001208 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001209 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1210 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001211 return CallingConv::ARM_AAPCS_VFP;
1212 else
1213 return CallingConv::ARM_AAPCS;
1214 case CallingConv::Fast:
1215 if (!Subtarget->isAAPCS_ABI()) {
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001216 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001217 return CallingConv::Fast;
1218 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001219 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001220 return CallingConv::ARM_AAPCS_VFP;
1221 else
1222 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001223 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001224}
1225
1226/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1227/// CallingConvention.
1228CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1229 bool Return,
1230 bool isVarArg) const {
1231 switch (getEffectiveCallingConv(CC, isVarArg)) {
1232 default:
1233 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001234 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001235 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001236 case CallingConv::ARM_AAPCS:
1237 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1238 case CallingConv::ARM_AAPCS_VFP:
1239 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1240 case CallingConv::Fast:
1241 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001242 case CallingConv::GHC:
1243 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001244 }
1245}
1246
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001247/// LowerCallResult - Lower the result values of a call into the
1248/// appropriate copies out of appropriate physical registers.
1249SDValue
1250ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001251 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001252 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001253 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001254 SmallVectorImpl<SDValue> &InVals,
1255 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001256
Bob Wilsona4c22902009-04-17 19:07:39 +00001257 // Assign locations to each value returned by this call.
1258 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00001259 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1260 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001261 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001262 CCAssignFnForNode(CallConv, /* Return*/ true,
1263 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001264
1265 // Copy all of the result registers out of their specified physreg.
1266 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1267 CCValAssign VA = RVLocs[i];
1268
Stephen Linb8bd2322013-04-20 05:14:40 +00001269 // Pass 'this' value directly from the argument to return value, to avoid
1270 // reg unit interference
1271 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001272 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1273 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001274 InVals.push_back(ThisVal);
1275 continue;
1276 }
1277
Bob Wilson0041bd32009-04-25 00:33:20 +00001278 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001279 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001280 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001281 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001282 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001283 Chain = Lo.getValue(1);
1284 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001285 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001286 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001287 InFlag);
1288 Chain = Hi.getValue(1);
1289 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001290 if (!Subtarget->isLittle())
1291 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001292 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001293
Owen Anderson9f944592009-08-11 20:47:22 +00001294 if (VA.getLocVT() == MVT::v2f64) {
1295 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1296 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1297 DAG.getConstant(0, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001298
1299 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001300 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001301 Chain = Lo.getValue(1);
1302 InFlag = Lo.getValue(2);
1303 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001304 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001305 Chain = Hi.getValue(1);
1306 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001307 if (!Subtarget->isLittle())
1308 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001309 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001310 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1311 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001312 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001313 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001314 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1315 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001316 Chain = Val.getValue(1);
1317 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001318 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001319
1320 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001321 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001322 case CCValAssign::Full: break;
1323 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001324 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001325 break;
1326 }
1327
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001328 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001329 }
1330
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001331 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001332}
1333
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001334/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001335SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001336ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1337 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001338 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001339 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001340 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001341 unsigned LocMemOffset = VA.getLocMemOffset();
1342 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1343 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilsona4c22902009-04-17 19:07:39 +00001344 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner886250c2010-09-21 18:51:21 +00001345 MachinePointerInfo::getStack(LocMemOffset),
David Greene0d0149f2010-02-15 16:55:24 +00001346 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001347}
1348
Andrew Trickef9de2a2013-05-25 02:42:55 +00001349void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001350 SDValue Chain, SDValue &Arg,
1351 RegsToPassVector &RegsToPass,
1352 CCValAssign &VA, CCValAssign &NextVA,
1353 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001354 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001355 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001356
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001357 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001358 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001359 unsigned id = Subtarget->isLittle() ? 0 : 1;
1360 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001361
1362 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001363 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001364 else {
1365 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001366 if (!StackPtr.getNode())
Bob Wilson2e076c42009-06-22 23:27:02 +00001367 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1368
Christian Pirkerb5728192014-05-08 14:06:24 +00001369 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001370 dl, DAG, NextVA,
1371 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001372 }
1373}
1374
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001375/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001376/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1377/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001378SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001379ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001380 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001381 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001382 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001383 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1384 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1385 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001386 SDValue Chain = CLI.Chain;
1387 SDValue Callee = CLI.Callee;
1388 bool &isTailCall = CLI.IsTailCall;
1389 CallingConv::ID CallConv = CLI.CallConv;
1390 bool doesNotRet = CLI.DoesNotReturn;
1391 bool isVarArg = CLI.IsVarArg;
1392
Dale Johannesend679ff72010-06-03 21:09:53 +00001393 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001394 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1395 bool isThisReturn = false;
1396 bool isSibCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001397
Bob Wilson8decdc42011-10-07 17:17:49 +00001398 // Disable tail calls if they're not supported.
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001399 if (!Subtarget->supportsTailCall() || MF.getTarget().Options.DisableTailCalls)
Bob Wilson3c9ed762010-08-13 22:43:33 +00001400 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001401
Dale Johannesend679ff72010-06-03 21:09:53 +00001402 if (isTailCall) {
1403 // Check if it's really possible to do a tail call.
1404 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001405 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001406 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001407 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1408 report_fatal_error("failed to perform tail call elimination on a call "
1409 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001410 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1411 // detected sibcalls.
1412 if (isTailCall) {
1413 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001414 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001415 }
1416 }
Evan Cheng10043e22007-01-19 07:51:42 +00001417
Bob Wilsona4c22902009-04-17 19:07:39 +00001418 // Analyze operands of the call, assigning locations to each operand.
1419 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00001420 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1421 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001422 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001423 CCAssignFnForNode(CallConv, /* Return*/ false,
1424 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001425
Bob Wilsona4c22902009-04-17 19:07:39 +00001426 // Get a count of how many bytes are to be pushed on the stack.
1427 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001428
Dale Johannesend679ff72010-06-03 21:09:53 +00001429 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001430 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001431 NumBytes = 0;
1432
Evan Cheng10043e22007-01-19 07:51:42 +00001433 // Adjust the stack pointer for the new arguments...
1434 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001435 if (!isSibCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00001436 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
1437 dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001438
Jim Grosbach6ad4bcb2010-02-24 01:43:03 +00001439 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Cheng10043e22007-01-19 07:51:42 +00001440
Bob Wilson2e076c42009-06-22 23:27:02 +00001441 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001442 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001443
Bob Wilsona4c22902009-04-17 19:07:39 +00001444 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001445 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001446 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1447 i != e;
1448 ++i, ++realArgIdx) {
1449 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001450 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001451 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001452 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001453
Bob Wilsona4c22902009-04-17 19:07:39 +00001454 // Promote the value if needed.
1455 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001456 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001457 case CCValAssign::Full: break;
1458 case CCValAssign::SExt:
1459 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1460 break;
1461 case CCValAssign::ZExt:
1462 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1463 break;
1464 case CCValAssign::AExt:
1465 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1466 break;
1467 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001468 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001469 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001470 }
1471
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001472 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001473 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001474 if (VA.getLocVT() == MVT::v2f64) {
1475 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1476 DAG.getConstant(0, MVT::i32));
1477 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1478 DAG.getConstant(1, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001479
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001480 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001481 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1482
1483 VA = ArgLocs[++i]; // skip ahead to next loc
1484 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001485 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001486 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1487 } else {
1488 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001489
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001490 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1491 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001492 }
1493 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001494 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001495 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001496 }
1497 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001498 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1499 assert(VA.getLocVT() == MVT::i32 &&
1500 "unexpected calling convention register assignment");
1501 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001502 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001503 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001504 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001505 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001506 } else if (isByVal) {
1507 assert(VA.isMemLoc());
1508 unsigned offset = 0;
1509
1510 // True if this byval aggregate will be split between registers
1511 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001512 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
1513 unsigned CurByValIdx = CCInfo.getInRegsParamsProceed();
1514
1515 if (CurByValIdx < ByValArgsCount) {
1516
1517 unsigned RegBegin, RegEnd;
1518 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1519
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001520 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1521 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001522 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001523 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1524 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1525 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1526 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001527 false, false, false,
1528 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001529 MemOpChains.push_back(Load.getValue(1));
1530 RegsToPass.push_back(std::make_pair(j, Load));
1531 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001532
1533 // If parameter size outsides register area, "offset" value
1534 // helps us to calculate stack slot for remained part properly.
1535 offset = RegEnd - RegBegin;
1536
1537 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001538 }
1539
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001540 if (Flags.getByValSize() > 4*offset) {
Manman Ren9f911162012-06-01 02:44:42 +00001541 unsigned LocMemOffset = VA.getLocMemOffset();
1542 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1543 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1544 StkPtrOff);
1545 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1546 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1547 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1548 MVT::i32);
Manman Rene8735522012-06-01 19:33:18 +00001549 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001550
Manman Ren9f911162012-06-01 02:44:42 +00001551 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001552 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001553 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001554 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001555 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001556 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001557 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001558
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001559 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1560 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001561 }
Evan Cheng10043e22007-01-19 07:51:42 +00001562 }
1563
1564 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001565 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001566
1567 // Build a sequence of copy-to-reg nodes chained together with token chain
1568 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001569 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001570 // Tail call byval lowering might overwrite argument registers so in case of
1571 // tail call optimization the copies to registers are lowered later.
1572 if (!isTailCall)
1573 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1574 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1575 RegsToPass[i].second, InFlag);
1576 InFlag = Chain.getValue(1);
1577 }
Evan Cheng10043e22007-01-19 07:51:42 +00001578
Dale Johannesend679ff72010-06-03 21:09:53 +00001579 // For tail calls lower the arguments to the 'real' stack slot.
1580 if (isTailCall) {
1581 // Force all the incoming stack arguments to be loaded from the stack
1582 // before any new outgoing arguments are stored to the stack, because the
1583 // outgoing stack slots may alias the incoming argument stack slots, and
1584 // the alias isn't otherwise explicit. This is slightly more conservative
1585 // than necessary, because it means that each store effectively depends
1586 // on every argument instead of just those arguments it would clobber.
1587
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001588 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001589 InFlag = SDValue();
1590 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1591 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1592 RegsToPass[i].second, InFlag);
1593 InFlag = Chain.getValue(1);
1594 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001595 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001596 }
1597
Bill Wendling24c79f22008-09-16 21:48:12 +00001598 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1599 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1600 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001601 bool isDirect = false;
1602 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001603 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001604 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001605
1606 if (EnableARMLongCalls) {
Saleem Abdulrasool90386ad2014-06-07 20:29:27 +00001607 assert((Subtarget->isTargetWindows() ||
1608 getTargetMachine().getRelocationModel() == Reloc::Static) &&
1609 "long-calls with non-static relocation model!");
Jim Grosbach32bb3622010-04-14 22:28:31 +00001610 // Handle a global address or an external symbol. If it's not one of
1611 // those, the target's already in a register, so we don't need to do
1612 // anything extra.
1613 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001614 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001615 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001616 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001617 ARMConstantPoolValue *CPV =
1618 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1619
Jim Grosbach32bb3622010-04-14 22:28:31 +00001620 // Get the address of the callee into a register
1621 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1622 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1623 Callee = DAG.getLoad(getPointerTy(), dl,
1624 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001625 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001626 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001627 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1628 const char *Sym = S->getSymbol();
1629
1630 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001631 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001632 ARMConstantPoolValue *CPV =
1633 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1634 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001635 // Get the address of the callee into a register
1636 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1637 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1638 Callee = DAG.getLoad(getPointerTy(), dl,
1639 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001640 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001641 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001642 }
1643 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001644 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001645 isDirect = true;
Chris Lattner55452c22009-07-15 04:12:33 +00001646 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Tim Northoverd6a729b2014-01-06 14:28:05 +00001647 bool isStub = (isExt && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001648 getTargetMachine().getRelocationModel() != Reloc::Static;
1649 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc3c949b42007-06-19 21:05:09 +00001650 // ARM call to a local ARM function is predicable.
Evan Chengf128bdc2010-06-16 07:35:02 +00001651 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001652 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001653 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001654 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Tim Northover72360d22013-12-02 10:35:41 +00001655 Callee = DAG.getNode(ARMISD::WrapperPIC, dl, getPointerTy(),
1656 DAG.getTargetGlobalAddress(GV, dl, getPointerTy()));
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001657 } else {
1658 // On ELF targets for PIC code, direct calls should go through the PLT
1659 unsigned OpFlags = 0;
1660 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001661 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001662 OpFlags = ARMII::MO_PLT;
1663 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1664 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001665 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001666 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001667 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001668 getTargetMachine().getRelocationModel() != Reloc::Static;
1669 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng83f35172007-01-30 20:37:08 +00001670 // tBX takes a register source operand.
1671 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001672 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001673 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001674 ARMConstantPoolValue *CPV =
1675 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1676 ARMPCLabelIndex, 4);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001677 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001678 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen021052a2009-02-04 20:06:27 +00001679 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Chengcdbb70c2009-10-31 03:39:36 +00001680 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001681 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001682 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00001683 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson7117a912009-03-20 22:42:55 +00001684 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen021052a2009-02-04 20:06:27 +00001685 getPointerTy(), Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001686 } else {
1687 unsigned OpFlags = 0;
1688 // On ELF targets for PIC code, direct calls should go through the PLT
1689 if (Subtarget->isTargetELF() &&
1690 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1691 OpFlags = ARMII::MO_PLT;
1692 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1693 }
Evan Cheng10043e22007-01-19 07:51:42 +00001694 }
1695
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001696 // FIXME: handle tail calls differently.
1697 unsigned CallOpc;
Tim Northoverdee86042013-12-02 14:46:26 +00001698 bool HasMinSizeAttr = Subtarget->isMinSize();
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001699 if (Subtarget->isThumb()) {
1700 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001701 CallOpc = ARMISD::CALL_NOLINK;
1702 else
1703 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1704 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001705 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001706 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001707 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet8e1fe842012-11-02 21:32:17 +00001708 // Emit regular call when code size is the priority
1709 !HasMinSizeAttr)
Evan Cheng65f9d192012-02-28 18:51:51 +00001710 // "mov lr, pc; b _foo" to avoid confusing the RSP
1711 CallOpc = ARMISD::CALL_NOLINK;
1712 else
1713 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001714 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001715
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001716 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001717 Ops.push_back(Chain);
1718 Ops.push_back(Callee);
1719
1720 // Add argument registers to the end of the list so that they are known live
1721 // into the call.
1722 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1723 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1724 RegsToPass[i].second.getValueType()));
1725
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001726 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001727 if (!isTailCall) {
1728 const uint32_t *Mask;
1729 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1730 const ARMBaseRegisterInfo *ARI = static_cast<const ARMBaseRegisterInfo*>(TRI);
1731 if (isThisReturn) {
1732 // For 'this' returns, use the R0-preserving mask if applicable
1733 Mask = ARI->getThisReturnPreservedMask(CallConv);
1734 if (!Mask) {
1735 // Set isThisReturn to false if the calling convention is not one that
1736 // allows 'returned' to be modeled in this way, so LowerCallResult does
1737 // not try to pass 'this' straight through
1738 isThisReturn = false;
1739 Mask = ARI->getCallPreservedMask(CallConv);
1740 }
1741 } else
Stephen Linff7fcee2013-06-26 21:42:14 +00001742 Mask = ARI->getCallPreservedMask(CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001743
Matthias Braunc22630e2013-10-04 16:52:54 +00001744 assert(Mask && "Missing call preserved mask for calling convention");
1745 Ops.push_back(DAG.getRegisterMask(Mask));
1746 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001747
Gabor Greiff304a7a2008-08-28 21:40:38 +00001748 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001749 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001750
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001751 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001752 if (isTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001753 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Dale Johannesend679ff72010-06-03 21:09:53 +00001754
Duncan Sands739a0542008-07-02 17:40:58 +00001755 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001756 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001757 InFlag = Chain.getValue(1);
1758
Chris Lattner27539552008-10-11 22:08:30 +00001759 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001760 DAG.getIntPtrConstant(0, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001761 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001762 InFlag = Chain.getValue(1);
1763
Bob Wilsona4c22902009-04-17 19:07:39 +00001764 // Handle result values, copying them out of physregs into vregs that we
1765 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001766 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001767 InVals, isThisReturn,
1768 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001769}
1770
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001771/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001772/// on the stack. Remember the next parameter register to allocate,
1773/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001774/// this.
1775void
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001776ARMTargetLowering::HandleByVal(
1777 CCState *State, unsigned &size, unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001778 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1779 assert((State->getCallOrPrologue() == Prologue ||
1780 State->getCallOrPrologue() == Call) &&
1781 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001782
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001783 if ((ARM::R0 <= reg) && (reg <= ARM::R3)) {
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001784 if (Subtarget->isAAPCS_ABI() && Align > 4) {
1785 unsigned AlignInRegs = Align / 4;
1786 unsigned Waste = (ARM::R4 - reg) % AlignInRegs;
1787 for (unsigned i = 0; i < Waste; ++i)
1788 reg = State->AllocateReg(GPRArgRegs, 4);
1789 }
1790 if (reg != 0) {
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001791 unsigned excess = 4 * (ARM::R4 - reg);
1792
1793 // Special case when NSAA != SP and parameter size greater than size of
1794 // all remained GPR regs. In that case we can't split parameter, we must
1795 // send it to stack. We also must set NCRN to R4, so waste all
1796 // remained registers.
Oliver Stannardd55e1152014-03-05 15:25:27 +00001797 const unsigned NSAAOffset = State->getNextStackOffset();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001798 if (Subtarget->isAAPCS_ABI() && NSAAOffset != 0 && size > excess) {
1799 while (State->AllocateReg(GPRArgRegs, 4))
1800 ;
1801 return;
1802 }
1803
1804 // First register for byval parameter is the first register that wasn't
1805 // allocated before this method call, so it would be "reg".
1806 // If parameter is small enough to be saved in range [reg, r4), then
1807 // the end (first after last) register would be reg + param-size-in-regs,
1808 // else parameter would be splitted between registers and stack,
1809 // end register would be r4 in this case.
1810 unsigned ByValRegBegin = reg;
Stepan Dyatkovskiy2703bca2013-05-08 14:51:27 +00001811 unsigned ByValRegEnd = (size < excess) ? reg + size/4 : (unsigned)ARM::R4;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001812 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
1813 // Note, first register is allocated in the beginning of function already,
1814 // allocate remained amount of registers we need.
1815 for (unsigned i = reg+1; i != ByValRegEnd; ++i)
1816 State->AllocateReg(GPRArgRegs, 4);
Oliver Stannardd55e1152014-03-05 15:25:27 +00001817 // A byval parameter that is split between registers and memory needs its
1818 // size truncated here.
1819 // In the case where the entire structure fits in registers, we set the
1820 // size in memory to zero.
1821 if (size < excess)
1822 size = 0;
1823 else
1824 size -= excess;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001825 }
1826 }
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001827}
1828
Dale Johannesend679ff72010-06-03 21:09:53 +00001829/// MatchingStackOffset - Return true if the given stack call argument is
1830/// already available in the same position (relatively) of the caller's
1831/// incoming argument stack.
1832static
1833bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1834 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00001835 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00001836 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1837 int FI = INT_MAX;
1838 if (Arg.getOpcode() == ISD::CopyFromReg) {
1839 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001840 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00001841 return false;
1842 MachineInstr *Def = MRI->getVRegDef(VR);
1843 if (!Def)
1844 return false;
1845 if (!Flags.isByVal()) {
1846 if (!TII->isLoadFromStackSlot(Def, FI))
1847 return false;
1848 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00001849 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00001850 }
1851 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1852 if (Flags.isByVal())
1853 // ByVal argument is passed in as a pointer but it's now being
1854 // dereferenced. e.g.
1855 // define @foo(%struct.X* %A) {
1856 // tail call @bar(%struct.X* byval %A)
1857 // }
1858 return false;
1859 SDValue Ptr = Ld->getBasePtr();
1860 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1861 if (!FINode)
1862 return false;
1863 FI = FINode->getIndex();
1864 } else
1865 return false;
1866
1867 assert(FI != INT_MAX);
1868 if (!MFI->isFixedObjectIndex(FI))
1869 return false;
1870 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1871}
1872
1873/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1874/// for tail call optimization. Targets which want to do tail call
1875/// optimization should implement this function.
1876bool
1877ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1878 CallingConv::ID CalleeCC,
1879 bool isVarArg,
1880 bool isCalleeStructRet,
1881 bool isCallerStructRet,
1882 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001883 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00001884 const SmallVectorImpl<ISD::InputArg> &Ins,
1885 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00001886 const Function *CallerF = DAG.getMachineFunction().getFunction();
1887 CallingConv::ID CallerCC = CallerF->getCallingConv();
1888 bool CCMatch = CallerCC == CalleeCC;
1889
1890 // Look for obvious safe cases to perform tail call optimization that do not
1891 // require ABI changes. This is what gcc calls sibcall.
1892
Jim Grosbache3864cc2010-06-16 23:45:49 +00001893 // Do not sibcall optimize vararg calls unless the call site is not passing
1894 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00001895 if (isVarArg && !Outs.empty())
1896 return false;
1897
Tim Northoverd8407452013-10-01 14:33:28 +00001898 // Exception-handling functions need a special set of instructions to indicate
1899 // a return to the hardware. Tail-calling another function would probably
1900 // break this.
1901 if (CallerF->hasFnAttribute("interrupt"))
1902 return false;
1903
Dale Johannesend679ff72010-06-03 21:09:53 +00001904 // Also avoid sibcall optimization if either caller or callee uses struct
1905 // return semantics.
1906 if (isCalleeStructRet || isCallerStructRet)
1907 return false;
1908
Dale Johannesend24c66b2010-06-23 18:52:34 +00001909 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach3840c902011-07-08 20:18:11 +00001910 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1911 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1912 // support in the assembler and linker to be used. This would need to be
1913 // fixed to fully support tail calls in Thumb1.
1914 //
Dale Johannesene2289282010-07-08 01:18:23 +00001915 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1916 // LR. This means if we need to reload LR, it takes an extra instructions,
1917 // which outweighs the value of the tail call; but here we don't know yet
1918 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach535d3b42010-09-08 03:54:02 +00001919 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesene2289282010-07-08 01:18:23 +00001920 // emitEpilogue if LR is used.
Dale Johannesene2289282010-07-08 01:18:23 +00001921
1922 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1923 // but we need to make sure there are enough registers; the only valid
1924 // registers are the 4 used for parameters. We don't currently do this
1925 // case.
Evan Chengd4b08732010-11-30 23:55:39 +00001926 if (Subtarget->isThumb1Only())
1927 return false;
Dale Johannesen3ac52b32010-06-18 18:13:11 +00001928
Dale Johannesend679ff72010-06-03 21:09:53 +00001929 // If the calling conventions do not match, then we'd better make sure the
1930 // results are returned in the same way as what the caller expects.
1931 if (!CCMatch) {
1932 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwarich89019782011-06-10 20:59:24 +00001933 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1934 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00001935 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1936
1937 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwarich89019782011-06-10 20:59:24 +00001938 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1939 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00001940 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1941
1942 if (RVLocs1.size() != RVLocs2.size())
1943 return false;
1944 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1945 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1946 return false;
1947 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1948 return false;
1949 if (RVLocs1[i].isRegLoc()) {
1950 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1951 return false;
1952 } else {
1953 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1954 return false;
1955 }
1956 }
1957 }
1958
Manman Ren7e48b252012-10-12 23:39:43 +00001959 // If Caller's vararg or byval argument has been split between registers and
1960 // stack, do not perform tail call, since part of the argument is in caller's
1961 // local frame.
1962 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
1963 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00001964 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00001965 return false;
1966
Dale Johannesend679ff72010-06-03 21:09:53 +00001967 // If the callee takes no arguments then go on to check the results of the
1968 // call.
1969 if (!Outs.empty()) {
1970 // Check if stack adjustment is needed. For now, do not do this if any
1971 // argument is passed on the stack.
1972 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00001973 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1974 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00001975 CCInfo.AnalyzeCallOperands(Outs,
1976 CCAssignFnForNode(CalleeCC, false, isVarArg));
1977 if (CCInfo.getNextStackOffset()) {
1978 MachineFunction &MF = DAG.getMachineFunction();
1979
1980 // Check if the arguments are already laid out in the right way as
1981 // the caller's fixed stack objects.
1982 MachineFrameInfo *MFI = MF.getFrameInfo();
1983 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topper07720d82012-03-25 23:49:58 +00001984 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001985 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1986 i != e;
1987 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00001988 CCValAssign &VA = ArgLocs[i];
1989 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001990 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001991 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00001992 if (VA.getLocInfo() == CCValAssign::Indirect)
1993 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001994 if (VA.needsCustom()) {
1995 // f64 and vector types are split into multiple registers or
1996 // register/stack-slot combinations. The types will not match
1997 // the registers; give up on memory f64 refs until we figure
1998 // out what to do about this.
1999 if (!VA.isRegLoc())
2000 return false;
2001 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002002 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002003 if (RegVT == MVT::v2f64) {
2004 if (!ArgLocs[++i].isRegLoc())
2005 return false;
2006 if (!ArgLocs[++i].isRegLoc())
2007 return false;
2008 }
2009 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002010 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2011 MFI, MRI, TII))
2012 return false;
2013 }
2014 }
2015 }
2016 }
2017
2018 return true;
2019}
2020
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002021bool
2022ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2023 MachineFunction &MF, bool isVarArg,
2024 const SmallVectorImpl<ISD::OutputArg> &Outs,
2025 LLVMContext &Context) const {
2026 SmallVector<CCValAssign, 16> RVLocs;
2027 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), RVLocs, Context);
2028 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2029 isVarArg));
2030}
2031
Tim Northoverd8407452013-10-01 14:33:28 +00002032static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2033 SDLoc DL, SelectionDAG &DAG) {
2034 const MachineFunction &MF = DAG.getMachineFunction();
2035 const Function *F = MF.getFunction();
2036
2037 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2038
2039 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2040 // version of the "preferred return address". These offsets affect the return
2041 // instruction if this is a return from PL1 without hypervisor extensions.
2042 // IRQ/FIQ: +4 "subs pc, lr, #4"
2043 // SWI: 0 "subs pc, lr, #0"
2044 // ABORT: +4 "subs pc, lr, #4"
2045 // UNDEF: +4/+2 "subs pc, lr, #0"
2046 // UNDEF varies depending on where the exception came from ARM or Thumb
2047 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2048
2049 int64_t LROffset;
2050 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2051 IntKind == "ABORT")
2052 LROffset = 4;
2053 else if (IntKind == "SWI" || IntKind == "UNDEF")
2054 LROffset = 0;
2055 else
2056 report_fatal_error("Unsupported interrupt attribute. If present, value "
2057 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2058
2059 RetOps.insert(RetOps.begin() + 1, DAG.getConstant(LROffset, MVT::i32, false));
2060
Craig Topper48d114b2014-04-26 18:35:24 +00002061 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002062}
2063
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002064SDValue
2065ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002066 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002067 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002068 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002069 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002070
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002071 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002072 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002073
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002074 // CCState - Info about the registers and stack slots.
Cameron Zwarich89019782011-06-10 20:59:24 +00002075 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2076 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002077
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002078 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002079 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2080 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002081
Bob Wilsona4c22902009-04-17 19:07:39 +00002082 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002083 SmallVector<SDValue, 4> RetOps;
2084 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002085 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002086
2087 // Copy the result values into the output registers.
2088 for (unsigned i = 0, realRVLocIdx = 0;
2089 i != RVLocs.size();
2090 ++i, ++realRVLocIdx) {
2091 CCValAssign &VA = RVLocs[i];
2092 assert(VA.isRegLoc() && "Can only return in registers!");
2093
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002094 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002095
2096 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002097 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002098 case CCValAssign::Full: break;
2099 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002100 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002101 break;
2102 }
2103
Bob Wilsona4c22902009-04-17 19:07:39 +00002104 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002105 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002106 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002107 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2108 DAG.getConstant(0, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002109 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002110 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002111
Christian Pirkerb5728192014-05-08 14:06:24 +00002112 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2113 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2114 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002115 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002116 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002117 VA = RVLocs[++i]; // skip ahead to next loc
2118 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002119 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2120 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002121 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002122 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002123 VA = RVLocs[++i]; // skip ahead to next loc
2124
2125 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002126 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2127 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002128 }
2129 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2130 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002131 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002132 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002133 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2134 fmrrd.getValue(isLittleEndian ? 0 : 1),
2135 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002136 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002137 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002138 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002139 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2140 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002141 Flag);
2142 } else
2143 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2144
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002145 // Guarantee that all emitted copies are
2146 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002147 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002148 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002149 }
2150
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002151 // Update chain and glue.
2152 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002153 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002154 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002155
Tim Northoverd8407452013-10-01 14:33:28 +00002156 // CPUs which aren't M-class use a special sequence to return from
2157 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2158 // though we use "subs pc, lr, #N").
2159 //
2160 // M-class CPUs actually use a normal return sequence with a special
2161 // (hardware-provided) value in LR, so the normal code path works.
2162 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2163 !Subtarget->isMClass()) {
2164 if (Subtarget->isThumb1Only())
2165 report_fatal_error("interrupt attribute is not supported in Thumb1");
2166 return LowerInterruptReturn(RetOps, dl, DAG);
2167 }
2168
Craig Topper48d114b2014-04-26 18:35:24 +00002169 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002170}
2171
Evan Chengf8bad082012-04-10 01:51:00 +00002172bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002173 if (N->getNumValues() != 1)
2174 return false;
2175 if (!N->hasNUsesOfValue(1, 0))
2176 return false;
2177
Evan Chengf8bad082012-04-10 01:51:00 +00002178 SDValue TCChain = Chain;
2179 SDNode *Copy = *N->use_begin();
2180 if (Copy->getOpcode() == ISD::CopyToReg) {
2181 // If the copy has a glue operand, we conservatively assume it isn't safe to
2182 // perform a tail call.
2183 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2184 return false;
2185 TCChain = Copy->getOperand(0);
2186 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2187 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002188 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002189 SmallPtrSet<SDNode*, 2> Copies;
2190 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002191 UI != UE; ++UI) {
2192 if (UI->getOpcode() != ISD::CopyToReg)
2193 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002194 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002195 }
Evan Chengf8bad082012-04-10 01:51:00 +00002196 if (Copies.size() > 2)
2197 return false;
2198
2199 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2200 UI != UE; ++UI) {
2201 SDValue UseChain = UI->getOperand(0);
2202 if (Copies.count(UseChain.getNode()))
2203 // Second CopyToReg
2204 Copy = *UI;
2205 else
2206 // First CopyToReg
2207 TCChain = UseChain;
2208 }
2209 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002210 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002211 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002212 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002213 Copy = *Copy->use_begin();
2214 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002215 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002216 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002217 } else {
2218 return false;
2219 }
2220
Evan Cheng419ea282010-12-01 22:59:46 +00002221 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002222 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2223 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002224 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2225 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002226 return false;
2227 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002228 }
2229
Evan Chengf8bad082012-04-10 01:51:00 +00002230 if (!HasRet)
2231 return false;
2232
2233 Chain = TCChain;
2234 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002235}
2236
Evan Cheng0663f232011-03-21 01:19:09 +00002237bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002238 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002239 return false;
2240
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00002241 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng0663f232011-03-21 01:19:09 +00002242 return false;
2243
2244 return !Subtarget->isThumb1Only();
2245}
2246
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002247// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2248// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2249// one of the above mentioned nodes. It has to be wrapped because otherwise
2250// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2251// be used to form addressing mode. These wrapped nodes will be selected
2252// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002253static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002254 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002255 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002256 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002257 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002258 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002259 if (CP->isMachineConstantPoolEntry())
2260 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2261 CP->getAlignment());
2262 else
2263 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2264 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002265 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002266}
2267
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002268unsigned ARMTargetLowering::getJumpTableEncoding() const {
2269 return MachineJumpTableInfo::EK_Inline;
2270}
2271
Dan Gohman21cea8a2010-04-17 15:26:15 +00002272SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2273 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002274 MachineFunction &MF = DAG.getMachineFunction();
2275 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2276 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002277 SDLoc DL(Op);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002278 EVT PtrVT = getPointerTy();
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002279 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002280 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2281 SDValue CPAddr;
2282 if (RelocM == Reloc::Static) {
2283 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2284 } else {
2285 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002286 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002287 ARMConstantPoolValue *CPV =
2288 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2289 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002290 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2291 }
2292 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2293 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002294 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002295 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002296 if (RelocM == Reloc::Static)
2297 return Result;
Evan Cheng408aa562009-11-06 22:24:13 +00002298 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002299 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002300}
2301
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002302// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002303SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002304ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002305 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002306 SDLoc dl(GA);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002307 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002308 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002309 MachineFunction &MF = DAG.getMachineFunction();
2310 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002311 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002312 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002313 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2314 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002315 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002316 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002317 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattner7727d052010-09-21 06:44:06 +00002318 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002319 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002320 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002321
Evan Cheng408aa562009-11-06 22:24:13 +00002322 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002323 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002324
2325 // call __tls_get_addr.
2326 ArgListTy Args;
2327 ArgListEntry Entry;
2328 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002329 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002330 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002331
Dale Johannesen555a3752009-01-30 23:10:59 +00002332 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002333 TargetLowering::CallLoweringInfo CLI(DAG);
2334 CLI.setDebugLoc(dl).setChain(Chain)
2335 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
2336 DAG.getExternalSymbol("__tls_get_addr", PtrVT), &Args, 0);
2337
Justin Holewinskiaa583972012-05-25 16:35:28 +00002338 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002339 return CallResult.first;
2340}
2341
2342// Lower ISD::GlobalTLSAddress using the "initial exec" or
2343// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002344SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002345ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002346 SelectionDAG &DAG,
2347 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002348 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002349 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002350 SDValue Offset;
2351 SDValue Chain = DAG.getEntryNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002352 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002353 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002354 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002355
Hans Wennborgaea41202012-05-04 09:40:39 +00002356 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002357 MachineFunction &MF = DAG.getMachineFunction();
2358 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002359 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002360 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002361 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2362 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002363 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2364 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2365 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002366 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002367 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002368 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002369 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002370 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002371 Chain = Offset.getValue(1);
2372
Evan Cheng408aa562009-11-06 22:24:13 +00002373 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002374 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002375
Evan Chengcdbb70c2009-10-31 03:39:36 +00002376 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002377 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002378 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002379 } else {
2380 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002381 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002382 ARMConstantPoolValue *CPV =
2383 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002384 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002385 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002386 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002387 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002388 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002389 }
2390
2391 // The address of the thread local variable is the add of the thread
2392 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002393 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002394}
2395
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002396SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002397ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002398 // TODO: implement the "local dynamic" model
2399 assert(Subtarget->isTargetELF() &&
2400 "TLS not implemented for non-ELF targets");
2401 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgaea41202012-05-04 09:40:39 +00002402
2403 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2404
2405 switch (model) {
2406 case TLSModel::GeneralDynamic:
2407 case TLSModel::LocalDynamic:
2408 return LowerToTLSGeneralDynamicModel(GA, DAG);
2409 case TLSModel::InitialExec:
2410 case TLSModel::LocalExec:
2411 return LowerToTLSExecModels(GA, DAG, model);
2412 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002413 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002414}
2415
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002416SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002417 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002418 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002419 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002420 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002421 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Rafael Espindola6de96a12009-01-15 20:18:42 +00002422 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002423 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002424 ARMConstantPoolConstant::Create(GV,
2425 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002426 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002427 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson7117a912009-03-20 22:42:55 +00002428 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002429 CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002430 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002431 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002432 SDValue Chain = Result.getValue(1);
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002433 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00002434 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002435 if (!UseGOTOFF)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002436 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002437 MachinePointerInfo::getGOT(),
2438 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002439 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002440 }
2441
2442 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002443 // pair. This is always cheaper.
2444 if (Subtarget->useMovt()) {
Evan Cheng68aec142011-01-19 02:16:49 +00002445 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002446 // FIXME: Once remat is capable of dealing with instructions with register
2447 // operands, expand this into two nodes.
2448 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2449 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002450 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002451 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2452 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2453 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2454 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002455 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002456 }
2457}
2458
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002459SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002460 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002461 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002462 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002463 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002464 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002465
Tim Northover72360d22013-12-02 10:35:41 +00002466 if (Subtarget->useMovt())
Evan Cheng68aec142011-01-19 02:16:49 +00002467 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002468
Tim Northover72360d22013-12-02 10:35:41 +00002469 // FIXME: Once remat is capable of dealing with instructions with register
2470 // operands, expand this into multiple nodes
2471 unsigned Wrapper =
2472 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002473
Tim Northover72360d22013-12-02 10:35:41 +00002474 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2475 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002476
Evan Cheng1b389522009-09-03 07:04:02 +00002477 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002478 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2479 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002480 return Result;
2481}
2482
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002483SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2484 SelectionDAG &DAG) const {
2485 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
2486 assert(Subtarget->useMovt() && "Windows on ARM expects to use movw/movt");
2487
2488 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2489 EVT PtrVT = getPointerTy();
2490 SDLoc DL(Op);
2491
2492 ++NumMovwMovt;
2493
2494 // FIXME: Once remat is capable of dealing with instructions with register
2495 // operands, expand this into two nodes.
2496 return DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2497 DAG.getTargetGlobalAddress(GV, DL, PtrVT));
2498}
2499
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002500SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002501 SelectionDAG &DAG) const {
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002502 assert(Subtarget->isTargetELF() &&
2503 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Cheng408aa562009-11-06 22:24:13 +00002504 MachineFunction &MF = DAG.getMachineFunction();
2505 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002506 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002507 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002508 SDLoc dl(Op);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002509 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingc214cb02011-10-01 08:58:29 +00002510 ARMConstantPoolValue *CPV =
2511 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2512 ARMPCLabelIndex, PCAdj);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002513 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002514 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002515 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002516 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002517 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00002518 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002519 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002520}
2521
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002522SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002523ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002524 SDLoc dl(Op);
Jim Grosbachfaa3abb2010-05-27 23:49:24 +00002525 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002526 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2527 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002528 Op.getOperand(1), Val);
2529}
2530
2531SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002532ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002533 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002534 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2535 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2536}
2537
2538SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002539ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002540 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002541 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002542 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002543 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002544 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson17f88782009-08-04 00:25:01 +00002545 case Intrinsic::arm_thread_pointer: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002546 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson17f88782009-08-04 00:25:01 +00002547 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2548 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002549 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002550 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002551 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002552 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002553 EVT PtrVT = getPointerTy();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002554 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2555 SDValue CPAddr;
2556 unsigned PCAdj = (RelocM != Reloc::PIC_)
2557 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002558 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002559 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2560 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002561 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002562 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002563 SDValue Result =
Evan Chengcdbb70c2009-10-31 03:39:36 +00002564 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002565 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002566 false, false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002567
2568 if (RelocM == Reloc::PIC_) {
Evan Cheng408aa562009-11-06 22:24:13 +00002569 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002570 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2571 }
2572 return Result;
2573 }
Evan Cheng18381b42011-03-29 23:06:19 +00002574 case Intrinsic::arm_neon_vmulls:
2575 case Intrinsic::arm_neon_vmullu: {
2576 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2577 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002578 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002579 Op.getOperand(1), Op.getOperand(2));
2580 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002581 }
2582}
2583
Eli Friedman30a49e92011-08-03 21:06:02 +00002584static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2585 const ARMSubtarget *Subtarget) {
2586 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002587 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00002588 if (!Subtarget->hasDataBarrier()) {
2589 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2590 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2591 // here.
2592 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00002593 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00002594 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00002595 DAG.getConstant(0, MVT::i32));
2596 }
2597
Tim Northover36b24172013-07-03 09:20:36 +00002598 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
2599 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
2600 unsigned Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00002601 if (Subtarget->isMClass()) {
2602 // Only a full system barrier exists in the M-class architectures.
2603 Domain = ARM_MB::SY;
2604 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00002605 // Swift happens to implement ISHST barriers in a way that's compatible with
2606 // Release semantics but weaker than ISH so we'd be fools not to use
2607 // it. Beware: other processors probably don't!
2608 Domain = ARM_MB::ISHST;
2609 }
2610
Joey Gouly926d3f52013-09-05 15:35:24 +00002611 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
2612 DAG.getConstant(Intrinsic::arm_dmb, MVT::i32),
Tim Northover36b24172013-07-03 09:20:36 +00002613 DAG.getConstant(Domain, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002614}
2615
Evan Cheng8740ee32010-11-03 06:34:55 +00002616static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2617 const ARMSubtarget *Subtarget) {
2618 // ARM pre v5TE and Thumb1 does not have preload instructions.
2619 if (!(Subtarget->isThumb2() ||
2620 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2621 // Just preserve the chain.
2622 return Op.getOperand(0);
2623
Andrew Trickef9de2a2013-05-25 02:42:55 +00002624 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00002625 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2626 if (!isRead &&
2627 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2628 // ARMv7 with MP extension has PLDW.
2629 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00002630
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002631 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2632 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00002633 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00002634 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002635 isData = ~isData & 1;
2636 }
Evan Cheng8740ee32010-11-03 06:34:55 +00002637
2638 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng21acf9f2010-11-04 05:19:35 +00002639 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2640 DAG.getConstant(isData, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00002641}
2642
Dan Gohman31ae5862010-04-17 14:41:14 +00002643static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2644 MachineFunction &MF = DAG.getMachineFunction();
2645 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2646
Evan Cheng10043e22007-01-19 07:51:42 +00002647 // vastart just stores the address of the VarArgsFrameIndex slot into the
2648 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002649 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002650 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00002651 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002652 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00002653 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2654 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002655}
2656
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002657SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00002658ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2659 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002660 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00002661 MachineFunction &MF = DAG.getMachineFunction();
2662 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2663
Craig Topper760b1342012-02-22 05:59:10 +00002664 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00002665 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002666 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002667 else
Craig Topperc7242e02012-04-20 07:30:17 +00002668 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002669
2670 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002671 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002672 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002673
2674 SDValue ArgValue2;
2675 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002676 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00002677 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00002678
2679 // Create load node to retrieve arguments from the stack.
2680 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Chengcdbb70c2009-10-31 03:39:36 +00002681 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002682 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002683 false, false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00002684 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00002685 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002686 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002687 }
Christian Pirkerb5728192014-05-08 14:06:24 +00002688 if (!Subtarget->isLittle())
2689 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002690 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00002691}
2692
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002693void
2694ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002695 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002696 unsigned ArgSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002697 unsigned &ArgRegsSize,
2698 unsigned &ArgRegsSaveSize)
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002699 const {
2700 unsigned NumGPRs;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002701 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2702 unsigned RBegin, REnd;
2703 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2704 NumGPRs = REnd - RBegin;
2705 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002706 unsigned int firstUnalloced;
2707 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2708 sizeof(GPRArgRegs) /
2709 sizeof(GPRArgRegs[0]));
2710 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2711 }
2712
2713 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002714 ArgRegsSize = NumGPRs * 4;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002715
2716 // If parameter is split between stack and GPRs...
Mark Seabornbe266aa2014-02-16 18:59:48 +00002717 if (NumGPRs && Align > 4 &&
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002718 (ArgRegsSize < ArgSize ||
2719 InRegsParamRecordIdx >= CCInfo.getInRegsParamsCount())) {
Mark Seabornbe266aa2014-02-16 18:59:48 +00002720 // Add padding for part of param recovered from GPRs. For example,
2721 // if Align == 8, its last byte must be at address K*8 - 1.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002722 // We need to do it, since remained (stack) part of parameter has
2723 // stack alignment, and we need to "attach" "GPRs head" without gaps
2724 // to it:
2725 // Stack:
2726 // |---- 8 bytes block ----| |---- 8 bytes block ----| |---- 8 bytes...
2727 // [ [padding] [GPRs head] ] [ Tail passed via stack ....
2728 //
2729 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2730 unsigned Padding =
Mark Seabornbe266aa2014-02-16 18:59:48 +00002731 OffsetToAlignment(ArgRegsSize + AFI->getArgRegsSaveSize(), Align);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002732 ArgRegsSaveSize = ArgRegsSize + Padding;
2733 } else
2734 // We don't need to extend regs save size for byval parameters if they
2735 // are passed via GPRs only.
2736 ArgRegsSaveSize = ArgRegsSize;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002737}
2738
2739// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00002740// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002741// byval). Either way, we allocate stack slots adjacent to the data
2742// provided by our caller, and store the unallocated registers there.
2743// If this is a variadic function, the va_list pointer will begin with
2744// these values; otherwise, this reassembles a (byval) structure that
2745// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002746// Return: The frame index registers were stored into.
2747int
2748ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002749 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002750 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002751 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002752 unsigned OffsetFromOrigArg,
2753 unsigned ArgOffset,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002754 unsigned ArgSize,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002755 bool ForceMutable,
2756 unsigned ByValStoreOffset,
2757 unsigned TotalArgRegsSaveSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002758
2759 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00002760 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002761 // Setup first unallocated register as first byval register;
2762 // eat all remained registers
2763 // (these two actions are performed by HandleByVal method).
2764 // Then, here, we initialize stack frame with
2765 // "store-reg" instructions.
2766 // Case #2. Var-args function, that doesn't contain byval parameters.
2767 // The same: eat all remained unallocated registers,
2768 // initialize stack frame.
2769
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002770 MachineFunction &MF = DAG.getMachineFunction();
2771 MachineFrameInfo *MFI = MF.getFrameInfo();
2772 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002773 unsigned firstRegToSaveIndex, lastRegToSaveIndex;
2774 unsigned RBegin, REnd;
2775 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2776 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2777 firstRegToSaveIndex = RBegin - ARM::R0;
2778 lastRegToSaveIndex = REnd - ARM::R0;
2779 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002780 firstRegToSaveIndex = CCInfo.getFirstUnallocated
Craig Topper58713212013-07-15 04:27:47 +00002781 (GPRArgRegs, array_lengthof(GPRArgRegs));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002782 lastRegToSaveIndex = 4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002783 }
2784
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002785 unsigned ArgRegsSize, ArgRegsSaveSize;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002786 computeRegArea(CCInfo, MF, InRegsParamRecordIdx, ArgSize,
2787 ArgRegsSize, ArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002788
2789 // Store any by-val regs to their spots on the stack so that they may be
2790 // loaded by deferencing the result of formal parameter pointer or va_next.
2791 // Note: once stack area for byval/varargs registers
2792 // was initialized, it can't be initialized again.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002793 if (ArgRegsSaveSize) {
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002794 unsigned Padding = ArgRegsSaveSize - ArgRegsSize;
2795
2796 if (Padding) {
2797 assert(AFI->getStoredByValParamsPadding() == 0 &&
2798 "The only parameter may be padded.");
2799 AFI->setStoredByValParamsPadding(Padding);
2800 }
2801
Oliver Stannardd55e1152014-03-05 15:25:27 +00002802 int FrameIndex = MFI->CreateFixedObject(ArgRegsSaveSize,
2803 Padding +
2804 ByValStoreOffset -
2805 (int64_t)TotalArgRegsSaveSize,
2806 false);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002807 SDValue FIN = DAG.getFrameIndex(FrameIndex, getPointerTy());
Oliver Stannardd55e1152014-03-05 15:25:27 +00002808 if (Padding) {
2809 MFI->CreateFixedObject(Padding,
2810 ArgOffset + ByValStoreOffset -
2811 (int64_t)ArgRegsSaveSize,
2812 false);
2813 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002814
2815 SmallVector<SDValue, 4> MemOps;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002816 for (unsigned i = 0; firstRegToSaveIndex < lastRegToSaveIndex;
2817 ++firstRegToSaveIndex, ++i) {
Craig Topper760b1342012-02-22 05:59:10 +00002818 const TargetRegisterClass *RC;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002819 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002820 RC = &ARM::tGPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002821 else
Craig Topperc7242e02012-04-20 07:30:17 +00002822 RC = &ARM::GPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002823
2824 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2825 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2826 SDValue Store =
2827 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002828 MachinePointerInfo(OrigArg, OffsetFromOrigArg + 4*i),
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002829 false, false, 0);
2830 MemOps.push_back(Store);
2831 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2832 DAG.getConstant(4, getPointerTy()));
2833 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002834
2835 AFI->setArgRegsSaveSize(ArgRegsSaveSize + AFI->getArgRegsSaveSize());
2836
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002837 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002838 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002839 return FrameIndex;
Oliver Stannardd55e1152014-03-05 15:25:27 +00002840 } else {
2841 if (ArgSize == 0) {
2842 // We cannot allocate a zero-byte object for the first variadic argument,
2843 // so just make up a size.
2844 ArgSize = 4;
2845 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002846 // This will point to the next argument passed via stack.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002847 return MFI->CreateFixedObject(
Oliver Stannardd55e1152014-03-05 15:25:27 +00002848 ArgSize, ArgOffset, !ForceMutable);
2849 }
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002850}
2851
2852// Setup stack frame, the va_list pointer will start from.
2853void
2854ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002855 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002856 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002857 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002858 bool ForceMutable) const {
2859 MachineFunction &MF = DAG.getMachineFunction();
2860 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2861
2862 // Try to store any remaining integer argument regs
2863 // to their spots on the stack so that they may be loaded by deferencing
2864 // the result of va_next.
2865 // If there is no regs to be stored, just point address after last
2866 // argument passed via stack.
2867 int FrameIndex =
Craig Topper062a2ba2014-04-25 05:30:21 +00002868 StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
2869 CCInfo.getInRegsParamsCount(), 0, ArgOffset, 0, ForceMutable,
2870 0, TotalArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002871
2872 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002873}
2874
Bob Wilson2e076c42009-06-22 23:27:02 +00002875SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002876ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002877 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002878 const SmallVectorImpl<ISD::InputArg>
2879 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002880 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002881 SmallVectorImpl<SDValue> &InVals)
2882 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00002883 MachineFunction &MF = DAG.getMachineFunction();
2884 MachineFrameInfo *MFI = MF.getFrameInfo();
2885
Bob Wilsona4c22902009-04-17 19:07:39 +00002886 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2887
2888 // Assign locations to all of the incoming arguments.
2889 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00002890 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2891 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002892 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002893 CCAssignFnForNode(CallConv, /* Return*/ false,
2894 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00002895
Bob Wilsona4c22902009-04-17 19:07:39 +00002896 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002897 int lastInsIndex = -1;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002898 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002899 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
2900 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002901
2902 // Initially ArgRegsSaveSize is zero.
2903 // Then we increase this value each time we meet byval parameter.
2904 // We also increase this value in case of varargs function.
2905 AFI->setArgRegsSaveSize(0);
2906
Oliver Stannardd55e1152014-03-05 15:25:27 +00002907 unsigned ByValStoreOffset = 0;
2908 unsigned TotalArgRegsSaveSize = 0;
2909 unsigned ArgRegsSaveSizeMaxAlign = 4;
2910
2911 // Calculate the amount of stack space that we need to allocate to store
2912 // byval and variadic arguments that are passed in registers.
2913 // We need to know this before we allocate the first byval or variadic
2914 // argument, as they will be allocated a stack slot below the CFA (Canonical
2915 // Frame Address, the stack pointer at entry to the function).
2916 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2917 CCValAssign &VA = ArgLocs[i];
2918 if (VA.isMemLoc()) {
2919 int index = VA.getValNo();
2920 if (index != lastInsIndex) {
2921 ISD::ArgFlagsTy Flags = Ins[index].Flags;
2922 if (Flags.isByVal()) {
2923 unsigned ExtraArgRegsSize;
2924 unsigned ExtraArgRegsSaveSize;
2925 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsProceed(),
2926 Flags.getByValSize(),
2927 ExtraArgRegsSize, ExtraArgRegsSaveSize);
2928
2929 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
2930 if (Flags.getByValAlign() > ArgRegsSaveSizeMaxAlign)
2931 ArgRegsSaveSizeMaxAlign = Flags.getByValAlign();
2932 CCInfo.nextInRegsParam();
2933 }
2934 lastInsIndex = index;
2935 }
2936 }
2937 }
2938 CCInfo.rewindByValRegsInfo();
2939 lastInsIndex = -1;
2940 if (isVarArg) {
2941 unsigned ExtraArgRegsSize;
2942 unsigned ExtraArgRegsSaveSize;
2943 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsCount(), 0,
2944 ExtraArgRegsSize, ExtraArgRegsSaveSize);
2945 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
2946 }
2947 // If the arg regs save area contains N-byte aligned values, the
2948 // bottom of it must be at least N-byte aligned.
2949 TotalArgRegsSaveSize = RoundUpToAlignment(TotalArgRegsSaveSize, ArgRegsSaveSizeMaxAlign);
2950 TotalArgRegsSaveSize = std::min(TotalArgRegsSaveSize, 16U);
2951
Bob Wilsona4c22902009-04-17 19:07:39 +00002952 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2953 CCValAssign &VA = ArgLocs[i];
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002954 std::advance(CurOrigArg, Ins[VA.getValNo()].OrigArgIndex - CurArgIdx);
2955 CurArgIdx = Ins[VA.getValNo()].OrigArgIndex;
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002956 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00002957 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002958 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00002959
Bob Wilsona4c22902009-04-17 19:07:39 +00002960 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002961 // f64 and vector types are split up into multiple registers or
2962 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00002963 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002964 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002965 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00002966 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00002967 SDValue ArgValue2;
2968 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00002969 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson699bdf72010-04-13 22:03:22 +00002970 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2971 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002972 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002973 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00002974 } else {
2975 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2976 Chain, DAG, dl);
2977 }
Owen Anderson9f944592009-08-11 20:47:22 +00002978 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2979 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00002980 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson9f944592009-08-11 20:47:22 +00002981 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00002982 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2983 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002984 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00002985
Bob Wilson2e076c42009-06-22 23:27:02 +00002986 } else {
Craig Topper760b1342012-02-22 05:59:10 +00002987 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002988
Owen Anderson9f944592009-08-11 20:47:22 +00002989 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00002990 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00002991 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00002992 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00002993 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00002994 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00002995 else if (RegVT == MVT::i32)
Craig Topperc7242e02012-04-20 07:30:17 +00002996 RC = AFI->isThumb1OnlyFunction() ?
2997 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2998 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002999 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003000 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003001
3002 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003003 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003004 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003005 }
3006
3007 // If this is an 8 or 16-bit value, it is really passed promoted
3008 // to 32 bits. Insert an assert[sz]ext to capture this, then
3009 // truncate to the right size.
3010 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003011 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003012 case CCValAssign::Full: break;
3013 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003014 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003015 break;
3016 case CCValAssign::SExt:
3017 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3018 DAG.getValueType(VA.getValVT()));
3019 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3020 break;
3021 case CCValAssign::ZExt:
3022 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3023 DAG.getValueType(VA.getValVT()));
3024 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3025 break;
3026 }
3027
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003028 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003029
3030 } else { // VA.isRegLoc()
3031
3032 // sanity check
3033 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003034 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003035
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003036 int index = ArgLocs[i].getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003037
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003038 // Some Ins[] entries become multiple ArgLoc[] entries.
3039 // Process them only once.
3040 if (index != lastInsIndex)
3041 {
3042 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003043 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003044 // This can be changed with more analysis.
3045 // In case of tail call optimization mark all arguments mutable.
3046 // Since they could be overwritten by lowering of arguments in case of
3047 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003048 if (Flags.isByVal()) {
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003049 unsigned CurByValIndex = CCInfo.getInRegsParamsProceed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003050
3051 ByValStoreOffset = RoundUpToAlignment(ByValStoreOffset, Flags.getByValAlign());
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003052 int FrameIndex = StoreByValRegs(
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003053 CCInfo, DAG, dl, Chain, CurOrigArg,
3054 CurByValIndex,
3055 Ins[VA.getValNo()].PartOffset,
3056 VA.getLocMemOffset(),
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003057 Flags.getByValSize(),
Oliver Stannardd55e1152014-03-05 15:25:27 +00003058 true /*force mutable frames*/,
3059 ByValStoreOffset,
3060 TotalArgRegsSaveSize);
3061 ByValStoreOffset += Flags.getByValSize();
3062 ByValStoreOffset = std::min(ByValStoreOffset, 16U);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003063 InVals.push_back(DAG.getFrameIndex(FrameIndex, getPointerTy()));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003064 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003065 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003066 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003067 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003068 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003069
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003070 // Create load nodes to retrieve arguments from the stack.
3071 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3072 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
3073 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003074 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003075 }
3076 lastInsIndex = index;
3077 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003078 }
3079 }
3080
3081 // varargs
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003082 if (isVarArg)
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003083 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003084 CCInfo.getNextStackOffset(),
3085 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003086
Oliver Stannardb14c6252014-04-02 16:10:33 +00003087 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3088
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003089 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003090}
3091
3092/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003093static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003094 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003095 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003096 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003097 // Maybe this has already been legalized into the constant pool?
3098 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003099 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003100 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003101 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003102 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003103 }
3104 }
3105 return false;
3106}
3107
Evan Cheng10043e22007-01-19 07:51:42 +00003108/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3109/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003110SDValue
3111ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003112 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003113 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003114 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003115 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003116 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003117 // Constant does not fit, try adjusting it by one?
3118 switch (CC) {
3119 default: break;
3120 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003121 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003122 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003123 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003124 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003125 }
3126 break;
3127 case ISD::SETULT:
3128 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003129 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003130 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003131 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003132 }
3133 break;
3134 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003135 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003136 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003137 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003138 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003139 }
3140 break;
3141 case ISD::SETULE:
3142 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003143 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003144 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003145 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003146 }
3147 break;
3148 }
3149 }
3150 }
3151
3152 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003153 ARMISD::NodeType CompareType;
3154 switch (CondCode) {
3155 default:
3156 CompareType = ARMISD::CMP;
3157 break;
3158 case ARMCC::EQ:
3159 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003160 // Uses only Z Flag
3161 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003162 break;
3163 }
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003164 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003165 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003166}
3167
3168/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003169SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003170ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003171 SDLoc dl) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003172 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003173 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003174 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003175 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003176 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3177 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003178}
3179
Bob Wilson45acbd02011-03-08 01:17:20 +00003180/// duplicateCmp - Glue values can have only one use, so this function
3181/// duplicates a comparison node.
3182SDValue
3183ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3184 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003185 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003186 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3187 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3188
3189 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3190 Cmp = Cmp.getOperand(0);
3191 Opc = Cmp.getOpcode();
3192 if (Opc == ARMISD::CMPFP)
3193 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3194 else {
3195 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3196 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3197 }
3198 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3199}
3200
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003201std::pair<SDValue, SDValue>
3202ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3203 SDValue &ARMcc) const {
3204 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3205
3206 SDValue Value, OverflowCmp;
3207 SDValue LHS = Op.getOperand(0);
3208 SDValue RHS = Op.getOperand(1);
3209
3210
3211 // FIXME: We are currently always generating CMPs because we don't support
3212 // generating CMN through the backend. This is not as good as the natural
3213 // CMP case because it causes a register dependency and cannot be folded
3214 // later.
3215
3216 switch (Op.getOpcode()) {
3217 default:
3218 llvm_unreachable("Unknown overflow instruction!");
3219 case ISD::SADDO:
3220 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32);
3221 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS);
3222 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS);
3223 break;
3224 case ISD::UADDO:
3225 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32);
3226 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS);
3227 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS);
3228 break;
3229 case ISD::SSUBO:
3230 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32);
3231 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS);
3232 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS);
3233 break;
3234 case ISD::USUBO:
3235 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32);
3236 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS);
3237 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS);
3238 break;
3239 } // switch (...)
3240
3241 return std::make_pair(Value, OverflowCmp);
3242}
3243
3244
3245SDValue
3246ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3247 // Let legalize expand this if it isn't a legal type yet.
3248 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3249 return SDValue();
3250
3251 SDValue Value, OverflowCmp;
3252 SDValue ARMcc;
3253 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3254 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3255 // We use 0 and 1 as false and true values.
3256 SDValue TVal = DAG.getConstant(1, MVT::i32);
3257 SDValue FVal = DAG.getConstant(0, MVT::i32);
3258 EVT VT = Op.getValueType();
3259
3260 SDValue Overflow = DAG.getNode(ARMISD::CMOV, SDLoc(Op), VT, TVal, FVal,
3261 ARMcc, CCR, OverflowCmp);
3262
3263 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
3264 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), VTs, Value, Overflow);
3265}
3266
3267
Bill Wendling6a981312010-08-11 08:43:16 +00003268SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3269 SDValue Cond = Op.getOperand(0);
3270 SDValue SelectTrue = Op.getOperand(1);
3271 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003272 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003273 unsigned Opc = Cond.getOpcode();
3274
3275 if (Cond.getResNo() == 1 &&
3276 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3277 Opc == ISD::USUBO)) {
3278 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3279 return SDValue();
3280
3281 SDValue Value, OverflowCmp;
3282 SDValue ARMcc;
3283 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3284 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3285 EVT VT = Op.getValueType();
3286
3287 return DAG.getNode(ARMISD::CMOV, SDLoc(Op), VT, SelectTrue, SelectFalse,
3288 ARMcc, CCR, OverflowCmp);
3289
3290 }
Bill Wendling6a981312010-08-11 08:43:16 +00003291
3292 // Convert:
3293 //
3294 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3295 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3296 //
3297 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3298 const ConstantSDNode *CMOVTrue =
3299 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3300 const ConstantSDNode *CMOVFalse =
3301 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3302
3303 if (CMOVTrue && CMOVFalse) {
3304 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3305 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3306
3307 SDValue True;
3308 SDValue False;
3309 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3310 True = SelectTrue;
3311 False = SelectFalse;
3312 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3313 True = SelectFalse;
3314 False = SelectTrue;
3315 }
3316
3317 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003318 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003319 SDValue ARMcc = Cond.getOperand(2);
3320 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003321 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003322 assert(True.getValueType() == VT);
3323 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendling6a981312010-08-11 08:43:16 +00003324 }
3325 }
3326 }
3327
Dan Gohmand4a77c42012-02-24 00:09:36 +00003328 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3329 // undefined bits before doing a full-word comparison with zero.
3330 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
3331 DAG.getConstant(1, Cond.getValueType()));
3332
Bill Wendling6a981312010-08-11 08:43:16 +00003333 return DAG.getSelectCC(dl, Cond,
3334 DAG.getConstant(0, Cond.getValueType()),
3335 SelectTrue, SelectFalse, ISD::SETNE);
3336}
3337
Joey Gouly881eab52013-08-22 15:29:11 +00003338static ISD::CondCode getInverseCCForVSEL(ISD::CondCode CC) {
3339 if (CC == ISD::SETNE)
3340 return ISD::SETEQ;
Weiming Zhao63871d22013-12-18 22:25:17 +00003341 return ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003342}
3343
3344static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3345 bool &swpCmpOps, bool &swpVselOps) {
3346 // Start by selecting the GE condition code for opcodes that return true for
3347 // 'equality'
3348 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3349 CC == ISD::SETULE)
3350 CondCode = ARMCC::GE;
3351
3352 // and GT for opcodes that return false for 'equality'.
3353 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3354 CC == ISD::SETULT)
3355 CondCode = ARMCC::GT;
3356
3357 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3358 // to swap the compare operands.
3359 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3360 CC == ISD::SETULT)
3361 swpCmpOps = true;
3362
3363 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3364 // If we have an unordered opcode, we need to swap the operands to the VSEL
3365 // instruction (effectively negating the condition).
3366 //
3367 // This also has the effect of swapping which one of 'less' or 'greater'
3368 // returns true, so we also swap the compare operands. It also switches
3369 // whether we return true for 'equality', so we compensate by picking the
3370 // opposite condition code to our original choice.
3371 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3372 CC == ISD::SETUGT) {
3373 swpCmpOps = !swpCmpOps;
3374 swpVselOps = !swpVselOps;
3375 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3376 }
3377
3378 // 'ordered' is 'anything but unordered', so use the VS condition code and
3379 // swap the VSEL operands.
3380 if (CC == ISD::SETO) {
3381 CondCode = ARMCC::VS;
3382 swpVselOps = true;
3383 }
3384
3385 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3386 // code and swap the VSEL operands.
3387 if (CC == ISD::SETUNE) {
3388 CondCode = ARMCC::EQ;
3389 swpVselOps = true;
3390 }
3391}
3392
Dan Gohman21cea8a2010-04-17 15:26:15 +00003393SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003394 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003395 SDValue LHS = Op.getOperand(0);
3396 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003397 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003398 SDValue TrueVal = Op.getOperand(2);
3399 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003400 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003401
Owen Anderson9f944592009-08-11 20:47:22 +00003402 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003403 // Try to generate VSEL on ARMv8.
3404 // The VSEL instruction can't use all the usual ARM condition
3405 // codes: it only has two bits to select the condition code, so it's
3406 // constrained to use only GE, GT, VS and EQ.
3407 //
3408 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3409 // swap the operands of the previous compare instruction (effectively
3410 // inverting the compare condition, swapping 'less' and 'greater') and
3411 // sometimes need to swap the operands to the VSEL (which inverts the
3412 // condition in the sense of firing whenever the previous condition didn't)
Joey Goulyccd04892013-09-13 13:46:57 +00003413 if (getSubtarget()->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
Joey Gouly881eab52013-08-22 15:29:11 +00003414 TrueVal.getValueType() == MVT::f64)) {
3415 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3416 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3417 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
3418 CC = getInverseCCForVSEL(CC);
3419 std::swap(TrueVal, FalseVal);
3420 }
3421 }
3422
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003423 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003424 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003425 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Joey Gouly881eab52013-08-22 15:29:11 +00003426 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3427 Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003428 }
3429
3430 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003431 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003432
Joey Gouly881eab52013-08-22 15:29:11 +00003433 // Try to generate VSEL on ARMv8.
Joey Goulyccd04892013-09-13 13:46:57 +00003434 if (getSubtarget()->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
Joey Gouly881eab52013-08-22 15:29:11 +00003435 TrueVal.getValueType() == MVT::f64)) {
Joey Goulye3dd6842013-08-23 12:01:13 +00003436 // We can select VMAXNM/VMINNM from a compare followed by a select with the
3437 // same operands, as follows:
3438 // c = fcmp [ogt, olt, ugt, ult] a, b
3439 // select c, a, b
3440 // We only do this in unsafe-fp-math, because signed zeros and NaNs are
3441 // handled differently than the original code sequence.
3442 if (getTargetMachine().Options.UnsafeFPMath && LHS == TrueVal &&
3443 RHS == FalseVal) {
3444 if (CC == ISD::SETOGT || CC == ISD::SETUGT)
3445 return DAG.getNode(ARMISD::VMAXNM, dl, VT, TrueVal, FalseVal);
3446 if (CC == ISD::SETOLT || CC == ISD::SETULT)
3447 return DAG.getNode(ARMISD::VMINNM, dl, VT, TrueVal, FalseVal);
3448 }
3449
Joey Gouly881eab52013-08-22 15:29:11 +00003450 bool swpCmpOps = false;
3451 bool swpVselOps = false;
3452 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3453
3454 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3455 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3456 if (swpCmpOps)
3457 std::swap(LHS, RHS);
3458 if (swpVselOps)
3459 std::swap(TrueVal, FalseVal);
3460 }
3461 }
3462
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003463 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3464 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003465 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00003466 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003467 ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003468 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003469 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003470 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003471 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson7117a912009-03-20 22:42:55 +00003472 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003473 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Cheng10043e22007-01-19 07:51:42 +00003474 }
3475 return Result;
3476}
3477
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003478/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3479/// to morph to an integer compare sequence.
3480static bool canChangeToInt(SDValue Op, bool &SeenZero,
3481 const ARMSubtarget *Subtarget) {
3482 SDNode *N = Op.getNode();
3483 if (!N->hasOneUse())
3484 // Otherwise it requires moving the value from fp to integer registers.
3485 return false;
3486 if (!N->getNumValues())
3487 return false;
3488 EVT VT = Op.getValueType();
3489 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3490 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3491 // vmrs are very slow, e.g. cortex-a8.
3492 return false;
3493
3494 if (isFloatingPointZero(Op)) {
3495 SeenZero = true;
3496 return true;
3497 }
3498 return ISD::isNormalLoad(N);
3499}
3500
3501static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3502 if (isFloatingPointZero(Op))
3503 return DAG.getConstant(0, MVT::i32);
3504
3505 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003506 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003507 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003508 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003509 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003510
3511 llvm_unreachable("Unknown VFP cmp argument!");
3512}
3513
3514static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3515 SDValue &RetVal1, SDValue &RetVal2) {
3516 if (isFloatingPointZero(Op)) {
3517 RetVal1 = DAG.getConstant(0, MVT::i32);
3518 RetVal2 = DAG.getConstant(0, MVT::i32);
3519 return;
3520 }
3521
3522 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3523 SDValue Ptr = Ld->getBasePtr();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003524 RetVal1 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003525 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003526 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003527 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003528 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003529
3530 EVT PtrType = Ptr.getValueType();
3531 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003532 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003533 PtrType, Ptr, DAG.getConstant(4, PtrType));
Andrew Trickef9de2a2013-05-25 02:42:55 +00003534 RetVal2 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003535 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003536 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003537 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003538 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003539 return;
3540 }
3541
3542 llvm_unreachable("Unknown VFP cmp argument!");
3543}
3544
3545/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3546/// f32 and even f64 comparisons to integer ones.
3547SDValue
3548ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3549 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003550 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003551 SDValue LHS = Op.getOperand(2);
3552 SDValue RHS = Op.getOperand(3);
3553 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003554 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003555
Evan Chengd12af5d2012-03-01 23:27:13 +00003556 bool LHSSeenZero = false;
3557 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3558 bool RHSSeenZero = false;
3559 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3560 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003561 // If unsafe fp math optimization is enabled and there are no other uses of
3562 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003563 // to an integer comparison.
3564 if (CC == ISD::SETOEQ)
3565 CC = ISD::SETEQ;
3566 else if (CC == ISD::SETUNE)
3567 CC = ISD::SETNE;
3568
Evan Chengd12af5d2012-03-01 23:27:13 +00003569 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003570 SDValue ARMcc;
3571 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003572 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3573 bitcastf32Toi32(LHS, DAG), Mask);
3574 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3575 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003576 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3577 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3578 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3579 Chain, Dest, ARMcc, CCR, Cmp);
3580 }
3581
3582 SDValue LHS1, LHS2;
3583 SDValue RHS1, RHS2;
3584 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3585 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003586 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3587 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003588 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3589 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003590 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003591 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003592 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003593 }
3594
3595 return SDValue();
3596}
3597
3598SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3599 SDValue Chain = Op.getOperand(0);
3600 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3601 SDValue LHS = Op.getOperand(2);
3602 SDValue RHS = Op.getOperand(3);
3603 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003604 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003605
Owen Anderson9f944592009-08-11 20:47:22 +00003606 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003607 SDValue ARMcc;
3608 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003609 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003610 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003611 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003612 }
3613
Owen Anderson9f944592009-08-11 20:47:22 +00003614 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003615
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003616 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003617 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3618 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3619 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3620 if (Result.getNode())
3621 return Result;
3622 }
3623
Evan Cheng10043e22007-01-19 07:51:42 +00003624 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003625 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003626
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003627 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3628 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003629 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003630 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003631 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003632 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003633 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003634 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3635 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003636 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003637 }
3638 return Res;
3639}
3640
Dan Gohman21cea8a2010-04-17 15:26:15 +00003641SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003642 SDValue Chain = Op.getOperand(0);
3643 SDValue Table = Op.getOperand(1);
3644 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003645 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003646
Owen Anderson53aa7a92009-08-10 22:56:29 +00003647 EVT PTy = getPointerTy();
Evan Cheng10043e22007-01-19 07:51:42 +00003648 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3649 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3f17aee2009-07-14 18:44:34 +00003650 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003651 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson9f944592009-08-11 20:47:22 +00003652 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chengc8bed032009-07-28 20:53:24 +00003653 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3654 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003655 if (Subtarget->isThumb2()) {
3656 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3657 // which does another jump to the destination. This also makes it easier
3658 // to translate it to TBB / TBH later.
3659 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00003660 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Chengc6d70ae2009-07-29 02:18:14 +00003661 Addr, Op.getOperand(2), JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003662 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00003663 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003664 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattner7727d052010-09-21 06:44:06 +00003665 MachinePointerInfo::getJumpTable(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003666 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003667 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00003668 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson9f944592009-08-11 20:47:22 +00003669 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003670 } else {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003671 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003672 MachinePointerInfo::getJumpTable(),
3673 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003674 Chain = Addr.getValue(1);
Owen Anderson9f944592009-08-11 20:47:22 +00003675 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003676 }
Evan Cheng10043e22007-01-19 07:51:42 +00003677}
3678
Eli Friedman2d4055b2011-11-09 23:36:02 +00003679static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00003680 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003681 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003682
James Molloy547d4c02012-02-20 09:24:05 +00003683 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3684 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3685 return Op;
3686 return DAG.UnrollVectorOp(Op.getNode());
3687 }
3688
3689 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3690 "Invalid type for custom lowering!");
3691 if (VT != MVT::v4i16)
3692 return DAG.UnrollVectorOp(Op.getNode());
3693
3694 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3695 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003696}
3697
Bob Wilsone4191e72010-03-19 22:51:32 +00003698static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman2d4055b2011-11-09 23:36:02 +00003699 EVT VT = Op.getValueType();
3700 if (VT.isVector())
3701 return LowerVectorFP_TO_INT(Op, DAG);
3702
Andrew Trickef9de2a2013-05-25 02:42:55 +00003703 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003704 unsigned Opc;
3705
3706 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003707 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003708 case ISD::FP_TO_SINT:
3709 Opc = ARMISD::FTOSI;
3710 break;
3711 case ISD::FP_TO_UINT:
3712 Opc = ARMISD::FTOUI;
3713 break;
3714 }
3715 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peck527da1b2010-11-23 03:31:01 +00003716 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003717}
3718
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003719static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3720 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003721 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003722
Eli Friedman2d4055b2011-11-09 23:36:02 +00003723 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3724 if (VT.getVectorElementType() == MVT::f32)
3725 return Op;
3726 return DAG.UnrollVectorOp(Op.getNode());
3727 }
3728
Duncan Sandsa41634e2011-08-12 14:54:45 +00003729 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3730 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003731 if (VT != MVT::v4f32)
3732 return DAG.UnrollVectorOp(Op.getNode());
3733
3734 unsigned CastOpc;
3735 unsigned Opc;
3736 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003737 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003738 case ISD::SINT_TO_FP:
3739 CastOpc = ISD::SIGN_EXTEND;
3740 Opc = ISD::SINT_TO_FP;
3741 break;
3742 case ISD::UINT_TO_FP:
3743 CastOpc = ISD::ZERO_EXTEND;
3744 Opc = ISD::UINT_TO_FP;
3745 break;
3746 }
3747
3748 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3749 return DAG.getNode(Opc, dl, VT, Op);
3750}
3751
Bob Wilsone4191e72010-03-19 22:51:32 +00003752static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3753 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003754 if (VT.isVector())
3755 return LowerVectorINT_TO_FP(Op, DAG);
3756
Andrew Trickef9de2a2013-05-25 02:42:55 +00003757 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003758 unsigned Opc;
3759
3760 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003761 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003762 case ISD::SINT_TO_FP:
3763 Opc = ARMISD::SITOF;
3764 break;
3765 case ISD::UINT_TO_FP:
3766 Opc = ARMISD::UITOF;
3767 break;
3768 }
3769
Wesley Peck527da1b2010-11-23 03:31:01 +00003770 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilsone4191e72010-03-19 22:51:32 +00003771 return DAG.getNode(Opc, dl, VT, Op);
3772}
3773
Evan Cheng25f93642010-07-08 02:08:50 +00003774SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00003775 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003776 SDValue Tmp0 = Op.getOperand(0);
3777 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003778 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003779 EVT VT = Op.getValueType();
3780 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00003781 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3782 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3783 bool UseNEON = !InGPR && Subtarget->hasNEON();
3784
3785 if (UseNEON) {
3786 // Use VBSL to copy the sign bit.
3787 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3788 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3789 DAG.getTargetConstant(EncodedVal, MVT::i32));
3790 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3791 if (VT == MVT::f64)
3792 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3793 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3794 DAG.getConstant(32, MVT::i32));
3795 else /*if (VT == MVT::f32)*/
3796 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3797 if (SrcVT == MVT::f32) {
3798 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3799 if (VT == MVT::f64)
3800 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3801 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3802 DAG.getConstant(32, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00003803 } else if (VT == MVT::f32)
3804 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3805 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3806 DAG.getConstant(32, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00003807 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3808 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3809
3810 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3811 MVT::i32);
3812 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3813 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3814 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00003815
Evan Chengd6b641e2011-02-23 02:24:55 +00003816 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3817 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3818 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00003819 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00003820 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3821 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3822 DAG.getConstant(0, MVT::i32));
3823 } else {
3824 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3825 }
3826
3827 return Res;
3828 }
Evan Cheng2da1c952011-02-11 02:28:55 +00003829
3830 // Bitcast operand 1 to i32.
3831 if (SrcVT == MVT::f64)
3832 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00003833 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00003834 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3835
Evan Chengd6b641e2011-02-23 02:24:55 +00003836 // Or in the signbit with integer operations.
3837 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3838 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3839 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3840 if (VT == MVT::f32) {
3841 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3842 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3843 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3844 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00003845 }
3846
Evan Chengd6b641e2011-02-23 02:24:55 +00003847 // f64: Or the high part with signbit and then combine two parts.
3848 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00003849 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00003850 SDValue Lo = Tmp0.getValue(0);
3851 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3852 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3853 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00003854}
3855
Evan Cheng168ced92010-05-22 01:47:14 +00003856SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3857 MachineFunction &MF = DAG.getMachineFunction();
3858 MachineFrameInfo *MFI = MF.getFrameInfo();
3859 MFI->setReturnAddressIsTaken(true);
3860
Bill Wendling908bf812014-01-06 00:43:20 +00003861 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00003862 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00003863
Evan Cheng168ced92010-05-22 01:47:14 +00003864 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003865 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00003866 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3867 if (Depth) {
3868 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3869 SDValue Offset = DAG.getConstant(4, MVT::i32);
3870 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3871 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003872 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00003873 }
3874
3875 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00003876 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00003877 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3878}
3879
Dan Gohman21cea8a2010-04-17 15:26:15 +00003880SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00003881 const ARMBaseRegisterInfo &ARI =
3882 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
3883 MachineFunction &MF = DAG.getMachineFunction();
3884 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003885 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00003886
Owen Anderson53aa7a92009-08-10 22:56:29 +00003887 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003888 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003889 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00003890 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003891 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3892 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00003893 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3894 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003895 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003896 return FrameAddr;
3897}
3898
Renato Golinc7aea402014-05-06 16:51:25 +00003899// FIXME? Maybe this could be a TableGen attribute on some registers and
3900// this table could be generated automatically from RegInfo.
Hal Finkelf0e086a2014-05-11 19:29:07 +00003901unsigned ARMTargetLowering::getRegisterByName(const char* RegName,
3902 EVT VT) const {
Renato Golinc7aea402014-05-06 16:51:25 +00003903 unsigned Reg = StringSwitch<unsigned>(RegName)
3904 .Case("sp", ARM::SP)
3905 .Default(0);
3906 if (Reg)
3907 return Reg;
3908 report_fatal_error("Invalid register name global variable");
3909}
3910
Wesley Peck527da1b2010-11-23 03:31:01 +00003911/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00003912/// expand a bit convert where either the source or destination type is i64 to
3913/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3914/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3915/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00003916static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00003917 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003918 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003919 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00003920
Bob Wilson59b70ea2010-04-17 05:30:19 +00003921 // This function is only supposed to be called for i64 types, either as the
3922 // source or destination of the bit convert.
3923 EVT SrcVT = Op.getValueType();
3924 EVT DstVT = N->getValueType(0);
3925 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00003926 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00003927
Bob Wilson59b70ea2010-04-17 05:30:19 +00003928 // Turn i64->f64 into VMOVDRR.
3929 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson9f944592009-08-11 20:47:22 +00003930 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3931 DAG.getConstant(0, MVT::i32));
3932 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3933 DAG.getConstant(1, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00003934 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00003935 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00003936 }
Bob Wilson7117a912009-03-20 22:42:55 +00003937
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00003938 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00003939 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00003940 SDValue Cvt;
Christian Pirker6692e7c2014-05-14 16:59:44 +00003941 if (TLI.isBigEndian() && SrcVT.isVector() &&
3942 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00003943 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3944 DAG.getVTList(MVT::i32, MVT::i32),
3945 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
3946 else
3947 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3948 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00003949 // Merge the pieces into a single i64 value.
3950 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3951 }
Bob Wilson7117a912009-03-20 22:42:55 +00003952
Bob Wilson59b70ea2010-04-17 05:30:19 +00003953 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00003954}
3955
Bob Wilson2e076c42009-06-22 23:27:02 +00003956/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00003957/// Zero vectors are used to represent vector negation and in those cases
3958/// will be implemented with the NEON VNEG instruction. However, VNEG does
3959/// not support i64 elements, so sometimes the zero vectors will need to be
3960/// explicitly constructed. Regardless, use a canonical VMOV to create the
3961/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003962static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003963 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00003964 // The canonical modified immediate encoding of a zero vector is....0!
3965 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3966 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3967 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00003968 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00003969}
3970
Jim Grosbach624fcb22009-10-31 21:00:56 +00003971/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3972/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00003973SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3974 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00003975 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3976 EVT VT = Op.getValueType();
3977 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003978 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00003979 SDValue ShOpLo = Op.getOperand(0);
3980 SDValue ShOpHi = Op.getOperand(1);
3981 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003982 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00003983 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00003984
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00003985 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3986
Jim Grosbach624fcb22009-10-31 21:00:56 +00003987 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3988 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3989 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3990 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3991 DAG.getConstant(VTBits, MVT::i32));
3992 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3993 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00003994 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00003995
3996 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3997 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003998 ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00003999 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004000 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004001 CCR, Cmp);
4002
4003 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004004 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004005}
4006
Jim Grosbach5d994042009-10-31 19:38:01 +00004007/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4008/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004009SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4010 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004011 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4012 EVT VT = Op.getValueType();
4013 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004014 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004015 SDValue ShOpLo = Op.getOperand(0);
4016 SDValue ShOpHi = Op.getOperand(1);
4017 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004018 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004019
4020 assert(Op.getOpcode() == ISD::SHL_PARTS);
4021 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
4022 DAG.getConstant(VTBits, MVT::i32), ShAmt);
4023 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4024 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
4025 DAG.getConstant(VTBits, MVT::i32));
4026 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4027 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4028
4029 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4030 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4031 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004032 ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004033 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004034 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004035 CCR, Cmp);
4036
4037 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004038 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004039}
4040
Jim Grosbach535d3b42010-09-08 03:54:02 +00004041SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004042 SelectionDAG &DAG) const {
4043 // The rounding mode is in bits 23:22 of the FPSCR.
4044 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4045 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4046 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004047 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004048 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
4049 DAG.getConstant(Intrinsic::arm_get_fpscr,
4050 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004051 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemanb69b1822010-08-03 21:31:55 +00004052 DAG.getConstant(1U << 22, MVT::i32));
4053 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
4054 DAG.getConstant(22, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004055 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemanb69b1822010-08-03 21:31:55 +00004056 DAG.getConstant(3, MVT::i32));
4057}
4058
Jim Grosbach8546ec92010-01-18 19:58:49 +00004059static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4060 const ARMSubtarget *ST) {
4061 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004062 SDLoc dl(N);
Jim Grosbach8546ec92010-01-18 19:58:49 +00004063
4064 if (!ST->hasV6T2Ops())
4065 return SDValue();
4066
4067 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
4068 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4069}
4070
Evan Chengb4eae132012-12-04 22:41:50 +00004071/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4072/// for each 16-bit element from operand, repeated. The basic idea is to
4073/// leverage vcnt to get the 8-bit counts, gather and add the results.
4074///
4075/// Trace for v4i16:
4076/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4077/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4078/// 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 +00004079/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004080/// [b0 b1 b2 b3 b4 b5 b6 b7]
4081/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4082/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4083/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4084static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4085 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004086 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004087
4088 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4089 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4090 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4091 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4092 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4093 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4094}
4095
4096/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4097/// bit-count for each 16-bit element from the operand. We need slightly
4098/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4099/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004100///
Evan Chengb4eae132012-12-04 22:41:50 +00004101/// Trace for v4i16:
4102/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4103/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4104/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4105/// v4i16:Extracted = [k0 k1 k2 k3 ]
4106static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4107 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004108 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004109
4110 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4111 if (VT.is64BitVector()) {
4112 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4113 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
4114 DAG.getIntPtrConstant(0));
4115 } else {
4116 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
4117 BitCounts, DAG.getIntPtrConstant(0));
4118 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4119 }
4120}
4121
4122/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4123/// bit-count for each 32-bit element from the operand. The idea here is
4124/// to split the vector into 16-bit elements, leverage the 16-bit count
4125/// routine, and then combine the results.
4126///
4127/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4128/// input = [v0 v1 ] (vi: 32-bit elements)
4129/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4130/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004131/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004132/// [k0 k1 k2 k3 ]
4133/// N1 =+[k1 k0 k3 k2 ]
4134/// [k0 k2 k1 k3 ]
4135/// N2 =+[k1 k3 k0 k2 ]
4136/// [k0 k2 k1 k3 ]
4137/// Extended =+[k1 k3 k0 k2 ]
4138/// [k0 k2 ]
4139/// Extracted=+[k1 k3 ]
4140///
4141static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4142 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004143 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004144
4145 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4146
4147 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4148 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4149 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4150 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4151 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4152
4153 if (VT.is64BitVector()) {
4154 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4155 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
4156 DAG.getIntPtrConstant(0));
4157 } else {
4158 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
4159 DAG.getIntPtrConstant(0));
4160 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4161 }
4162}
4163
4164static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4165 const ARMSubtarget *ST) {
4166 EVT VT = N->getValueType(0);
4167
4168 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004169 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4170 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004171 "Unexpected type for custom ctpop lowering");
4172
4173 if (VT.getVectorElementType() == MVT::i32)
4174 return lowerCTPOP32BitElements(N, DAG);
4175 else
4176 return lowerCTPOP16BitElements(N, DAG);
4177}
4178
Bob Wilson2e076c42009-06-22 23:27:02 +00004179static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4180 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004181 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004182 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004183
Bob Wilson7d471332010-11-18 21:16:28 +00004184 if (!VT.isVector())
4185 return SDValue();
4186
Bob Wilson2e076c42009-06-22 23:27:02 +00004187 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004188 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004189
Bob Wilson7d471332010-11-18 21:16:28 +00004190 // Left shifts translate directly to the vshiftu intrinsic.
4191 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004192 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilson7d471332010-11-18 21:16:28 +00004193 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
4194 N->getOperand(0), N->getOperand(1));
4195
4196 assert((N->getOpcode() == ISD::SRA ||
4197 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4198
4199 // NEON uses the same intrinsics for both left and right shifts. For
4200 // right shifts, the shift amounts are negative, so negate the vector of
4201 // shift amounts.
4202 EVT ShiftVT = N->getOperand(1).getValueType();
4203 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4204 getZeroVector(ShiftVT, DAG, dl),
4205 N->getOperand(1));
4206 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4207 Intrinsic::arm_neon_vshifts :
4208 Intrinsic::arm_neon_vshiftu);
4209 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4210 DAG.getConstant(vshiftInt, MVT::i32),
4211 N->getOperand(0), NegatedCount);
4212}
4213
4214static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4215 const ARMSubtarget *ST) {
4216 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004217 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004218
Eli Friedman682d8c12009-08-22 03:13:10 +00004219 // We can get here for a node like i32 = ISD::SHL i32, i64
4220 if (VT != MVT::i64)
4221 return SDValue();
4222
4223 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004224 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004225
Chris Lattnerf81d5882007-11-24 07:07:01 +00004226 // We only lower SRA, SRL of 1 here, all others use generic lowering.
4227 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmaneffb8942008-09-12 16:56:44 +00004228 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands6ed40142008-12-01 11:39:25 +00004229 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004230
Chris Lattnerf81d5882007-11-24 07:07:01 +00004231 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004232 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004233
Chris Lattnerf81d5882007-11-24 07:07:01 +00004234 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004235 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004236 DAG.getConstant(0, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004237 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004238 DAG.getConstant(1, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004239
Chris Lattnerf81d5882007-11-24 07:07:01 +00004240 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4241 // captures the result into a carry flag.
4242 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004243 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004244
Chris Lattnerf81d5882007-11-24 07:07:01 +00004245 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004246 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004247
Chris Lattnerf81d5882007-11-24 07:07:01 +00004248 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004249 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004250}
4251
Bob Wilson2e076c42009-06-22 23:27:02 +00004252static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4253 SDValue TmpOp0, TmpOp1;
4254 bool Invert = false;
4255 bool Swap = false;
4256 unsigned Opc = 0;
4257
4258 SDValue Op0 = Op.getOperand(0);
4259 SDValue Op1 = Op.getOperand(1);
4260 SDValue CC = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004261 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004262 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004263 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004264
4265 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
4266 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004267 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004268 case ISD::SETUNE:
4269 case ISD::SETNE: Invert = true; // Fallthrough
4270 case ISD::SETOEQ:
4271 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4272 case ISD::SETOLT:
4273 case ISD::SETLT: Swap = true; // Fallthrough
4274 case ISD::SETOGT:
4275 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4276 case ISD::SETOLE:
4277 case ISD::SETLE: Swap = true; // Fallthrough
4278 case ISD::SETOGE:
4279 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4280 case ISD::SETUGE: Swap = true; // Fallthrough
4281 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4282 case ISD::SETUGT: Swap = true; // Fallthrough
4283 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4284 case ISD::SETUEQ: Invert = true; // Fallthrough
4285 case ISD::SETONE:
4286 // Expand this to (OLT | OGT).
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::VCGT, dl, VT, TmpOp0, TmpOp1);
4292 break;
4293 case ISD::SETUO: Invert = true; // Fallthrough
4294 case ISD::SETO:
4295 // Expand this to (OLT | OGE).
4296 TmpOp0 = Op0;
4297 TmpOp1 = Op1;
4298 Opc = ISD::OR;
4299 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
4300 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
4301 break;
4302 }
4303 } else {
4304 // Integer comparisons.
4305 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004306 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004307 case ISD::SETNE: Invert = true;
4308 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4309 case ISD::SETLT: Swap = true;
4310 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4311 case ISD::SETLE: Swap = true;
4312 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4313 case ISD::SETULT: Swap = true;
4314 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4315 case ISD::SETULE: Swap = true;
4316 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4317 }
4318
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004319 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004320 if (Opc == ARMISD::VCEQ) {
4321
4322 SDValue AndOp;
4323 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4324 AndOp = Op0;
4325 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4326 AndOp = Op1;
4327
4328 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004329 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004330 AndOp = AndOp.getOperand(0);
4331
4332 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4333 Opc = ARMISD::VTST;
Wesley Peck527da1b2010-11-23 03:31:01 +00004334 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
4335 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004336 Invert = !Invert;
4337 }
4338 }
4339 }
4340
4341 if (Swap)
4342 std::swap(Op0, Op1);
4343
Owen Andersonc7baee32010-11-08 23:21:22 +00004344 // If one of the operands is a constant vector zero, attempt to fold the
4345 // comparison to a specialized compare-against-zero form.
4346 SDValue SingleOp;
4347 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4348 SingleOp = Op0;
4349 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4350 if (Opc == ARMISD::VCGE)
4351 Opc = ARMISD::VCLEZ;
4352 else if (Opc == ARMISD::VCGT)
4353 Opc = ARMISD::VCLTZ;
4354 SingleOp = Op1;
4355 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004356
Owen Andersonc7baee32010-11-08 23:21:22 +00004357 SDValue Result;
4358 if (SingleOp.getNode()) {
4359 switch (Opc) {
4360 case ARMISD::VCEQ:
4361 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
4362 case ARMISD::VCGE:
4363 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
4364 case ARMISD::VCLEZ:
4365 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
4366 case ARMISD::VCGT:
4367 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
4368 case ARMISD::VCLTZ:
4369 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
4370 default:
4371 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
4372 }
4373 } else {
4374 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
4375 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004376
4377 if (Invert)
4378 Result = DAG.getNOT(dl, Result, VT);
4379
4380 return Result;
4381}
4382
Bob Wilson5b2b5042010-06-14 22:19:57 +00004383/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4384/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004385/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004386static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4387 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Andersona4076922010-11-05 21:57:54 +00004388 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004389 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004390
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004391 // SplatBitSize is set to the smallest size that splats the vector, so a
4392 // zero vector will always have SplatBitSize == 8. However, NEON modified
4393 // immediate instructions others than VMOV do not support the 8-bit encoding
4394 // of a zero vector, and the default encoding of zero is supposed to be the
4395 // 32-bit version.
4396 if (SplatBits == 0)
4397 SplatBitSize = 32;
4398
Bob Wilson2e076c42009-06-22 23:27:02 +00004399 switch (SplatBitSize) {
4400 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004401 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004402 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004403 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004404 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004405 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004406 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004407 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004408 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004409
4410 case 16:
4411 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004412 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004413 if ((SplatBits & ~0xff) == 0) {
4414 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004415 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004416 Imm = SplatBits;
4417 break;
4418 }
4419 if ((SplatBits & ~0xff00) == 0) {
4420 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004421 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004422 Imm = SplatBits >> 8;
4423 break;
4424 }
4425 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004426
4427 case 32:
4428 // NEON's 32-bit VMOV supports splat values where:
4429 // * only one byte is nonzero, or
4430 // * the least significant byte is 0xff and the second byte is nonzero, or
4431 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004432 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004433 if ((SplatBits & ~0xff) == 0) {
4434 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004435 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004436 Imm = SplatBits;
4437 break;
4438 }
4439 if ((SplatBits & ~0xff00) == 0) {
4440 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004441 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004442 Imm = SplatBits >> 8;
4443 break;
4444 }
4445 if ((SplatBits & ~0xff0000) == 0) {
4446 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004447 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004448 Imm = SplatBits >> 16;
4449 break;
4450 }
4451 if ((SplatBits & ~0xff000000) == 0) {
4452 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004453 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004454 Imm = SplatBits >> 24;
4455 break;
4456 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004457
Owen Andersona4076922010-11-05 21:57:54 +00004458 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4459 if (type == OtherModImm) return SDValue();
4460
Bob Wilson2e076c42009-06-22 23:27:02 +00004461 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004462 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4463 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004464 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004465 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004466 break;
4467 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004468
4469 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004470 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4471 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004472 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004473 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004474 break;
4475 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004476
4477 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4478 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4479 // VMOV.I32. A (very) minor optimization would be to replicate the value
4480 // and fall through here to test for a valid 64-bit splat. But, then the
4481 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004482 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004483
4484 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004485 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004486 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004487 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00004488 uint64_t BitMask = 0xff;
4489 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004490 unsigned ImmMask = 1;
4491 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00004492 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00004493 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004494 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00004495 Imm |= ImmMask;
4496 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004497 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004498 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004499 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004500 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00004501 }
Bob Wilson6eae5202010-06-11 21:34:50 +00004502 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004503 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00004504 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00004505 break;
4506 }
4507
Bob Wilson6eae5202010-06-11 21:34:50 +00004508 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00004509 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00004510 }
4511
Bob Wilsona3f19012010-07-13 21:16:48 +00004512 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
4513 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00004514}
4515
Lang Hames591cdaf2012-03-29 21:56:11 +00004516SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4517 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004518 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00004519 return SDValue();
4520
Tim Northoverf79c3a52013-08-20 08:57:11 +00004521 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00004522 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004523
4524 // Try splatting with a VMOV.f32...
4525 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00004526 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
4527
Lang Hames591cdaf2012-03-29 21:56:11 +00004528 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004529 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
4530 // We have code in place to select a valid ConstantFP already, no need to
4531 // do any mangling.
4532 return Op;
4533 }
4534
4535 // It's a float and we are trying to use NEON operations where
4536 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004537 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004538 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
4539 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4540 NewVal);
4541 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
4542 DAG.getConstant(0, MVT::i32));
4543 }
4544
Tim Northoverf79c3a52013-08-20 08:57:11 +00004545 // The rest of our options are NEON only, make sure that's allowed before
4546 // proceeding..
4547 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
4548 return SDValue();
4549
Lang Hames591cdaf2012-03-29 21:56:11 +00004550 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00004551 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
4552
4553 // It wouldn't really be worth bothering for doubles except for one very
4554 // important value, which does happen to match: 0.0. So make sure we don't do
4555 // anything stupid.
4556 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
4557 return SDValue();
4558
4559 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
4560 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4561 false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004562 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004563 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004564 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4565 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004566 if (IsDouble)
4567 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4568
4569 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004570 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4571 VecConstant);
4572 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4573 DAG.getConstant(0, MVT::i32));
4574 }
4575
4576 // Finally, try a VMVN.i32
Tim Northoverf79c3a52013-08-20 08:57:11 +00004577 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4578 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004579 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004580 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004581 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004582
4583 if (IsDouble)
4584 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4585
4586 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004587 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4588 VecConstant);
4589 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4590 DAG.getConstant(0, MVT::i32));
4591 }
4592
4593 return SDValue();
4594}
4595
Quentin Colombet8e1fe842012-11-02 21:32:17 +00004596// check if an VEXT instruction can handle the shuffle mask when the
4597// vector sources of the shuffle are the same.
4598static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4599 unsigned NumElts = VT.getVectorNumElements();
4600
4601 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4602 if (M[0] < 0)
4603 return false;
4604
4605 Imm = M[0];
4606
4607 // If this is a VEXT shuffle, the immediate value is the index of the first
4608 // element. The other shuffle indices must be the successive elements after
4609 // the first one.
4610 unsigned ExpectedElt = Imm;
4611 for (unsigned i = 1; i < NumElts; ++i) {
4612 // Increment the expected index. If it wraps around, just follow it
4613 // back to index zero and keep going.
4614 ++ExpectedElt;
4615 if (ExpectedElt == NumElts)
4616 ExpectedElt = 0;
4617
4618 if (M[i] < 0) continue; // ignore UNDEF indices
4619 if (ExpectedElt != static_cast<unsigned>(M[i]))
4620 return false;
4621 }
4622
4623 return true;
4624}
4625
Lang Hames591cdaf2012-03-29 21:56:11 +00004626
Benjamin Kramer339ced42012-01-15 13:16:05 +00004627static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004628 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004629 unsigned NumElts = VT.getVectorNumElements();
4630 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00004631
4632 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4633 if (M[0] < 0)
4634 return false;
4635
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004636 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00004637
4638 // If this is a VEXT shuffle, the immediate value is the index of the first
4639 // element. The other shuffle indices must be the successive elements after
4640 // the first one.
4641 unsigned ExpectedElt = Imm;
4642 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004643 // Increment the expected index. If it wraps around, it may still be
4644 // a VEXT but the source vectors must be swapped.
4645 ExpectedElt += 1;
4646 if (ExpectedElt == NumElts * 2) {
4647 ExpectedElt = 0;
4648 ReverseVEXT = true;
4649 }
4650
Bob Wilson411dfad2010-08-17 05:54:34 +00004651 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004652 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00004653 return false;
4654 }
4655
4656 // Adjust the index value if the source operands will be swapped.
4657 if (ReverseVEXT)
4658 Imm -= NumElts;
4659
Bob Wilson32cd8552009-08-19 17:03:43 +00004660 return true;
4661}
4662
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004663/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4664/// instruction with the specified blocksize. (The order of the elements
4665/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00004666static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004667 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4668 "Only possible block sizes for VREV are: 16, 32, 64");
4669
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004670 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00004671 if (EltSz == 64)
4672 return false;
4673
4674 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004675 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00004676 // If the first shuffle index is UNDEF, be optimistic.
4677 if (M[0] < 0)
4678 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004679
4680 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4681 return false;
4682
4683 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004684 if (M[i] < 0) continue; // ignore UNDEF indices
4685 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004686 return false;
4687 }
4688
4689 return true;
4690}
4691
Benjamin Kramer339ced42012-01-15 13:16:05 +00004692static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00004693 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
4694 // range, then 0 is placed into the resulting vector. So pretty much any mask
4695 // of 8 elements can work here.
4696 return VT == MVT::v8i8 && M.size() == 8;
4697}
4698
Benjamin Kramer339ced42012-01-15 13:16:05 +00004699static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004700 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4701 if (EltSz == 64)
4702 return false;
4703
Bob Wilsona7062312009-08-21 20:54:19 +00004704 unsigned NumElts = VT.getVectorNumElements();
4705 WhichResult = (M[0] == 0 ? 0 : 1);
4706 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004707 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4708 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsona7062312009-08-21 20:54:19 +00004709 return false;
4710 }
4711 return true;
4712}
4713
Bob Wilson0bbd3072009-12-03 06:40:55 +00004714/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
4715/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4716/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004717static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004718 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4719 if (EltSz == 64)
4720 return false;
4721
4722 unsigned NumElts = VT.getVectorNumElements();
4723 WhichResult = (M[0] == 0 ? 0 : 1);
4724 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004725 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4726 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004727 return false;
4728 }
4729 return true;
4730}
4731
Benjamin Kramer339ced42012-01-15 13:16:05 +00004732static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004733 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4734 if (EltSz == 64)
4735 return false;
4736
Bob Wilsona7062312009-08-21 20:54:19 +00004737 unsigned NumElts = VT.getVectorNumElements();
4738 WhichResult = (M[0] == 0 ? 0 : 1);
4739 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004740 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsona7062312009-08-21 20:54:19 +00004741 if ((unsigned) M[i] != 2 * i + WhichResult)
4742 return false;
4743 }
4744
4745 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004746 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004747 return false;
4748
4749 return true;
4750}
4751
Bob Wilson0bbd3072009-12-03 06:40:55 +00004752/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4753/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4754/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00004755static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004756 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4757 if (EltSz == 64)
4758 return false;
4759
4760 unsigned Half = VT.getVectorNumElements() / 2;
4761 WhichResult = (M[0] == 0 ? 0 : 1);
4762 for (unsigned j = 0; j != 2; ++j) {
4763 unsigned Idx = WhichResult;
4764 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004765 int MIdx = M[i + j * Half];
4766 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson0bbd3072009-12-03 06:40:55 +00004767 return false;
4768 Idx += 2;
4769 }
4770 }
4771
4772 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4773 if (VT.is64BitVector() && EltSz == 32)
4774 return false;
4775
4776 return true;
4777}
4778
Benjamin Kramer339ced42012-01-15 13:16:05 +00004779static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004780 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4781 if (EltSz == 64)
4782 return false;
4783
Bob Wilsona7062312009-08-21 20:54:19 +00004784 unsigned NumElts = VT.getVectorNumElements();
4785 WhichResult = (M[0] == 0 ? 0 : 1);
4786 unsigned Idx = WhichResult * NumElts / 2;
4787 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004788 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4789 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsona7062312009-08-21 20:54:19 +00004790 return false;
4791 Idx += 1;
4792 }
4793
4794 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004795 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004796 return false;
4797
4798 return true;
4799}
4800
Bob Wilson0bbd3072009-12-03 06:40:55 +00004801/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4802/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4803/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004804static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004805 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4806 if (EltSz == 64)
4807 return false;
4808
4809 unsigned NumElts = VT.getVectorNumElements();
4810 WhichResult = (M[0] == 0 ? 0 : 1);
4811 unsigned Idx = WhichResult * NumElts / 2;
4812 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004813 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4814 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004815 return false;
4816 Idx += 1;
4817 }
4818
4819 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4820 if (VT.is64BitVector() && EltSz == 32)
4821 return false;
4822
4823 return true;
4824}
4825
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00004826/// \return true if this is a reverse operation on an vector.
4827static bool isReverseMask(ArrayRef<int> M, EVT VT) {
4828 unsigned NumElts = VT.getVectorNumElements();
4829 // Make sure the mask has the right size.
4830 if (NumElts != M.size())
4831 return false;
4832
4833 // Look for <15, ..., 3, -1, 1, 0>.
4834 for (unsigned i = 0; i != NumElts; ++i)
4835 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
4836 return false;
4837
4838 return true;
4839}
4840
Dale Johannesen2bff5052010-07-29 20:10:08 +00004841// If N is an integer constant that can be moved into a register in one
4842// instruction, return an SDValue of such a constant (will become a MOV
4843// instruction). Otherwise return null.
4844static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004845 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00004846 uint64_t Val;
4847 if (!isa<ConstantSDNode>(N))
4848 return SDValue();
4849 Val = cast<ConstantSDNode>(N)->getZExtValue();
4850
4851 if (ST->isThumb1Only()) {
4852 if (Val <= 255 || ~Val <= 255)
4853 return DAG.getConstant(Val, MVT::i32);
4854 } else {
4855 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4856 return DAG.getConstant(Val, MVT::i32);
4857 }
4858 return SDValue();
4859}
4860
Bob Wilson2e076c42009-06-22 23:27:02 +00004861// If this is a case we can't handle, return null and let the default
4862// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00004863SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4864 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00004865 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004866 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004867 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004868
4869 APInt SplatBits, SplatUndef;
4870 unsigned SplatBitSize;
4871 bool HasAnyUndefs;
4872 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00004873 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00004874 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00004875 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00004876 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00004877 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00004878 DAG, VmovVT, VT.is128BitVector(),
4879 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00004880 if (Val.getNode()) {
4881 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00004882 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00004883 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00004884
4885 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00004886 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004887 Val = isNEONModifiedImm(NegatedImm,
4888 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00004889 DAG, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00004890 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00004891 if (Val.getNode()) {
4892 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00004893 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00004894 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00004895
4896 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00004897 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00004898 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00004899 if (ImmVal != -1) {
4900 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4901 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4902 }
4903 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00004904 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00004905 }
4906
Bob Wilson91fdf682010-05-22 00:23:12 +00004907 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00004908 //
4909 // As an optimisation, even if more than one value is used it may be more
4910 // profitable to splat with one value then change some lanes.
4911 //
4912 // Heuristically we decide to do this if the vector has a "dominant" value,
4913 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00004914 unsigned NumElts = VT.getVectorNumElements();
4915 bool isOnlyLowElement = true;
4916 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00004917 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00004918 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00004919
4920 // Map of the number of times a particular SDValue appears in the
4921 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00004922 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00004923 SDValue Value;
4924 for (unsigned i = 0; i < NumElts; ++i) {
4925 SDValue V = Op.getOperand(i);
4926 if (V.getOpcode() == ISD::UNDEF)
4927 continue;
4928 if (i > 0)
4929 isOnlyLowElement = false;
4930 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4931 isConstant = false;
4932
James Molloy49bdbce2012-09-06 09:55:02 +00004933 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00004934 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00004935
James Molloy49bdbce2012-09-06 09:55:02 +00004936 // Is this value dominant? (takes up more than half of the lanes)
4937 if (++Count > (NumElts / 2)) {
4938 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00004939 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00004940 }
Bob Wilson91fdf682010-05-22 00:23:12 +00004941 }
James Molloy49bdbce2012-09-06 09:55:02 +00004942 if (ValueCounts.size() != 1)
4943 usesOnlyOneValue = false;
4944 if (!Value.getNode() && ValueCounts.size() > 0)
4945 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00004946
James Molloy49bdbce2012-09-06 09:55:02 +00004947 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00004948 return DAG.getUNDEF(VT);
4949
Quentin Colombet0f2fe742013-07-23 22:34:47 +00004950 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
4951 // Keep going if we are hitting this case.
4952 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00004953 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4954
Dale Johannesen2bff5052010-07-29 20:10:08 +00004955 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4956
Dale Johannesen710a2d92010-10-19 20:00:17 +00004957 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4958 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00004959 if (hasDominantValue && EltSize <= 32) {
4960 if (!isConstant) {
4961 SDValue N;
4962
4963 // If we are VDUPing a value that comes directly from a vector, that will
4964 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00004965 // just use VDUPLANE. We can only do this if the lane being extracted
4966 // is at a constant index, as the VDUP from lane instructions only have
4967 // constant-index forms.
4968 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4969 isa<ConstantSDNode>(Value->getOperand(1))) {
Silviu Barangab1409702012-10-15 09:41:32 +00004970 // We need to create a new undef vector to use for the VDUPLANE if the
4971 // size of the vector from which we get the value is different than the
4972 // size of the vector that we need to create. We will insert the element
4973 // such that the register coalescer will remove unnecessary copies.
4974 if (VT != Value->getOperand(0).getValueType()) {
4975 ConstantSDNode *constIndex;
4976 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
4977 assert(constIndex && "The index is not a constant!");
4978 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
4979 VT.getVectorNumElements();
4980 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
4981 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
4982 Value, DAG.getConstant(index, MVT::i32)),
4983 DAG.getConstant(index, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00004984 } else
Silviu Barangab1409702012-10-15 09:41:32 +00004985 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00004986 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00004987 } else
James Molloy49bdbce2012-09-06 09:55:02 +00004988 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4989
4990 if (!usesOnlyOneValue) {
4991 // The dominant value was splatted as 'N', but we now have to insert
4992 // all differing elements.
4993 for (unsigned I = 0; I < NumElts; ++I) {
4994 if (Op.getOperand(I) == Value)
4995 continue;
4996 SmallVector<SDValue, 3> Ops;
4997 Ops.push_back(N);
4998 Ops.push_back(Op.getOperand(I));
4999 Ops.push_back(DAG.getConstant(I, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00005000 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00005001 }
5002 }
5003 return N;
5004 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00005005 if (VT.getVectorElementType().isFloatingPoint()) {
5006 SmallVector<SDValue, 8> Ops;
5007 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005008 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005009 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005010 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00005011 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005012 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5013 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005014 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005015 }
James Molloy49bdbce2012-09-06 09:55:02 +00005016 if (usesOnlyOneValue) {
5017 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5018 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005019 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005020 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005021 }
5022
5023 // If all elements are constants and the case above didn't get hit, fall back
5024 // to the default expansion, which will generate a load from the constant
5025 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005026 if (isConstant)
5027 return SDValue();
5028
Bob Wilson6f2b8962011-01-07 21:37:30 +00005029 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5030 if (NumElts >= 4) {
5031 SDValue shuffle = ReconstructShuffle(Op, DAG);
5032 if (shuffle != SDValue())
5033 return shuffle;
5034 }
5035
Bob Wilson91fdf682010-05-22 00:23:12 +00005036 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005037 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5038 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005039 if (EltSize >= 32) {
5040 // Do the expansion with floating-point types, since that is what the VFP
5041 // registers are defined to use, and since i64 is not legal.
5042 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5043 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005044 SmallVector<SDValue, 8> Ops;
5045 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005046 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005047 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005048 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005049 }
5050
Jim Grosbach24e102a2013-07-08 18:18:52 +00005051 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5052 // know the default expansion would otherwise fall back on something even
5053 // worse. For a vector with one or two non-undef values, that's
5054 // scalar_to_vector for the elements followed by a shuffle (provided the
5055 // shuffle is valid for the target) and materialization element by element
5056 // on the stack followed by a load for everything else.
5057 if (!isConstant && !usesOnlyOneValue) {
5058 SDValue Vec = DAG.getUNDEF(VT);
5059 for (unsigned i = 0 ; i < NumElts; ++i) {
5060 SDValue V = Op.getOperand(i);
5061 if (V.getOpcode() == ISD::UNDEF)
5062 continue;
5063 SDValue LaneIdx = DAG.getConstant(i, MVT::i32);
5064 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5065 }
5066 return Vec;
5067 }
5068
Bob Wilson2e076c42009-06-22 23:27:02 +00005069 return SDValue();
5070}
5071
Bob Wilson6f2b8962011-01-07 21:37:30 +00005072// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005073// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005074SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5075 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005076 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005077 EVT VT = Op.getValueType();
5078 unsigned NumElts = VT.getVectorNumElements();
5079
5080 SmallVector<SDValue, 2> SourceVecs;
5081 SmallVector<unsigned, 2> MinElts;
5082 SmallVector<unsigned, 2> MaxElts;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005083
Bob Wilson6f2b8962011-01-07 21:37:30 +00005084 for (unsigned i = 0; i < NumElts; ++i) {
5085 SDValue V = Op.getOperand(i);
5086 if (V.getOpcode() == ISD::UNDEF)
5087 continue;
5088 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5089 // A shuffle can only come from building a vector from various
5090 // elements of other vectors.
5091 return SDValue();
Eli Friedman74d1da52011-10-14 23:58:49 +00005092 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
5093 VT.getVectorElementType()) {
5094 // This code doesn't know how to handle shuffles where the vector
5095 // element types do not match (this happens because type legalization
5096 // promotes the return type of EXTRACT_VECTOR_ELT).
5097 // FIXME: It might be appropriate to extend this code to handle
5098 // mismatched types.
5099 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005100 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005101
Bob Wilson6f2b8962011-01-07 21:37:30 +00005102 // Record this extraction against the appropriate vector if possible...
5103 SDValue SourceVec = V.getOperand(0);
Jim Grosbach6df755c2012-07-25 17:02:47 +00005104 // If the element number isn't a constant, we can't effectively
5105 // analyze what's going on.
5106 if (!isa<ConstantSDNode>(V.getOperand(1)))
5107 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005108 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5109 bool FoundSource = false;
5110 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
5111 if (SourceVecs[j] == SourceVec) {
5112 if (MinElts[j] > EltNo)
5113 MinElts[j] = EltNo;
5114 if (MaxElts[j] < EltNo)
5115 MaxElts[j] = EltNo;
5116 FoundSource = true;
5117 break;
5118 }
5119 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005120
Bob Wilson6f2b8962011-01-07 21:37:30 +00005121 // Or record a new source if not...
5122 if (!FoundSource) {
5123 SourceVecs.push_back(SourceVec);
5124 MinElts.push_back(EltNo);
5125 MaxElts.push_back(EltNo);
5126 }
5127 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005128
Bob Wilson6f2b8962011-01-07 21:37:30 +00005129 // Currently only do something sane when at most two source vectors
5130 // involved.
5131 if (SourceVecs.size() > 2)
5132 return SDValue();
5133
5134 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
5135 int VEXTOffsets[2] = {0, 0};
Andrew Trick5eb0a302011-01-19 02:26:13 +00005136
Bob Wilson6f2b8962011-01-07 21:37:30 +00005137 // This loop extracts the usage patterns of the source vectors
5138 // and prepares appropriate SDValues for a shuffle if possible.
5139 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
5140 if (SourceVecs[i].getValueType() == VT) {
5141 // No VEXT necessary
5142 ShuffleSrcs[i] = SourceVecs[i];
5143 VEXTOffsets[i] = 0;
5144 continue;
5145 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
5146 // It probably isn't worth padding out a smaller vector just to
5147 // break it down again in a shuffle.
5148 return SDValue();
5149 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005150
Bob Wilson6f2b8962011-01-07 21:37:30 +00005151 // Since only 64-bit and 128-bit vectors are legal on ARM and
5152 // we've eliminated the other cases...
Bob Wilson3fa9c062011-01-07 23:40:46 +00005153 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
5154 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick5eb0a302011-01-19 02:26:13 +00005155
Bob Wilson6f2b8962011-01-07 21:37:30 +00005156 if (MaxElts[i] - MinElts[i] >= NumElts) {
5157 // Span too large for a VEXT to cope
5158 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005159 }
5160
Bob Wilson6f2b8962011-01-07 21:37:30 +00005161 if (MinElts[i] >= NumElts) {
5162 // The extraction can just take the second half
5163 VEXTOffsets[i] = NumElts;
Eric Christopher2af95512011-01-14 23:50:53 +00005164 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5165 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005166 DAG.getIntPtrConstant(NumElts));
5167 } else if (MaxElts[i] < NumElts) {
5168 // The extraction can just take the first half
5169 VEXTOffsets[i] = 0;
Eric Christopher2af95512011-01-14 23:50:53 +00005170 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5171 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005172 DAG.getIntPtrConstant(0));
5173 } else {
5174 // An actual VEXT is needed
5175 VEXTOffsets[i] = MinElts[i];
Eric Christopher2af95512011-01-14 23:50:53 +00005176 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5177 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005178 DAG.getIntPtrConstant(0));
Eric Christopher2af95512011-01-14 23:50:53 +00005179 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5180 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005181 DAG.getIntPtrConstant(NumElts));
5182 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
5183 DAG.getConstant(VEXTOffsets[i], MVT::i32));
5184 }
5185 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005186
Bob Wilson6f2b8962011-01-07 21:37:30 +00005187 SmallVector<int, 8> Mask;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005188
Bob Wilson6f2b8962011-01-07 21:37:30 +00005189 for (unsigned i = 0; i < NumElts; ++i) {
5190 SDValue Entry = Op.getOperand(i);
5191 if (Entry.getOpcode() == ISD::UNDEF) {
5192 Mask.push_back(-1);
5193 continue;
5194 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005195
Bob Wilson6f2b8962011-01-07 21:37:30 +00005196 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher2af95512011-01-14 23:50:53 +00005197 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
5198 .getOperand(1))->getSExtValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005199 if (ExtractVec == SourceVecs[0]) {
5200 Mask.push_back(ExtractElt - VEXTOffsets[0]);
5201 } else {
5202 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
5203 }
5204 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005205
Bob Wilson6f2b8962011-01-07 21:37:30 +00005206 // Final check before we try to produce nonsense...
5207 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher2af95512011-01-14 23:50:53 +00005208 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
5209 &Mask[0]);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005210
Bob Wilson6f2b8962011-01-07 21:37:30 +00005211 return SDValue();
5212}
5213
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005214/// isShuffleMaskLegal - Targets can use this to indicate that they only
5215/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5216/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5217/// are assumed to be legal.
5218bool
5219ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5220 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005221 if (VT.getVectorNumElements() == 4 &&
5222 (VT.is128BitVector() || VT.is64BitVector())) {
5223 unsigned PFIndexes[4];
5224 for (unsigned i = 0; i != 4; ++i) {
5225 if (M[i] < 0)
5226 PFIndexes[i] = 8;
5227 else
5228 PFIndexes[i] = M[i];
5229 }
5230
5231 // Compute the index in the perfect shuffle table.
5232 unsigned PFTableIndex =
5233 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5234 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5235 unsigned Cost = (PFEntry >> 30);
5236
5237 if (Cost <= 4)
5238 return true;
5239 }
5240
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005241 bool ReverseVEXT;
Bob Wilsona7062312009-08-21 20:54:19 +00005242 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005243
Bob Wilson846bd792010-06-07 23:53:38 +00005244 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5245 return (EltSize >= 32 ||
5246 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005247 isVREVMask(M, VT, 64) ||
5248 isVREVMask(M, VT, 32) ||
5249 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005250 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005251 isVTBLMask(M, VT) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005252 isVTRNMask(M, VT, WhichResult) ||
5253 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson0bbd3072009-12-03 06:40:55 +00005254 isVZIPMask(M, VT, WhichResult) ||
5255 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
5256 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005257 isVZIP_v_undef_Mask(M, VT, WhichResult) ||
5258 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005259}
5260
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005261/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5262/// the specified operations to build the shuffle.
5263static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5264 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005265 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005266 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5267 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5268 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5269
5270 enum {
5271 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5272 OP_VREV,
5273 OP_VDUP0,
5274 OP_VDUP1,
5275 OP_VDUP2,
5276 OP_VDUP3,
5277 OP_VEXT1,
5278 OP_VEXT2,
5279 OP_VEXT3,
5280 OP_VUZPL, // VUZP, left result
5281 OP_VUZPR, // VUZP, right result
5282 OP_VZIPL, // VZIP, left result
5283 OP_VZIPR, // VZIP, right result
5284 OP_VTRNL, // VTRN, left result
5285 OP_VTRNR // VTRN, right result
5286 };
5287
5288 if (OpNum == OP_COPY) {
5289 if (LHSID == (1*9+2)*9+3) return LHS;
5290 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5291 return RHS;
5292 }
5293
5294 SDValue OpLHS, OpRHS;
5295 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5296 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5297 EVT VT = OpLHS.getValueType();
5298
5299 switch (OpNum) {
5300 default: llvm_unreachable("Unknown shuffle opcode!");
5301 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00005302 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00005303 if (VT.getVectorElementType() == MVT::i32 ||
5304 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00005305 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
5306 // vrev <4 x i16> -> VREV32
5307 if (VT.getVectorElementType() == MVT::i16)
5308 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
5309 // vrev <4 x i8> -> VREV16
5310 assert(VT.getVectorElementType() == MVT::i8);
5311 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005312 case OP_VDUP0:
5313 case OP_VDUP1:
5314 case OP_VDUP2:
5315 case OP_VDUP3:
5316 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005317 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005318 case OP_VEXT1:
5319 case OP_VEXT2:
5320 case OP_VEXT3:
5321 return DAG.getNode(ARMISD::VEXT, dl, VT,
5322 OpLHS, OpRHS,
5323 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
5324 case OP_VUZPL:
5325 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005326 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005327 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
5328 case OP_VZIPL:
5329 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005330 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005331 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
5332 case OP_VTRNL:
5333 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005334 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5335 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005336 }
5337}
5338
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005339static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005340 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005341 SelectionDAG &DAG) {
5342 // Check to see if we can use the VTBL instruction.
5343 SDValue V1 = Op.getOperand(0);
5344 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005345 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005346
5347 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00005348 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005349 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
5350 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
5351
5352 if (V2.getNode()->getOpcode() == ISD::UNDEF)
5353 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Craig Topper48d114b2014-04-26 18:35:24 +00005354 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00005355
Owen Anderson77aa2662011-04-05 21:48:57 +00005356 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Craig Topper48d114b2014-04-26 18:35:24 +00005357 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005358}
5359
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005360static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
5361 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005362 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005363 SDValue OpLHS = Op.getOperand(0);
5364 EVT VT = OpLHS.getValueType();
5365
5366 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
5367 "Expect an v8i16/v16i8 type");
5368 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
5369 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
5370 // extract the first 8 bytes into the top double word and the last 8 bytes
5371 // into the bottom double word. The v8i16 case is similar.
5372 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
5373 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
5374 DAG.getConstant(ExtractNum, MVT::i32));
5375}
5376
Bob Wilson2e076c42009-06-22 23:27:02 +00005377static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005378 SDValue V1 = Op.getOperand(0);
5379 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005380 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00005381 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005382 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00005383
Bob Wilsonc6800b52009-08-13 02:13:04 +00005384 // Convert shuffles that are directly supported on NEON to target-specific
5385 // DAG nodes, instead of keeping them as shuffles and matching them again
5386 // during code selection. This is more efficient and avoids the possibility
5387 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00005388 // FIXME: floating-point vectors should be canonicalized to integer vectors
5389 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005390 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005391
Bob Wilson846bd792010-06-07 23:53:38 +00005392 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5393 if (EltSize <= 32) {
5394 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
5395 int Lane = SVN->getSplatIndex();
5396 // If this is undef splat, generate it via "just" vdup, if possible.
5397 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00005398
Dan Gohman198b7ff2011-11-03 21:49:52 +00005399 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00005400 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5401 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5402 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005403 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
5404 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
5405 // reaches it).
5406 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
5407 !isa<ConstantSDNode>(V1.getOperand(0))) {
5408 bool IsScalarToVector = true;
5409 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
5410 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
5411 IsScalarToVector = false;
5412 break;
5413 }
5414 if (IsScalarToVector)
5415 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5416 }
Bob Wilson846bd792010-06-07 23:53:38 +00005417 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
5418 DAG.getConstant(Lane, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00005419 }
Bob Wilson846bd792010-06-07 23:53:38 +00005420
5421 bool ReverseVEXT;
5422 unsigned Imm;
5423 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
5424 if (ReverseVEXT)
5425 std::swap(V1, V2);
5426 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
5427 DAG.getConstant(Imm, MVT::i32));
5428 }
5429
5430 if (isVREVMask(ShuffleMask, VT, 64))
5431 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
5432 if (isVREVMask(ShuffleMask, VT, 32))
5433 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
5434 if (isVREVMask(ShuffleMask, VT, 16))
5435 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
5436
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005437 if (V2->getOpcode() == ISD::UNDEF &&
5438 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
5439 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
5440 DAG.getConstant(Imm, MVT::i32));
5441 }
5442
Bob Wilson846bd792010-06-07 23:53:38 +00005443 // Check for Neon shuffles that modify both input vectors in place.
5444 // If both results are used, i.e., if there are two shuffles with the same
5445 // source operands and with masks corresponding to both results of one of
5446 // these operations, DAG memoization will ensure that a single node is
5447 // used for both shuffles.
5448 unsigned WhichResult;
5449 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5450 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5451 V1, V2).getValue(WhichResult);
5452 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5453 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5454 V1, V2).getValue(WhichResult);
5455 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5456 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5457 V1, V2).getValue(WhichResult);
5458
5459 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5460 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5461 V1, V1).getValue(WhichResult);
5462 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5463 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5464 V1, V1).getValue(WhichResult);
5465 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5466 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5467 V1, V1).getValue(WhichResult);
Bob Wilsoncce31f62009-08-14 05:08:32 +00005468 }
Bob Wilson32cd8552009-08-19 17:03:43 +00005469
Bob Wilsona7062312009-08-21 20:54:19 +00005470 // If the shuffle is not directly supported and it has 4 elements, use
5471 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00005472 unsigned NumElts = VT.getVectorNumElements();
5473 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005474 unsigned PFIndexes[4];
5475 for (unsigned i = 0; i != 4; ++i) {
5476 if (ShuffleMask[i] < 0)
5477 PFIndexes[i] = 8;
5478 else
5479 PFIndexes[i] = ShuffleMask[i];
5480 }
5481
5482 // Compute the index in the perfect shuffle table.
5483 unsigned PFTableIndex =
5484 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005485 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5486 unsigned Cost = (PFEntry >> 30);
5487
5488 if (Cost <= 4)
5489 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
5490 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00005491
Bob Wilsond8a9a042010-06-04 00:04:02 +00005492 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00005493 if (EltSize >= 32) {
5494 // Do the expansion with floating-point types, since that is what the VFP
5495 // registers are defined to use, and since i64 is not legal.
5496 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5497 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00005498 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
5499 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005500 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00005501 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00005502 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00005503 Ops.push_back(DAG.getUNDEF(EltVT));
5504 else
5505 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
5506 ShuffleMask[i] < (int)NumElts ? V1 : V2,
5507 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
5508 MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00005509 }
Craig Topper48d114b2014-04-26 18:35:24 +00005510 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005511 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00005512 }
5513
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005514 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
5515 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
5516
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005517 if (VT == MVT::v8i8) {
5518 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
5519 if (NewOp.getNode())
5520 return NewOp;
5521 }
5522
Bob Wilson6f34e272009-08-14 05:16:33 +00005523 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005524}
5525
Eli Friedmana5e244c2011-10-24 23:08:52 +00005526static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
5527 // INSERT_VECTOR_ELT is legal only for immediate indexes.
5528 SDValue Lane = Op.getOperand(2);
5529 if (!isa<ConstantSDNode>(Lane))
5530 return SDValue();
5531
5532 return Op;
5533}
5534
Bob Wilson2e076c42009-06-22 23:27:02 +00005535static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00005536 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00005537 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00005538 if (!isa<ConstantSDNode>(Lane))
5539 return SDValue();
5540
5541 SDValue Vec = Op.getOperand(0);
5542 if (Op.getValueType() == MVT::i32 &&
5543 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005544 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00005545 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
5546 }
5547
5548 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00005549}
5550
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005551static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5552 // The only time a CONCAT_VECTORS operation can have legal types is when
5553 // two 64-bit vectors are concatenated to a 128-bit vector.
5554 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
5555 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005556 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005557 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005558 SDValue Op0 = Op.getOperand(0);
5559 SDValue Op1 = Op.getOperand(1);
5560 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005561 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005562 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005563 DAG.getIntPtrConstant(0));
5564 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005565 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005566 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005567 DAG.getIntPtrConstant(1));
Wesley Peck527da1b2010-11-23 03:31:01 +00005568 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005569}
5570
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005571/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
5572/// element has been zero/sign-extended, depending on the isSigned parameter,
5573/// from an integer type half its size.
5574static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
5575 bool isSigned) {
5576 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
5577 EVT VT = N->getValueType(0);
5578 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
5579 SDNode *BVN = N->getOperand(0).getNode();
5580 if (BVN->getValueType(0) != MVT::v4i32 ||
5581 BVN->getOpcode() != ISD::BUILD_VECTOR)
5582 return false;
5583 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5584 unsigned HiElt = 1 - LoElt;
5585 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
5586 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
5587 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
5588 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
5589 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
5590 return false;
5591 if (isSigned) {
5592 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
5593 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
5594 return true;
5595 } else {
5596 if (Hi0->isNullValue() && Hi1->isNullValue())
5597 return true;
5598 }
5599 return false;
5600 }
5601
5602 if (N->getOpcode() != ISD::BUILD_VECTOR)
5603 return false;
5604
5605 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5606 SDNode *Elt = N->getOperand(i).getNode();
5607 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
5608 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5609 unsigned HalfSize = EltSize / 2;
5610 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005611 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005612 return false;
5613 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005614 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005615 return false;
5616 }
5617 continue;
5618 }
5619 return false;
5620 }
5621
5622 return true;
5623}
5624
5625/// isSignExtended - Check if a node is a vector value that is sign-extended
5626/// or a constant BUILD_VECTOR with sign-extended elements.
5627static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
5628 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
5629 return true;
5630 if (isExtendedBUILD_VECTOR(N, DAG, true))
5631 return true;
5632 return false;
5633}
5634
5635/// isZeroExtended - Check if a node is a vector value that is zero-extended
5636/// or a constant BUILD_VECTOR with zero-extended elements.
5637static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
5638 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
5639 return true;
5640 if (isExtendedBUILD_VECTOR(N, DAG, false))
5641 return true;
5642 return false;
5643}
5644
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005645static EVT getExtensionTo64Bits(const EVT &OrigVT) {
5646 if (OrigVT.getSizeInBits() >= 64)
5647 return OrigVT;
5648
5649 assert(OrigVT.isSimple() && "Expecting a simple value type");
5650
5651 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
5652 switch (OrigSimpleTy) {
5653 default: llvm_unreachable("Unexpected Vector Type");
5654 case MVT::v2i8:
5655 case MVT::v2i16:
5656 return MVT::v2i32;
5657 case MVT::v4i8:
5658 return MVT::v4i16;
5659 }
5660}
5661
Sebastian Popa204f722012-11-30 19:08:04 +00005662/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
5663/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
5664/// We insert the required extension here to get the vector to fill a D register.
5665static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
5666 const EVT &OrigTy,
5667 const EVT &ExtTy,
5668 unsigned ExtOpcode) {
5669 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
5670 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
5671 // 64-bits we need to insert a new extension so that it will be 64-bits.
5672 assert(ExtTy.is128BitVector() && "Unexpected extension size");
5673 if (OrigTy.getSizeInBits() >= 64)
5674 return N;
5675
5676 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005677 EVT NewVT = getExtensionTo64Bits(OrigTy);
5678
Andrew Trickef9de2a2013-05-25 02:42:55 +00005679 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00005680}
5681
5682/// SkipLoadExtensionForVMULL - return a load of the original vector size that
5683/// does not do any sign/zero extension. If the original vector is less
5684/// than 64 bits, an appropriate extension will be added after the load to
5685/// reach a total size of 64 bits. We have to add the extension separately
5686/// because ARM does not have a sign/zero extending load for vectors.
5687static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005688 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
5689
5690 // The load already has the right type.
5691 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00005692 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00005693 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
5694 LD->isNonTemporal(), LD->isInvariant(),
5695 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005696
5697 // We need to create a zextload/sextload. We cannot just create a load
5698 // followed by a zext/zext node because LowerMUL is also run during normal
5699 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005700 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005701 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
5702 LD->getMemoryVT(), LD->isVolatile(),
5703 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00005704}
5705
5706/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
5707/// extending load, or BUILD_VECTOR with extended elements, return the
5708/// unextended value. The unextended vector should be 64 bits so that it can
5709/// be used as an operand to a VMULL instruction. If the original vector size
5710/// before extension is less than 64 bits we add a an extension to resize
5711/// the vector to 64 bits.
5712static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00005713 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00005714 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
5715 N->getOperand(0)->getValueType(0),
5716 N->getValueType(0),
5717 N->getOpcode());
5718
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005719 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00005720 return SkipLoadExtensionForVMULL(LD, DAG);
5721
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005722 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
5723 // have been legalized as a BITCAST from v4i32.
5724 if (N->getOpcode() == ISD::BITCAST) {
5725 SDNode *BVN = N->getOperand(0).getNode();
5726 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
5727 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
5728 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00005729 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005730 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
5731 }
5732 // Construct a new BUILD_VECTOR with elements truncated to half the size.
5733 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
5734 EVT VT = N->getValueType(0);
5735 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
5736 unsigned NumElts = VT.getVectorNumElements();
5737 MVT TruncVT = MVT::getIntegerVT(EltSize);
5738 SmallVector<SDValue, 8> Ops;
5739 for (unsigned i = 0; i != NumElts; ++i) {
5740 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
5741 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00005742 // Element types smaller than 32 bits are not legal, so use i32 elements.
5743 // The values are implicitly truncated so sext vs. zext doesn't matter.
5744 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005745 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005746 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Craig Topper48d114b2014-04-26 18:35:24 +00005747 MVT::getVectorVT(TruncVT, NumElts), Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00005748}
5749
Evan Chenge2086e72011-03-29 01:56:09 +00005750static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
5751 unsigned Opcode = N->getOpcode();
5752 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5753 SDNode *N0 = N->getOperand(0).getNode();
5754 SDNode *N1 = N->getOperand(1).getNode();
5755 return N0->hasOneUse() && N1->hasOneUse() &&
5756 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
5757 }
5758 return false;
5759}
5760
5761static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
5762 unsigned Opcode = N->getOpcode();
5763 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5764 SDNode *N0 = N->getOperand(0).getNode();
5765 SDNode *N1 = N->getOperand(1).getNode();
5766 return N0->hasOneUse() && N1->hasOneUse() &&
5767 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
5768 }
5769 return false;
5770}
5771
Bob Wilson38ab35a2010-09-01 23:50:19 +00005772static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
5773 // Multiplications are only custom-lowered for 128-bit vectors so that
5774 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
5775 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00005776 assert(VT.is128BitVector() && VT.isInteger() &&
5777 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00005778 SDNode *N0 = Op.getOperand(0).getNode();
5779 SDNode *N1 = Op.getOperand(1).getNode();
5780 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00005781 bool isMLA = false;
5782 bool isN0SExt = isSignExtended(N0, DAG);
5783 bool isN1SExt = isSignExtended(N1, DAG);
5784 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00005785 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00005786 else {
5787 bool isN0ZExt = isZeroExtended(N0, DAG);
5788 bool isN1ZExt = isZeroExtended(N1, DAG);
5789 if (isN0ZExt && isN1ZExt)
5790 NewOpc = ARMISD::VMULLu;
5791 else if (isN1SExt || isN1ZExt) {
5792 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
5793 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
5794 if (isN1SExt && isAddSubSExt(N0, DAG)) {
5795 NewOpc = ARMISD::VMULLs;
5796 isMLA = true;
5797 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
5798 NewOpc = ARMISD::VMULLu;
5799 isMLA = true;
5800 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
5801 std::swap(N0, N1);
5802 NewOpc = ARMISD::VMULLu;
5803 isMLA = true;
5804 }
5805 }
5806
5807 if (!NewOpc) {
5808 if (VT == MVT::v2i64)
5809 // Fall through to expand this. It is not legal.
5810 return SDValue();
5811 else
5812 // Other vector multiplications are legal.
5813 return Op;
5814 }
5815 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00005816
5817 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005818 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00005819 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00005820 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005821 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00005822 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005823 assert(Op0.getValueType().is64BitVector() &&
5824 Op1.getValueType().is64BitVector() &&
5825 "unexpected types for extended operands to VMULL");
5826 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
5827 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00005828
Evan Chenge2086e72011-03-29 01:56:09 +00005829 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
5830 // isel lowering to take advantage of no-stall back to back vmul + vmla.
5831 // vmull q0, d4, d6
5832 // vmlal q0, d5, d6
5833 // is faster than
5834 // vaddl q0, d4, d5
5835 // vmovl q1, d6
5836 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00005837 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
5838 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005839 EVT Op1VT = Op1.getValueType();
5840 return DAG.getNode(N0->getOpcode(), DL, VT,
5841 DAG.getNode(NewOpc, DL, VT,
5842 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
5843 DAG.getNode(NewOpc, DL, VT,
5844 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00005845}
5846
Owen Anderson77aa2662011-04-05 21:48:57 +00005847static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005848LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00005849 // Convert to float
5850 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
5851 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
5852 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
5853 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
5854 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
5855 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
5856 // Get reciprocal estimate.
5857 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00005858 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005859 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
5860 // Because char has a smaller range than uchar, we can actually get away
5861 // without any newton steps. This requires that we use a weird bias
5862 // of 0xb000, however (again, this has been exhaustively tested).
5863 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
5864 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
5865 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
5866 Y = DAG.getConstant(0xb000, MVT::i32);
5867 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
5868 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
5869 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
5870 // Convert back to short.
5871 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
5872 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
5873 return X;
5874}
5875
Owen Anderson77aa2662011-04-05 21:48:57 +00005876static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005877LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00005878 SDValue N2;
5879 // Convert to float.
5880 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
5881 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
5882 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
5883 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
5884 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
5885 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005886
Nate Begemanfa62d502011-02-11 20:53:29 +00005887 // Use reciprocal estimate and one refinement step.
5888 // float4 recip = vrecpeq_f32(yf);
5889 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00005890 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005891 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005892 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005893 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
5894 N1, N2);
5895 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5896 // Because short has a smaller range than ushort, we can actually get away
5897 // with only a single newton step. This requires that we use a weird bias
5898 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005899 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00005900 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5901 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005902 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00005903 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5904 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5905 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5906 // Convert back to integer and return.
5907 // return vmovn_s32(vcvt_s32_f32(result));
5908 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5909 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5910 return N0;
5911}
5912
5913static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
5914 EVT VT = Op.getValueType();
5915 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5916 "unexpected type for custom-lowering ISD::SDIV");
5917
Andrew Trickef9de2a2013-05-25 02:42:55 +00005918 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00005919 SDValue N0 = Op.getOperand(0);
5920 SDValue N1 = Op.getOperand(1);
5921 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00005922
Nate Begemanfa62d502011-02-11 20:53:29 +00005923 if (VT == MVT::v8i8) {
5924 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
5925 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005926
Nate Begemanfa62d502011-02-11 20:53:29 +00005927 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5928 DAG.getIntPtrConstant(4));
5929 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00005930 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00005931 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5932 DAG.getIntPtrConstant(0));
5933 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5934 DAG.getIntPtrConstant(0));
5935
5936 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5937 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5938
5939 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5940 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00005941
Nate Begemanfa62d502011-02-11 20:53:29 +00005942 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5943 return N0;
5944 }
5945 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5946}
5947
5948static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5949 EVT VT = Op.getValueType();
5950 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5951 "unexpected type for custom-lowering ISD::UDIV");
5952
Andrew Trickef9de2a2013-05-25 02:42:55 +00005953 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00005954 SDValue N0 = Op.getOperand(0);
5955 SDValue N1 = Op.getOperand(1);
5956 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00005957
Nate Begemanfa62d502011-02-11 20:53:29 +00005958 if (VT == MVT::v8i8) {
5959 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5960 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005961
Nate Begemanfa62d502011-02-11 20:53:29 +00005962 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5963 DAG.getIntPtrConstant(4));
5964 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00005965 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00005966 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5967 DAG.getIntPtrConstant(0));
5968 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5969 DAG.getIntPtrConstant(0));
Owen Anderson77aa2662011-04-05 21:48:57 +00005970
Nate Begemanfa62d502011-02-11 20:53:29 +00005971 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5972 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00005973
Nate Begemanfa62d502011-02-11 20:53:29 +00005974 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5975 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00005976
5977 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begemanfa62d502011-02-11 20:53:29 +00005978 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5979 N0);
5980 return N0;
5981 }
Owen Anderson77aa2662011-04-05 21:48:57 +00005982
Nate Begemanfa62d502011-02-11 20:53:29 +00005983 // v4i16 sdiv ... Convert to float.
5984 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5985 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5986 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5987 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5988 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005989 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00005990
5991 // Use reciprocal estimate and two refinement steps.
5992 // float4 recip = vrecpeq_f32(yf);
5993 // recip *= vrecpsq_f32(yf, recip);
5994 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00005995 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005996 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005997 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005998 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005999 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006000 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00006001 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006002 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006003 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006004 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6005 // Simply multiplying by the reciprocal estimate can leave us a few ulps
6006 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6007 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006008 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006009 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6010 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
6011 N1 = DAG.getConstant(2, MVT::i32);
6012 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6013 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6014 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6015 // Convert back to integer and return.
6016 // return vmovn_u32(vcvt_s32_f32(result));
6017 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6018 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6019 return N0;
6020}
6021
Evan Chenge8916542011-08-30 01:34:54 +00006022static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6023 EVT VT = Op.getNode()->getValueType(0);
6024 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6025
6026 unsigned Opc;
6027 bool ExtraOp = false;
6028 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006029 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006030 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6031 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6032 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6033 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6034 }
6035
6036 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006037 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006038 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006039 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006040 Op.getOperand(1), Op.getOperand(2));
6041}
6042
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006043SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6044 assert(Subtarget->isTargetDarwin());
6045
6046 // For iOS, we want to call an alternative entry point: __sincos_stret,
6047 // return values are passed via sret.
6048 SDLoc dl(Op);
6049 SDValue Arg = Op.getOperand(0);
6050 EVT ArgVT = Arg.getValueType();
6051 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
6052
6053 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
6054 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6055
6056 // Pair of floats / doubles used to pass the result.
6057 StructType *RetTy = StructType::get(ArgTy, ArgTy, NULL);
6058
6059 // Create stack object for sret.
6060 const uint64_t ByteSize = TLI.getDataLayout()->getTypeAllocSize(RetTy);
6061 const unsigned StackAlign = TLI.getDataLayout()->getPrefTypeAlignment(RetTy);
6062 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
6063 SDValue SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
6064
6065 ArgListTy Args;
6066 ArgListEntry Entry;
6067
6068 Entry.Node = SRet;
6069 Entry.Ty = RetTy->getPointerTo();
6070 Entry.isSExt = false;
6071 Entry.isZExt = false;
6072 Entry.isSRet = true;
6073 Args.push_back(Entry);
6074
6075 Entry.Node = Arg;
6076 Entry.Ty = ArgTy;
6077 Entry.isSExt = false;
6078 Entry.isZExt = false;
6079 Args.push_back(Entry);
6080
6081 const char *LibcallName = (ArgVT == MVT::f64)
6082 ? "__sincos_stret" : "__sincosf_stret";
6083 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
6084
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006085 TargetLowering::CallLoweringInfo CLI(DAG);
6086 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
6087 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), Callee,
6088 &Args, 0)
6089 .setDiscardResult();
6090
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006091 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6092
6093 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6094 MachinePointerInfo(), false, false, false, 0);
6095
6096 // Address of cos field.
6097 SDValue Add = DAG.getNode(ISD::ADD, dl, getPointerTy(), SRet,
6098 DAG.getIntPtrConstant(ArgVT.getStoreSize()));
6099 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6100 MachinePointerInfo(), false, false, false, 0);
6101
6102 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6103 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6104 LoadSin.getValue(0), LoadCos.getValue(0));
6105}
6106
Eli Friedman10f9ce22011-09-15 22:26:18 +00006107static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00006108 // Monotonic load/store is legal for all targets
6109 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
6110 return Op;
6111
Alp Tokercb402912014-01-24 17:20:08 +00006112 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00006113 // dmb or equivalent available.
6114 return SDValue();
6115}
6116
Tim Northoverbc933082013-05-23 19:11:20 +00006117static void ReplaceREADCYCLECOUNTER(SDNode *N,
6118 SmallVectorImpl<SDValue> &Results,
6119 SelectionDAG &DAG,
6120 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006121 SDLoc DL(N);
Tim Northoverbc933082013-05-23 19:11:20 +00006122 SDValue Cycles32, OutChain;
6123
6124 if (Subtarget->hasPerfMon()) {
6125 // Under Power Management extensions, the cycle-count is:
6126 // mrc p15, #0, <Rt>, c9, c13, #0
6127 SDValue Ops[] = { N->getOperand(0), // Chain
6128 DAG.getConstant(Intrinsic::arm_mrc, MVT::i32),
6129 DAG.getConstant(15, MVT::i32),
6130 DAG.getConstant(0, MVT::i32),
6131 DAG.getConstant(9, MVT::i32),
6132 DAG.getConstant(13, MVT::i32),
6133 DAG.getConstant(0, MVT::i32)
6134 };
6135
6136 Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00006137 DAG.getVTList(MVT::i32, MVT::Other), Ops);
Tim Northoverbc933082013-05-23 19:11:20 +00006138 OutChain = Cycles32.getValue(1);
6139 } else {
6140 // Intrinsic is defined to return 0 on unsupported platforms. Technically
6141 // there are older ARM CPUs that have implementation-specific ways of
6142 // obtaining this information (FIXME!).
6143 Cycles32 = DAG.getConstant(0, MVT::i32);
6144 OutChain = DAG.getEntryNode();
6145 }
6146
6147
6148 SDValue Cycles64 = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64,
6149 Cycles32, DAG.getConstant(0, MVT::i32));
6150 Results.push_back(Cycles64);
6151 Results.push_back(OutChain);
6152}
6153
Dan Gohman21cea8a2010-04-17 15:26:15 +00006154SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006155 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006156 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Cheng10043e22007-01-19 07:51:42 +00006157 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006158 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006159 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00006160 switch (Subtarget->getTargetTriple().getObjectFormat()) {
6161 default: llvm_unreachable("unknown object format");
6162 case Triple::COFF:
6163 return LowerGlobalAddressWindows(Op, DAG);
6164 case Triple::ELF:
6165 return LowerGlobalAddressELF(Op, DAG);
6166 case Triple::MachO:
6167 return LowerGlobalAddressDarwin(Op, DAG);
6168 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006169 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006170 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006171 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6172 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006173 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006174 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006175 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006176 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006177 case ISD::SINT_TO_FP:
6178 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6179 case ISD::FP_TO_SINT:
6180 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006181 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006182 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006183 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006184 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006185 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006186 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006187 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6188 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006189 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006190 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006191 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006192 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng15b80e42009-11-12 07:13:11 +00006193 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00006194 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00006195 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach8546ec92010-01-18 19:58:49 +00006196 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00006197 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00006198 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00006199 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006200 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00006201 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00006202 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006203 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006204 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00006205 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006206 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begemanfa62d502011-02-11 20:53:29 +00006207 case ISD::SDIV: return LowerSDIV(Op, DAG);
6208 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00006209 case ISD::ADDC:
6210 case ISD::ADDE:
6211 case ISD::SUBC:
6212 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00006213 case ISD::SADDO:
6214 case ISD::UADDO:
6215 case ISD::SSUBO:
6216 case ISD::USUBO:
6217 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00006218 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00006219 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006220 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00006221 case ISD::SDIVREM:
6222 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00006223 case ISD::DYNAMIC_STACKALLOC:
6224 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
6225 return LowerDYNAMIC_STACKALLOC(Op, DAG);
6226 llvm_unreachable("Don't know how to custom lower this!");
Evan Cheng10043e22007-01-19 07:51:42 +00006227 }
Evan Cheng10043e22007-01-19 07:51:42 +00006228}
6229
Duncan Sands6ed40142008-12-01 11:39:25 +00006230/// ReplaceNodeResults - Replace the results of node with an illegal result
6231/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00006232void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
6233 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006234 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00006235 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006236 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006237 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00006238 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peck527da1b2010-11-23 03:31:01 +00006239 case ISD::BITCAST:
6240 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006241 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006242 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00006243 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00006244 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006245 break;
Tim Northoverbc933082013-05-23 19:11:20 +00006246 case ISD::READCYCLECOUNTER:
6247 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
6248 return;
Duncan Sands6ed40142008-12-01 11:39:25 +00006249 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00006250 if (Res.getNode())
6251 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00006252}
Chris Lattnerf81d5882007-11-24 07:07:01 +00006253
Evan Cheng10043e22007-01-19 07:51:42 +00006254//===----------------------------------------------------------------------===//
6255// ARM Scheduler Hooks
6256//===----------------------------------------------------------------------===//
6257
Bill Wendling030b58e2011-10-06 22:18:16 +00006258/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6259/// registers the function context.
6260void ARMTargetLowering::
6261SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6262 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendling374ee192011-10-03 21:25:38 +00006263 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6264 DebugLoc dl = MI->getDebugLoc();
6265 MachineFunction *MF = MBB->getParent();
6266 MachineRegisterInfo *MRI = &MF->getRegInfo();
6267 MachineConstantPool *MCP = MF->getConstantPool();
6268 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6269 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00006270
Bill Wendling374ee192011-10-03 21:25:38 +00006271 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006272 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00006273
Bill Wendling374ee192011-10-03 21:25:38 +00006274 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006275 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00006276 ARMConstantPoolValue *CPV =
6277 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6278 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6279
Craig Topperc7242e02012-04-20 07:30:17 +00006280 const TargetRegisterClass *TRC = isThumb ?
6281 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6282 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00006283
Bill Wendling030b58e2011-10-06 22:18:16 +00006284 // Grab constant pool and fixed stack memory operands.
6285 MachineMemOperand *CPMMO =
6286 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6287 MachineMemOperand::MOLoad, 4, 4);
6288
6289 MachineMemOperand *FIMMOSt =
6290 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6291 MachineMemOperand::MOStore, 4, 4);
6292
6293 // Load the address of the dispatch MBB into the jump buffer.
6294 if (isThumb2) {
6295 // Incoming value: jbuf
6296 // ldr.n r5, LCPI1_1
6297 // orr r5, r5, #1
6298 // add r5, pc
6299 // str r5, [$jbuf, #+4] ; &jbuf[1]
6300 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6301 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6302 .addConstantPoolIndex(CPI)
6303 .addMemOperand(CPMMO));
6304 // Set the low bit because of thumb mode.
6305 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6306 AddDefaultCC(
6307 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6308 .addReg(NewVReg1, RegState::Kill)
6309 .addImm(0x01)));
6310 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6311 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6312 .addReg(NewVReg2, RegState::Kill)
6313 .addImm(PCLabelId);
6314 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6315 .addReg(NewVReg3, RegState::Kill)
6316 .addFrameIndex(FI)
6317 .addImm(36) // &jbuf[1] :: pc
6318 .addMemOperand(FIMMOSt));
6319 } else if (isThumb) {
6320 // Incoming value: jbuf
6321 // ldr.n r1, LCPI1_4
6322 // add r1, pc
6323 // mov r2, #1
6324 // orrs r1, r2
6325 // add r2, $jbuf, #+4 ; &jbuf[1]
6326 // str r1, [r2]
6327 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6328 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6329 .addConstantPoolIndex(CPI)
6330 .addMemOperand(CPMMO));
6331 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6332 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6333 .addReg(NewVReg1, RegState::Kill)
6334 .addImm(PCLabelId);
6335 // Set the low bit because of thumb mode.
6336 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6337 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6338 .addReg(ARM::CPSR, RegState::Define)
6339 .addImm(1));
6340 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6341 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6342 .addReg(ARM::CPSR, RegState::Define)
6343 .addReg(NewVReg2, RegState::Kill)
6344 .addReg(NewVReg3, RegState::Kill));
6345 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6346 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
6347 .addFrameIndex(FI)
6348 .addImm(36)); // &jbuf[1] :: pc
6349 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6350 .addReg(NewVReg4, RegState::Kill)
6351 .addReg(NewVReg5, RegState::Kill)
6352 .addImm(0)
6353 .addMemOperand(FIMMOSt));
6354 } else {
6355 // Incoming value: jbuf
6356 // ldr r1, LCPI1_1
6357 // add r1, pc, r1
6358 // str r1, [$jbuf, #+4] ; &jbuf[1]
6359 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6360 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6361 .addConstantPoolIndex(CPI)
6362 .addImm(0)
6363 .addMemOperand(CPMMO));
6364 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6365 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6366 .addReg(NewVReg1, RegState::Kill)
6367 .addImm(PCLabelId));
6368 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6369 .addReg(NewVReg2, RegState::Kill)
6370 .addFrameIndex(FI)
6371 .addImm(36) // &jbuf[1] :: pc
6372 .addMemOperand(FIMMOSt));
6373 }
6374}
6375
6376MachineBasicBlock *ARMTargetLowering::
6377EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
6378 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6379 DebugLoc dl = MI->getDebugLoc();
6380 MachineFunction *MF = MBB->getParent();
6381 MachineRegisterInfo *MRI = &MF->getRegInfo();
6382 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6383 MachineFrameInfo *MFI = MF->getFrameInfo();
6384 int FI = MFI->getFunctionContextIndex();
6385
Craig Topperc7242e02012-04-20 07:30:17 +00006386 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
6387 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen691ae332012-05-20 06:38:47 +00006388 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00006389
Bill Wendling362c1b02011-10-06 21:29:56 +00006390 // Get a mapping of the call site numbers to all of the landing pads they're
6391 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00006392 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6393 unsigned MaxCSNum = 0;
6394 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00006395 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6396 ++BB) {
Bill Wendling202803e2011-10-05 00:02:33 +00006397 if (!BB->isLandingPad()) continue;
6398
6399 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6400 // pad.
6401 for (MachineBasicBlock::iterator
6402 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6403 if (!II->isEHLabel()) continue;
6404
6405 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006406 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00006407
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006408 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6409 for (SmallVectorImpl<unsigned>::iterator
6410 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6411 CSI != CSE; ++CSI) {
6412 CallSiteNumToLPad[*CSI].push_back(BB);
6413 MaxCSNum = std::max(MaxCSNum, *CSI);
6414 }
Bill Wendling202803e2011-10-05 00:02:33 +00006415 break;
6416 }
6417 }
6418
6419 // Get an ordered list of the machine basic blocks for the jump table.
6420 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling883ec972011-10-07 23:18:02 +00006421 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00006422 LPadList.reserve(CallSiteNumToLPad.size());
6423 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6424 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6425 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006426 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00006427 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00006428 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6429 }
Bill Wendling202803e2011-10-05 00:02:33 +00006430 }
6431
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006432 assert(!LPadList.empty() &&
6433 "No landing pad destinations for the dispatch jump table!");
6434
Bill Wendling362c1b02011-10-06 21:29:56 +00006435 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00006436 MachineJumpTableInfo *JTI =
6437 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6438 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
6439 unsigned UId = AFI->createJumpTableUId();
Chad Rosier96603432013-03-01 18:30:38 +00006440 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00006441
Bill Wendling362c1b02011-10-06 21:29:56 +00006442 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00006443
6444 // Shove the dispatch's address into the return slot in the function context.
6445 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6446 DispatchBB->setIsLandingPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00006447
Bill Wendling324be982011-10-05 00:39:32 +00006448 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006449 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00006450 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006451 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00006452 else
6453 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
6454
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006455 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00006456 DispatchBB->addSuccessor(TrapBB);
6457
6458 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6459 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00006460
Bill Wendling510fbcd2011-10-17 21:32:56 +00006461 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00006462 MF->insert(MF->end(), DispatchBB);
6463 MF->insert(MF->end(), DispContBB);
6464 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00006465
Bill Wendling030b58e2011-10-06 22:18:16 +00006466 // Insert code into the entry block that creates and registers the function
6467 // context.
6468 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6469
Bill Wendling030b58e2011-10-06 22:18:16 +00006470 MachineMemOperand *FIMMOLd =
Bill Wendling362c1b02011-10-06 21:29:56 +00006471 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendlingb3d46782011-10-06 23:37:36 +00006472 MachineMemOperand::MOLoad |
6473 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00006474
Chad Rosier1ec8e402012-11-06 23:05:24 +00006475 MachineInstrBuilder MIB;
6476 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6477
6478 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6479 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6480
6481 // Add a register mask with no preserved registers. This results in all
6482 // registers being marked as clobbered.
6483 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00006484
Bill Wendling85833f72011-10-18 22:49:07 +00006485 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00006486 if (Subtarget->isThumb2()) {
6487 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6488 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6489 .addFrameIndex(FI)
6490 .addImm(4)
6491 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006492
Bill Wendling85833f72011-10-18 22:49:07 +00006493 if (NumLPads < 256) {
6494 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6495 .addReg(NewVReg1)
6496 .addImm(LPadList.size()));
6497 } else {
6498 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6499 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006500 .addImm(NumLPads & 0xFFFF));
6501
6502 unsigned VReg2 = VReg1;
6503 if ((NumLPads & 0xFFFF0000) != 0) {
6504 VReg2 = MRI->createVirtualRegister(TRC);
6505 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6506 .addReg(VReg1)
6507 .addImm(NumLPads >> 16));
6508 }
6509
Bill Wendling85833f72011-10-18 22:49:07 +00006510 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6511 .addReg(NewVReg1)
6512 .addReg(VReg2));
6513 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006514
Bill Wendling5626c662011-10-06 22:53:00 +00006515 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6516 .addMBB(TrapBB)
6517 .addImm(ARMCC::HI)
6518 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00006519
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006520 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6521 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006522 .addJumpTableIndex(MJTI)
6523 .addImm(UId));
Bill Wendling202803e2011-10-05 00:02:33 +00006524
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006525 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006526 AddDefaultCC(
6527 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006528 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6529 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00006530 .addReg(NewVReg1)
6531 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6532
6533 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006534 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00006535 .addReg(NewVReg1)
Bill Wendling5626c662011-10-06 22:53:00 +00006536 .addJumpTableIndex(MJTI)
6537 .addImm(UId);
6538 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00006539 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6540 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6541 .addFrameIndex(FI)
6542 .addImm(1)
6543 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00006544
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006545 if (NumLPads < 256) {
6546 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6547 .addReg(NewVReg1)
6548 .addImm(NumLPads));
6549 } else {
6550 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00006551 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6552 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6553
6554 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006555 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006556 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006557 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006558 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006559
6560 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6561 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6562 .addReg(VReg1, RegState::Define)
6563 .addConstantPoolIndex(Idx));
6564 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6565 .addReg(NewVReg1)
6566 .addReg(VReg1));
6567 }
6568
Bill Wendlingb3d46782011-10-06 23:37:36 +00006569 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6570 .addMBB(TrapBB)
6571 .addImm(ARMCC::HI)
6572 .addReg(ARM::CPSR);
6573
6574 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6575 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6576 .addReg(ARM::CPSR, RegState::Define)
6577 .addReg(NewVReg1)
6578 .addImm(2));
6579
6580 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00006581 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendlingb3d46782011-10-06 23:37:36 +00006582 .addJumpTableIndex(MJTI)
6583 .addImm(UId));
6584
6585 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6586 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6587 .addReg(ARM::CPSR, RegState::Define)
6588 .addReg(NewVReg2, RegState::Kill)
6589 .addReg(NewVReg3));
6590
6591 MachineMemOperand *JTMMOLd =
6592 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6593 MachineMemOperand::MOLoad, 4, 4);
6594
6595 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6596 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6597 .addReg(NewVReg4, RegState::Kill)
6598 .addImm(0)
6599 .addMemOperand(JTMMOLd));
6600
Chad Rosier96603432013-03-01 18:30:38 +00006601 unsigned NewVReg6 = NewVReg5;
6602 if (RelocM == Reloc::PIC_) {
6603 NewVReg6 = MRI->createVirtualRegister(TRC);
6604 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6605 .addReg(ARM::CPSR, RegState::Define)
6606 .addReg(NewVReg5, RegState::Kill)
6607 .addReg(NewVReg3));
6608 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00006609
6610 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6611 .addReg(NewVReg6, RegState::Kill)
6612 .addJumpTableIndex(MJTI)
6613 .addImm(UId);
Bill Wendling5626c662011-10-06 22:53:00 +00006614 } else {
6615 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6616 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6617 .addFrameIndex(FI)
6618 .addImm(4)
6619 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00006620
Bill Wendling4969dcd2011-10-18 22:52:20 +00006621 if (NumLPads < 256) {
6622 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6623 .addReg(NewVReg1)
6624 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00006625 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00006626 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6627 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006628 .addImm(NumLPads & 0xFFFF));
6629
6630 unsigned VReg2 = VReg1;
6631 if ((NumLPads & 0xFFFF0000) != 0) {
6632 VReg2 = MRI->createVirtualRegister(TRC);
6633 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6634 .addReg(VReg1)
6635 .addImm(NumLPads >> 16));
6636 }
6637
Bill Wendling4969dcd2011-10-18 22:52:20 +00006638 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6639 .addReg(NewVReg1)
6640 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00006641 } else {
6642 MachineConstantPool *ConstantPool = MF->getConstantPool();
6643 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6644 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6645
6646 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006647 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006648 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006649 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006650 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6651
6652 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6653 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6654 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00006655 .addConstantPoolIndex(Idx)
6656 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00006657 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6658 .addReg(NewVReg1)
6659 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00006660 }
6661
Bill Wendling5626c662011-10-06 22:53:00 +00006662 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6663 .addMBB(TrapBB)
6664 .addImm(ARMCC::HI)
6665 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00006666
Bill Wendling973c8172011-10-18 22:11:18 +00006667 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006668 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00006669 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006670 .addReg(NewVReg1)
6671 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00006672 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6673 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006674 .addJumpTableIndex(MJTI)
6675 .addImm(UId));
6676
6677 MachineMemOperand *JTMMOLd =
6678 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6679 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00006680 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006681 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00006682 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6683 .addReg(NewVReg3, RegState::Kill)
6684 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006685 .addImm(0)
6686 .addMemOperand(JTMMOLd));
6687
Chad Rosier96603432013-03-01 18:30:38 +00006688 if (RelocM == Reloc::PIC_) {
6689 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
6690 .addReg(NewVReg5, RegState::Kill)
6691 .addReg(NewVReg4)
6692 .addJumpTableIndex(MJTI)
6693 .addImm(UId);
6694 } else {
6695 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
6696 .addReg(NewVReg5, RegState::Kill)
6697 .addJumpTableIndex(MJTI)
6698 .addImm(UId);
6699 }
Bill Wendling5626c662011-10-06 22:53:00 +00006700 }
Bill Wendling202803e2011-10-05 00:02:33 +00006701
Bill Wendling324be982011-10-05 00:39:32 +00006702 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006703 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00006704 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006705 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6706 MachineBasicBlock *CurMBB = *I;
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006707 if (SeenMBBs.insert(CurMBB))
Bill Wendling883ec972011-10-07 23:18:02 +00006708 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006709 }
6710
Bill Wendling26d27802011-10-17 05:25:09 +00006711 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00006712 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00006713 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling883ec972011-10-07 23:18:02 +00006714 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6715 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6716 MachineBasicBlock *BB = *I;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006717
6718 // Remove the landing pad successor from the invoke block and replace it
6719 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00006720 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6721 BB->succ_end());
6722 while (!Successors.empty()) {
6723 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling883ec972011-10-07 23:18:02 +00006724 if (SMBB->isLandingPad()) {
6725 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00006726 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006727 }
6728 }
6729
6730 BB->addSuccessor(DispatchBB);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006731
6732 // Find the invoke call and mark all of the callee-saved registers as
6733 // 'implicit defined' so that they're spilled. This prevents code from
6734 // moving instructions to before the EH block, where they will never be
6735 // executed.
6736 for (MachineBasicBlock::reverse_iterator
6737 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00006738 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006739
6740 DenseMap<unsigned, bool> DefRegs;
6741 for (MachineInstr::mop_iterator
6742 OI = II->operands_begin(), OE = II->operands_end();
6743 OI != OE; ++OI) {
6744 if (!OI->isReg()) continue;
6745 DefRegs[OI->getReg()] = true;
6746 }
6747
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00006748 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006749
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006750 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00006751 unsigned Reg = SavedRegs[i];
6752 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00006753 !ARM::tGPRRegClass.contains(Reg) &&
6754 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006755 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006756 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006757 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006758 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006759 continue;
6760 if (!DefRegs[Reg])
6761 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006762 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006763
6764 break;
6765 }
Bill Wendling883ec972011-10-07 23:18:02 +00006766 }
Bill Wendling324be982011-10-05 00:39:32 +00006767
Bill Wendling617075f2011-10-18 18:30:49 +00006768 // Mark all former landing pads as non-landing pads. The dispatch is the only
6769 // landing pad now.
6770 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6771 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6772 (*I)->setIsLandingPad(false);
6773
Bill Wendling324be982011-10-05 00:39:32 +00006774 // The instruction is gone now.
6775 MI->eraseFromParent();
6776
Bill Wendling374ee192011-10-03 21:25:38 +00006777 return MBB;
6778}
6779
Evan Cheng0cc4ad92010-07-13 19:27:42 +00006780static
6781MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6782 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6783 E = MBB->succ_end(); I != E; ++I)
6784 if (*I != Succ)
6785 return *I;
6786 llvm_unreachable("Expecting a BB with two successors!");
6787}
6788
Manman Renb504f492013-10-29 22:27:32 +00006789/// Return the load opcode for a given load size. If load size >= 8,
6790/// neon opcode will be returned.
6791static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
6792 if (LdSize >= 8)
6793 return LdSize == 16 ? ARM::VLD1q32wb_fixed
6794 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
6795 if (IsThumb1)
6796 return LdSize == 4 ? ARM::tLDRi
6797 : LdSize == 2 ? ARM::tLDRHi
6798 : LdSize == 1 ? ARM::tLDRBi : 0;
6799 if (IsThumb2)
6800 return LdSize == 4 ? ARM::t2LDR_POST
6801 : LdSize == 2 ? ARM::t2LDRH_POST
6802 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
6803 return LdSize == 4 ? ARM::LDR_POST_IMM
6804 : LdSize == 2 ? ARM::LDRH_POST
6805 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
6806}
6807
6808/// Return the store opcode for a given store size. If store size >= 8,
6809/// neon opcode will be returned.
6810static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
6811 if (StSize >= 8)
6812 return StSize == 16 ? ARM::VST1q32wb_fixed
6813 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
6814 if (IsThumb1)
6815 return StSize == 4 ? ARM::tSTRi
6816 : StSize == 2 ? ARM::tSTRHi
6817 : StSize == 1 ? ARM::tSTRBi : 0;
6818 if (IsThumb2)
6819 return StSize == 4 ? ARM::t2STR_POST
6820 : StSize == 2 ? ARM::t2STRH_POST
6821 : StSize == 1 ? ARM::t2STRB_POST : 0;
6822 return StSize == 4 ? ARM::STR_POST_IMM
6823 : StSize == 2 ? ARM::STRH_POST
6824 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
6825}
6826
6827/// Emit a post-increment load operation with given size. The instructions
6828/// will be added to BB at Pos.
6829static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
6830 const TargetInstrInfo *TII, DebugLoc dl,
6831 unsigned LdSize, unsigned Data, unsigned AddrIn,
6832 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
6833 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
6834 assert(LdOpc != 0 && "Should have a load opcode");
6835 if (LdSize >= 8) {
6836 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6837 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6838 .addImm(0));
6839 } else if (IsThumb1) {
6840 // load + update AddrIn
6841 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6842 .addReg(AddrIn).addImm(0));
6843 MachineInstrBuilder MIB =
6844 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
6845 MIB = AddDefaultT1CC(MIB);
6846 MIB.addReg(AddrIn).addImm(LdSize);
6847 AddDefaultPred(MIB);
6848 } else if (IsThumb2) {
6849 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6850 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6851 .addImm(LdSize));
6852 } else { // arm
6853 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6854 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6855 .addReg(0).addImm(LdSize));
6856 }
6857}
6858
6859/// Emit a post-increment store operation with given size. The instructions
6860/// will be added to BB at Pos.
6861static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
6862 const TargetInstrInfo *TII, DebugLoc dl,
6863 unsigned StSize, unsigned Data, unsigned AddrIn,
6864 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
6865 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
6866 assert(StOpc != 0 && "Should have a store opcode");
6867 if (StSize >= 8) {
6868 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6869 .addReg(AddrIn).addImm(0).addReg(Data));
6870 } else if (IsThumb1) {
6871 // store + update AddrIn
6872 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
6873 .addReg(AddrIn).addImm(0));
6874 MachineInstrBuilder MIB =
6875 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
6876 MIB = AddDefaultT1CC(MIB);
6877 MIB.addReg(AddrIn).addImm(StSize);
6878 AddDefaultPred(MIB);
6879 } else if (IsThumb2) {
6880 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6881 .addReg(Data).addReg(AddrIn).addImm(StSize));
6882 } else { // arm
6883 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6884 .addReg(Data).addReg(AddrIn).addReg(0)
6885 .addImm(StSize));
6886 }
6887}
6888
David Peixottoc32e24a2013-10-17 19:49:22 +00006889MachineBasicBlock *
6890ARMTargetLowering::EmitStructByval(MachineInstr *MI,
6891 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00006892 // This pseudo instruction has 3 operands: dst, src, size
6893 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6894 // Otherwise, we will generate unrolled scalar copies.
6895 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6896 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6897 MachineFunction::iterator It = BB;
6898 ++It;
6899
6900 unsigned dest = MI->getOperand(0).getReg();
6901 unsigned src = MI->getOperand(1).getReg();
6902 unsigned SizeVal = MI->getOperand(2).getImm();
6903 unsigned Align = MI->getOperand(3).getImm();
6904 DebugLoc dl = MI->getDebugLoc();
6905
Manman Rene8735522012-06-01 19:33:18 +00006906 MachineFunction *MF = BB->getParent();
6907 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00006908 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00006909 const TargetRegisterClass *TRC = nullptr;
6910 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00006911
6912 bool IsThumb1 = Subtarget->isThumb1Only();
6913 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00006914
6915 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00006916 UnitSize = 1;
6917 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00006918 UnitSize = 2;
6919 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00006920 // Check whether we can use NEON instructions.
Bill Wendling698e84f2012-12-30 10:32:01 +00006921 if (!MF->getFunction()->getAttributes().
6922 hasAttribute(AttributeSet::FunctionIndex,
6923 Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00006924 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00006925 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00006926 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00006927 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00006928 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00006929 }
6930 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00006931 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00006932 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00006933 }
Manman Ren6e1fd462012-06-18 22:23:48 +00006934
David Peixottob0653e532013-10-24 16:39:36 +00006935 // Select the correct opcode and register class for unit size load/store
6936 bool IsNeon = UnitSize >= 8;
6937 TRC = (IsThumb1 || IsThumb2) ? (const TargetRegisterClass *)&ARM::tGPRRegClass
6938 : (const TargetRegisterClass *)&ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00006939 if (IsNeon)
David Peixottob0653e532013-10-24 16:39:36 +00006940 VecTRC = UnitSize == 16
6941 ? (const TargetRegisterClass *)&ARM::DPairRegClass
6942 : UnitSize == 8
6943 ? (const TargetRegisterClass *)&ARM::DPRRegClass
Craig Topper062a2ba2014-04-25 05:30:21 +00006944 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00006945
Manman Rene8735522012-06-01 19:33:18 +00006946 unsigned BytesLeft = SizeVal % UnitSize;
6947 unsigned LoopSize = SizeVal - BytesLeft;
6948
6949 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6950 // Use LDR and STR to copy.
6951 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6952 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6953 unsigned srcIn = src;
6954 unsigned destIn = dest;
6955 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00006956 unsigned srcOut = MRI.createVirtualRegister(TRC);
6957 unsigned destOut = MRI.createVirtualRegister(TRC);
6958 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00006959 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
6960 IsThumb1, IsThumb2);
6961 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
6962 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00006963 srcIn = srcOut;
6964 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00006965 }
6966
6967 // Handle the leftover bytes with LDRB and STRB.
6968 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6969 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00006970 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00006971 unsigned srcOut = MRI.createVirtualRegister(TRC);
6972 unsigned destOut = MRI.createVirtualRegister(TRC);
6973 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00006974 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
6975 IsThumb1, IsThumb2);
6976 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
6977 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00006978 srcIn = srcOut;
6979 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00006980 }
6981 MI->eraseFromParent(); // The instruction is gone now.
6982 return BB;
6983 }
6984
6985 // Expand the pseudo op to a loop.
6986 // thisMBB:
6987 // ...
6988 // movw varEnd, # --> with thumb2
6989 // movt varEnd, #
6990 // ldrcp varEnd, idx --> without thumb2
6991 // fallthrough --> loopMBB
6992 // loopMBB:
6993 // PHI varPhi, varEnd, varLoop
6994 // PHI srcPhi, src, srcLoop
6995 // PHI destPhi, dst, destLoop
6996 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6997 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
6998 // subs varLoop, varPhi, #UnitSize
6999 // bne loopMBB
7000 // fallthrough --> exitMBB
7001 // exitMBB:
7002 // epilogue to handle left-over bytes
7003 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7004 // [destOut] = STRB_POST(scratch, destLoop, 1)
7005 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7006 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7007 MF->insert(It, loopMBB);
7008 MF->insert(It, exitMBB);
7009
7010 // Transfer the remainder of BB and its successor edges to exitMBB.
7011 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007012 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007013 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7014
7015 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007016 unsigned varEnd = MRI.createVirtualRegister(TRC);
7017 if (IsThumb2) {
7018 unsigned Vtmp = varEnd;
7019 if ((LoopSize & 0xFFFF0000) != 0)
7020 Vtmp = MRI.createVirtualRegister(TRC);
7021 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), Vtmp)
7022 .addImm(LoopSize & 0xFFFF));
7023
7024 if ((LoopSize & 0xFFFF0000) != 0)
7025 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
7026 .addReg(Vtmp).addImm(LoopSize >> 16));
7027 } else {
7028 MachineConstantPool *ConstantPool = MF->getConstantPool();
7029 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7030 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7031
7032 // MachineConstantPool wants an explicit alignment.
7033 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
7034 if (Align == 0)
7035 Align = getDataLayout()->getTypeAllocSize(C->getType());
7036 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7037
7038 if (IsThumb1)
7039 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7040 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7041 else
7042 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7043 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7044 }
Manman Rene8735522012-06-01 19:33:18 +00007045 BB->addSuccessor(loopMBB);
7046
7047 // Generate the loop body:
7048 // varPhi = PHI(varLoop, varEnd)
7049 // srcPhi = PHI(srcLoop, src)
7050 // destPhi = PHI(destLoop, dst)
7051 MachineBasicBlock *entryBB = BB;
7052 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007053 unsigned varLoop = MRI.createVirtualRegister(TRC);
7054 unsigned varPhi = MRI.createVirtualRegister(TRC);
7055 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7056 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7057 unsigned destLoop = MRI.createVirtualRegister(TRC);
7058 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007059
7060 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7061 .addReg(varLoop).addMBB(loopMBB)
7062 .addReg(varEnd).addMBB(entryBB);
7063 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7064 .addReg(srcLoop).addMBB(loopMBB)
7065 .addReg(src).addMBB(entryBB);
7066 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7067 .addReg(destLoop).addMBB(loopMBB)
7068 .addReg(dest).addMBB(entryBB);
7069
7070 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7071 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007072 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007073 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7074 IsThumb1, IsThumb2);
7075 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7076 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007077
7078 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007079 if (IsThumb1) {
7080 MachineInstrBuilder MIB =
7081 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7082 MIB = AddDefaultT1CC(MIB);
7083 MIB.addReg(varPhi).addImm(UnitSize);
7084 AddDefaultPred(MIB);
7085 } else {
7086 MachineInstrBuilder MIB =
7087 BuildMI(*BB, BB->end(), dl,
7088 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7089 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7090 MIB->getOperand(5).setReg(ARM::CPSR);
7091 MIB->getOperand(5).setIsDef(true);
7092 }
7093 BuildMI(*BB, BB->end(), dl,
7094 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7095 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007096
7097 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7098 BB->addSuccessor(loopMBB);
7099 BB->addSuccessor(exitMBB);
7100
7101 // Add epilogue to handle BytesLeft.
7102 BB = exitMBB;
7103 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007104
7105 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7106 // [destOut] = STRB_POST(scratch, destLoop, 1)
7107 unsigned srcIn = srcLoop;
7108 unsigned destIn = destLoop;
7109 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007110 unsigned srcOut = MRI.createVirtualRegister(TRC);
7111 unsigned destOut = MRI.createVirtualRegister(TRC);
7112 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007113 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7114 IsThumb1, IsThumb2);
7115 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7116 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007117 srcIn = srcOut;
7118 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007119 }
7120
7121 MI->eraseFromParent(); // The instruction is gone now.
7122 return BB;
7123}
7124
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00007125MachineBasicBlock *
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007126ARMTargetLowering::EmitLowered__chkstk(MachineInstr *MI,
7127 MachineBasicBlock *MBB) const {
7128 const TargetMachine &TM = getTargetMachine();
7129 const TargetInstrInfo &TII = *TM.getInstrInfo();
7130 DebugLoc DL = MI->getDebugLoc();
7131
7132 assert(Subtarget->isTargetWindows() &&
7133 "__chkstk is only supported on Windows");
7134 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
7135
7136 // __chkstk takes the number of words to allocate on the stack in R4, and
7137 // returns the stack adjustment in number of bytes in R4. This will not
7138 // clober any other registers (other than the obvious lr).
7139 //
7140 // Although, technically, IP should be considered a register which may be
7141 // clobbered, the call itself will not touch it. Windows on ARM is a pure
7142 // thumb-2 environment, so there is no interworking required. As a result, we
7143 // do not expect a veneer to be emitted by the linker, clobbering IP.
7144 //
7145 // Each module recieves its own copy of __chkstk, so no import thunk is
7146 // required, again, ensuring that IP is not clobbered.
7147 //
7148 // Finally, although some linkers may theoretically provide a trampoline for
7149 // out of range calls (which is quite common due to a 32M range limitation of
7150 // branches for Thumb), we can generate the long-call version via
7151 // -mcmodel=large, alleviating the need for the trampoline which may clobber
7152 // IP.
7153
7154 switch (TM.getCodeModel()) {
7155 case CodeModel::Small:
7156 case CodeModel::Medium:
7157 case CodeModel::Default:
7158 case CodeModel::Kernel:
7159 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
7160 .addImm((unsigned)ARMCC::AL).addReg(0)
7161 .addExternalSymbol("__chkstk")
7162 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7163 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7164 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7165 break;
7166 case CodeModel::Large:
7167 case CodeModel::JITDefault: {
7168 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
7169 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
7170
7171 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
7172 .addExternalSymbol("__chkstk");
7173 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
7174 .addImm((unsigned)ARMCC::AL).addReg(0)
7175 .addReg(Reg, RegState::Kill)
7176 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7177 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7178 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7179 break;
7180 }
7181 }
7182
7183 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr),
7184 ARM::SP)
7185 .addReg(ARM::SP, RegState::Define)
7186 .addReg(ARM::R4, RegState::Kill)));
7187
7188 MI->eraseFromParent();
7189 return MBB;
7190}
7191
7192MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00007193ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00007194 MachineBasicBlock *BB) const {
Evan Cheng10043e22007-01-19 07:51:42 +00007195 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00007196 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00007197 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00007198 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00007199 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00007200 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00007201 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00007202 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00007203 // The Thumb2 pre-indexed stores have the same MI operands, they just
7204 // define them differently in the .td files from the isel patterns, so
7205 // they need pseudos.
7206 case ARM::t2STR_preidx:
7207 MI->setDesc(TII->get(ARM::t2STR_PRE));
7208 return BB;
7209 case ARM::t2STRB_preidx:
7210 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7211 return BB;
7212 case ARM::t2STRH_preidx:
7213 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7214 return BB;
7215
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007216 case ARM::STRi_preidx:
7217 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00007218 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007219 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7220 // Decode the offset.
7221 unsigned Offset = MI->getOperand(4).getImm();
7222 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7223 Offset = ARM_AM::getAM2Offset(Offset);
7224 if (isSub)
7225 Offset = -Offset;
7226
Jim Grosbachf402f692011-08-12 21:02:34 +00007227 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00007228 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007229 .addOperand(MI->getOperand(0)) // Rn_wb
7230 .addOperand(MI->getOperand(1)) // Rt
7231 .addOperand(MI->getOperand(2)) // Rn
7232 .addImm(Offset) // offset (skip GPR==zero_reg)
7233 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00007234 .addOperand(MI->getOperand(6))
7235 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007236 MI->eraseFromParent();
7237 return BB;
7238 }
7239 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00007240 case ARM::STRBr_preidx:
7241 case ARM::STRH_preidx: {
7242 unsigned NewOpc;
7243 switch (MI->getOpcode()) {
7244 default: llvm_unreachable("unexpected opcode!");
7245 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7246 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7247 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7248 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007249 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7250 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7251 MIB.addOperand(MI->getOperand(i));
7252 MI->eraseFromParent();
7253 return BB;
7254 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00007255
Evan Chengbb2af352009-08-12 05:17:19 +00007256 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00007257 // To "insert" a SELECT_CC instruction, we actually have to insert the
7258 // diamond control-flow pattern. The incoming instruction knows the
7259 // destination vreg to set, the condition code register to branch on, the
7260 // true/false values to select between, and a branch opcode to use.
7261 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00007262 MachineFunction::iterator It = BB;
Evan Cheng10043e22007-01-19 07:51:42 +00007263 ++It;
7264
7265 // thisMBB:
7266 // ...
7267 // TrueVal = ...
7268 // cmpTY ccX, r1, r2
7269 // bCC copy1MBB
7270 // fallthrough --> copy0MBB
7271 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00007272 MachineFunction *F = BB->getParent();
7273 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7274 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00007275 F->insert(It, copy0MBB);
7276 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007277
7278 // Transfer the remainder of BB and its successor edges to sinkMBB.
7279 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007280 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007281 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7282
Dan Gohmanf4f04102010-07-06 15:49:48 +00007283 BB->addSuccessor(copy0MBB);
7284 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00007285
Dan Gohman34396292010-07-06 20:24:04 +00007286 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7287 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7288
Evan Cheng10043e22007-01-19 07:51:42 +00007289 // copy0MBB:
7290 // %FalseValue = ...
7291 // # fallthrough to sinkMBB
7292 BB = copy0MBB;
7293
7294 // Update machine-CFG edges
7295 BB->addSuccessor(sinkMBB);
7296
7297 // sinkMBB:
7298 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7299 // ...
7300 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007301 BuildMI(*BB, BB->begin(), dl,
7302 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00007303 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7304 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7305
Dan Gohman34396292010-07-06 20:24:04 +00007306 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00007307 return BB;
7308 }
Evan Chengb972e562009-08-07 00:34:42 +00007309
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007310 case ARM::BCCi64:
7311 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00007312 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007313 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00007314
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007315 // Compare both parts that make up the double comparison separately for
7316 // equality.
7317 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7318
7319 unsigned LHS1 = MI->getOperand(1).getReg();
7320 unsigned LHS2 = MI->getOperand(2).getReg();
7321 if (RHSisZero) {
7322 AddDefaultPred(BuildMI(BB, dl,
7323 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7324 .addReg(LHS1).addImm(0));
7325 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7326 .addReg(LHS2).addImm(0)
7327 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7328 } else {
7329 unsigned RHS1 = MI->getOperand(3).getReg();
7330 unsigned RHS2 = MI->getOperand(4).getReg();
7331 AddDefaultPred(BuildMI(BB, dl,
7332 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7333 .addReg(LHS1).addReg(RHS1));
7334 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7335 .addReg(LHS2).addReg(RHS2)
7336 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7337 }
7338
7339 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7340 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7341 if (MI->getOperand(0).getImm() == ARMCC::NE)
7342 std::swap(destMBB, exitMBB);
7343
7344 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7345 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00007346 if (isThumb2)
7347 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7348 else
7349 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007350
7351 MI->eraseFromParent(); // The pseudo instruction is gone now.
7352 return BB;
7353 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007354
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007355 case ARM::Int_eh_sjlj_setjmp:
7356 case ARM::Int_eh_sjlj_setjmp_nofp:
7357 case ARM::tInt_eh_sjlj_setjmp:
7358 case ARM::t2Int_eh_sjlj_setjmp:
7359 case ARM::t2Int_eh_sjlj_setjmp_nofp:
7360 EmitSjLjDispatchBlock(MI, BB);
7361 return BB;
7362
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007363 case ARM::ABS:
7364 case ARM::t2ABS: {
7365 // To insert an ABS instruction, we have to insert the
7366 // diamond control-flow pattern. The incoming instruction knows the
7367 // source vreg to test against 0, the destination vreg to set,
7368 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00007369 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007370 // It transforms
7371 // V1 = ABS V0
7372 // into
7373 // V2 = MOVS V0
7374 // BCC (branch to SinkBB if V0 >= 0)
7375 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00007376 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007377 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7378 MachineFunction::iterator BBI = BB;
7379 ++BBI;
7380 MachineFunction *Fn = BB->getParent();
7381 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7382 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7383 Fn->insert(BBI, RSBBB);
7384 Fn->insert(BBI, SinkBB);
7385
7386 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7387 unsigned int ABSDstReg = MI->getOperand(0).getReg();
7388 bool isThumb2 = Subtarget->isThumb2();
7389 MachineRegisterInfo &MRI = Fn->getRegInfo();
7390 // In Thumb mode S must not be specified if source register is the SP or
7391 // PC and if destination register is the SP, so restrict register class
Craig Topperc7242e02012-04-20 07:30:17 +00007392 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
7393 (const TargetRegisterClass*)&ARM::rGPRRegClass :
7394 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007395
7396 // Transfer the remainder of BB and its successor edges to sinkMBB.
7397 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007398 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007399 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7400
7401 BB->addSuccessor(RSBBB);
7402 BB->addSuccessor(SinkBB);
7403
7404 // fall through to SinkMBB
7405 RSBBB->addSuccessor(SinkBB);
7406
Manman Rene0763c72012-06-15 21:32:12 +00007407 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00007408 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00007409 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7410 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007411
7412 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00007413 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007414 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7415 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7416
7417 // insert rsbri in RSBBB
7418 // Note: BCC and rsbri will be converted into predicated rsbmi
7419 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00007420 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007421 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Rene0763c72012-06-15 21:32:12 +00007422 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007423 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7424
Andrew Trick3f07c422011-10-18 18:40:53 +00007425 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007426 // reuse ABSDstReg to not change uses of ABS instruction
7427 BuildMI(*SinkBB, SinkBB->begin(), dl,
7428 TII->get(ARM::PHI), ABSDstReg)
7429 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00007430 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007431
7432 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00007433 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007434
7435 // return last added BB
7436 return SinkBB;
7437 }
Manman Rene8735522012-06-01 19:33:18 +00007438 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00007439 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00007440 return EmitStructByval(MI, BB);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007441 case ARM::WIN__CHKSTK:
7442 return EmitLowered__chkstk(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00007443 }
7444}
7445
Evan Chenge6fba772011-08-30 19:09:48 +00007446void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7447 SDNode *Node) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007448 if (!MI->hasPostISelHook()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007449 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
7450 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
7451 return;
7452 }
7453
Evan Cheng7f8e5632011-12-07 07:15:52 +00007454 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00007455 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7456 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7457 // operand is still set to noreg. If needed, set the optional operand's
7458 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00007459 //
Andrew Trick88b24502011-10-18 19:18:52 +00007460 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00007461
Andrew Trick924123a2011-09-21 02:20:46 +00007462 // Rename pseudo opcodes.
7463 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7464 if (NewOpc) {
7465 const ARMBaseInstrInfo *TII =
7466 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick88b24502011-10-18 19:18:52 +00007467 MCID = &TII->get(NewOpc);
7468
7469 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7470 "converted opcode should be the same except for cc_out");
7471
7472 MI->setDesc(*MCID);
7473
7474 // Add the optional cc_out operand
7475 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00007476 }
Andrew Trick88b24502011-10-18 19:18:52 +00007477 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00007478
7479 // Any ARM instruction that sets the 's' bit should specify an optional
7480 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00007481 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007482 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007483 return;
7484 }
Andrew Trick924123a2011-09-21 02:20:46 +00007485 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7486 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007487 bool definesCPSR = false;
7488 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00007489 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00007490 i != e; ++i) {
7491 const MachineOperand &MO = MI->getOperand(i);
7492 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7493 definesCPSR = true;
7494 if (MO.isDead())
7495 deadCPSR = true;
7496 MI->RemoveOperand(i);
7497 break;
Evan Chenge6fba772011-08-30 19:09:48 +00007498 }
7499 }
Andrew Trick8586e622011-09-20 03:17:40 +00007500 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00007501 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007502 return;
7503 }
7504 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00007505 if (deadCPSR) {
7506 assert(!MI->getOperand(ccOutIdx).getReg() &&
7507 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00007508 return;
Andrew Trick924123a2011-09-21 02:20:46 +00007509 }
Andrew Trick8586e622011-09-20 03:17:40 +00007510
Andrew Trick924123a2011-09-21 02:20:46 +00007511 // If this instruction was defined with an optional CPSR def and its dag node
7512 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007513 MachineOperand &MO = MI->getOperand(ccOutIdx);
7514 MO.setReg(ARM::CPSR);
7515 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00007516}
7517
Evan Cheng10043e22007-01-19 07:51:42 +00007518//===----------------------------------------------------------------------===//
7519// ARM Optimization Hooks
7520//===----------------------------------------------------------------------===//
7521
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007522// Helper function that checks if N is a null or all ones constant.
7523static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7524 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7525 if (!C)
7526 return false;
7527 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7528}
7529
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007530// Return true if N is conditionally 0 or all ones.
7531// Detects these expressions where cc is an i1 value:
7532//
7533// (select cc 0, y) [AllOnes=0]
7534// (select cc y, 0) [AllOnes=0]
7535// (zext cc) [AllOnes=0]
7536// (sext cc) [AllOnes=0/1]
7537// (select cc -1, y) [AllOnes=1]
7538// (select cc y, -1) [AllOnes=1]
7539//
7540// Invert is set when N is the null/all ones constant when CC is false.
7541// OtherOp is set to the alternative value of N.
7542static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7543 SDValue &CC, bool &Invert,
7544 SDValue &OtherOp,
7545 SelectionDAG &DAG) {
7546 switch (N->getOpcode()) {
7547 default: return false;
7548 case ISD::SELECT: {
7549 CC = N->getOperand(0);
7550 SDValue N1 = N->getOperand(1);
7551 SDValue N2 = N->getOperand(2);
7552 if (isZeroOrAllOnes(N1, AllOnes)) {
7553 Invert = false;
7554 OtherOp = N2;
7555 return true;
7556 }
7557 if (isZeroOrAllOnes(N2, AllOnes)) {
7558 Invert = true;
7559 OtherOp = N1;
7560 return true;
7561 }
7562 return false;
7563 }
7564 case ISD::ZERO_EXTEND:
7565 // (zext cc) can never be the all ones value.
7566 if (AllOnes)
7567 return false;
7568 // Fall through.
7569 case ISD::SIGN_EXTEND: {
7570 EVT VT = N->getValueType(0);
7571 CC = N->getOperand(0);
7572 if (CC.getValueType() != MVT::i1)
7573 return false;
7574 Invert = !AllOnes;
7575 if (AllOnes)
7576 // When looking for an AllOnes constant, N is an sext, and the 'other'
7577 // value is 0.
7578 OtherOp = DAG.getConstant(0, VT);
7579 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7580 // When looking for a 0 constant, N can be zext or sext.
7581 OtherOp = DAG.getConstant(1, VT);
7582 else
7583 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7584 return true;
7585 }
7586 }
7587}
7588
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007589// Combine a constant select operand into its use:
7590//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007591// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7592// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7593// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7594// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7595// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007596//
7597// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007598// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007599//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007600// Also recognize sext/zext from i1:
7601//
7602// (add (zext cc), x) -> (select cc (add x, 1), x)
7603// (add (sext cc), x) -> (select cc (add x, -1), x)
7604//
7605// These transformations eventually create predicated instructions.
7606//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007607// @param N The node to transform.
7608// @param Slct The N operand that is a select.
7609// @param OtherOp The other N operand (x above).
7610// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007611// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007612// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00007613static
7614SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007615 TargetLowering::DAGCombinerInfo &DCI,
7616 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00007617 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00007618 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007619 SDValue NonConstantVal;
7620 SDValue CCOp;
7621 bool SwapSelectOps;
7622 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7623 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007624 return SDValue();
7625
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007626 // Slct is now know to be the desired identity constant when CC is true.
7627 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007628 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007629 OtherOp, NonConstantVal);
7630 // Unless SwapSelectOps says CC should be false.
7631 if (SwapSelectOps)
7632 std::swap(TrueVal, FalseVal);
7633
Andrew Trickef9de2a2013-05-25 02:42:55 +00007634 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007635 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00007636}
7637
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007638// Attempt combineSelectAndUse on each operand of a commutative operator N.
7639static
7640SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7641 TargetLowering::DAGCombinerInfo &DCI) {
7642 SDValue N0 = N->getOperand(0);
7643 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007644 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007645 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7646 if (Result.getNode())
7647 return Result;
7648 }
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007649 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007650 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7651 if (Result.getNode())
7652 return Result;
7653 }
7654 return SDValue();
7655}
7656
Eric Christopher1b8b94192011-06-29 21:10:36 +00007657// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00007658// (only after legalization).
7659static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7660 TargetLowering::DAGCombinerInfo &DCI,
7661 const ARMSubtarget *Subtarget) {
7662
7663 // Only perform optimization if after legalize, and if NEON is available. We
7664 // also expected both operands to be BUILD_VECTORs.
7665 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7666 || N0.getOpcode() != ISD::BUILD_VECTOR
7667 || N1.getOpcode() != ISD::BUILD_VECTOR)
7668 return SDValue();
7669
7670 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7671 EVT VT = N->getValueType(0);
7672 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7673 return SDValue();
7674
7675 // Check that the vector operands are of the right form.
7676 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7677 // operands, where N is the size of the formed vector.
7678 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7679 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007680
7681 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00007682 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00007683 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00007684 SDValue Vec = N0->getOperand(0)->getOperand(0);
7685 SDNode *V = Vec.getNode();
7686 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00007687
Eric Christopher1b8b94192011-06-29 21:10:36 +00007688 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007689 // check to see if each of their operands are an EXTRACT_VECTOR with
7690 // the same vector and appropriate index.
7691 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7692 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7693 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00007694
Tanya Lattnere9e67052011-06-14 23:48:48 +00007695 SDValue ExtVec0 = N0->getOperand(i);
7696 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007697
Tanya Lattnere9e67052011-06-14 23:48:48 +00007698 // First operand is the vector, verify its the same.
7699 if (V != ExtVec0->getOperand(0).getNode() ||
7700 V != ExtVec1->getOperand(0).getNode())
7701 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00007702
Tanya Lattnere9e67052011-06-14 23:48:48 +00007703 // Second is the constant, verify its correct.
7704 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7705 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00007706
Tanya Lattnere9e67052011-06-14 23:48:48 +00007707 // For the constant, we want to see all the even or all the odd.
7708 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7709 || C1->getZExtValue() != nextIndex+1)
7710 return SDValue();
7711
7712 // Increment index.
7713 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00007714 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00007715 return SDValue();
7716 }
7717
7718 // Create VPADDL node.
7719 SelectionDAG &DAG = DCI.DAG;
7720 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00007721
7722 // Build operand list.
7723 SmallVector<SDValue, 8> Ops;
7724 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7725 TLI.getPointerTy()));
7726
7727 // Input is the vector.
7728 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007729
Tanya Lattnere9e67052011-06-14 23:48:48 +00007730 // Get widened type and narrowed type.
7731 MVT widenType;
7732 unsigned numElem = VT.getVectorNumElements();
Silviu Barangaa3106e62014-04-03 10:44:27 +00007733
7734 EVT inputLaneType = Vec.getValueType().getVectorElementType();
7735 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00007736 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7737 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7738 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7739 default:
Craig Toppere55c5562012-02-07 02:50:20 +00007740 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00007741 }
7742
Craig Topper48d114b2014-04-26 18:35:24 +00007743 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N), widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00007744 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
7745 return DAG.getNode(ExtOp, SDLoc(N), VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00007746}
7747
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007748static SDValue findMUL_LOHI(SDValue V) {
7749 if (V->getOpcode() == ISD::UMUL_LOHI ||
7750 V->getOpcode() == ISD::SMUL_LOHI)
7751 return V;
7752 return SDValue();
7753}
7754
7755static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7756 TargetLowering::DAGCombinerInfo &DCI,
7757 const ARMSubtarget *Subtarget) {
7758
7759 if (Subtarget->isThumb1Only()) return SDValue();
7760
7761 // Only perform the checks after legalize when the pattern is available.
7762 if (DCI.isBeforeLegalize()) return SDValue();
7763
7764 // Look for multiply add opportunities.
7765 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7766 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7767 // a glue link from the first add to the second add.
7768 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
7769 // a S/UMLAL instruction.
7770 // loAdd UMUL_LOHI
7771 // \ / :lo \ :hi
7772 // \ / \ [no multiline comment]
7773 // ADDC | hiAdd
7774 // \ :glue / /
7775 // \ / /
7776 // ADDE
7777 //
7778 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
7779 SDValue AddcOp0 = AddcNode->getOperand(0);
7780 SDValue AddcOp1 = AddcNode->getOperand(1);
7781
7782 // Check if the two operands are from the same mul_lohi node.
7783 if (AddcOp0.getNode() == AddcOp1.getNode())
7784 return SDValue();
7785
7786 assert(AddcNode->getNumValues() == 2 &&
7787 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00007788 "Expect ADDC with two result values. First: i32");
7789
7790 // Check that we have a glued ADDC node.
7791 if (AddcNode->getValueType(1) != MVT::Glue)
7792 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007793
7794 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
7795 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
7796 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
7797 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
7798 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
7799 return SDValue();
7800
7801 // Look for the glued ADDE.
7802 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00007803 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007804 return SDValue();
7805
7806 // Make sure it is really an ADDE.
7807 if (AddeNode->getOpcode() != ISD::ADDE)
7808 return SDValue();
7809
7810 assert(AddeNode->getNumOperands() == 3 &&
7811 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
7812 "ADDE node has the wrong inputs");
7813
7814 // Check for the triangle shape.
7815 SDValue AddeOp0 = AddeNode->getOperand(0);
7816 SDValue AddeOp1 = AddeNode->getOperand(1);
7817
7818 // Make sure that the ADDE operands are not coming from the same node.
7819 if (AddeOp0.getNode() == AddeOp1.getNode())
7820 return SDValue();
7821
7822 // Find the MUL_LOHI node walking up ADDE's operands.
7823 bool IsLeftOperandMUL = false;
7824 SDValue MULOp = findMUL_LOHI(AddeOp0);
7825 if (MULOp == SDValue())
7826 MULOp = findMUL_LOHI(AddeOp1);
7827 else
7828 IsLeftOperandMUL = true;
7829 if (MULOp == SDValue())
7830 return SDValue();
7831
7832 // Figure out the right opcode.
7833 unsigned Opc = MULOp->getOpcode();
7834 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
7835
7836 // Figure out the high and low input values to the MLAL node.
7837 SDValue* HiMul = &MULOp;
Craig Topper062a2ba2014-04-25 05:30:21 +00007838 SDValue* HiAdd = nullptr;
7839 SDValue* LoMul = nullptr;
7840 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007841
7842 if (IsLeftOperandMUL)
7843 HiAdd = &AddeOp1;
7844 else
7845 HiAdd = &AddeOp0;
7846
7847
7848 if (AddcOp0->getOpcode() == Opc) {
7849 LoMul = &AddcOp0;
7850 LowAdd = &AddcOp1;
7851 }
7852 if (AddcOp1->getOpcode() == Opc) {
7853 LoMul = &AddcOp1;
7854 LowAdd = &AddcOp0;
7855 }
7856
Craig Topper062a2ba2014-04-25 05:30:21 +00007857 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007858 return SDValue();
7859
7860 if (LoMul->getNode() != HiMul->getNode())
7861 return SDValue();
7862
7863 // Create the merged node.
7864 SelectionDAG &DAG = DCI.DAG;
7865
7866 // Build operand list.
7867 SmallVector<SDValue, 8> Ops;
7868 Ops.push_back(LoMul->getOperand(0));
7869 Ops.push_back(LoMul->getOperand(1));
7870 Ops.push_back(*LowAdd);
7871 Ops.push_back(*HiAdd);
7872
Andrew Trickef9de2a2013-05-25 02:42:55 +00007873 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00007874 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007875
7876 // Replace the ADDs' nodes uses by the MLA node's values.
7877 SDValue HiMLALResult(MLALNode.getNode(), 1);
7878 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
7879
7880 SDValue LoMLALResult(MLALNode.getNode(), 0);
7881 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
7882
7883 // Return original node to notify the driver to stop replacing.
7884 SDValue resNode(AddcNode, 0);
7885 return resNode;
7886}
7887
7888/// PerformADDCCombine - Target-specific dag combine transform from
7889/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
7890static SDValue PerformADDCCombine(SDNode *N,
7891 TargetLowering::DAGCombinerInfo &DCI,
7892 const ARMSubtarget *Subtarget) {
7893
7894 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
7895
7896}
7897
Bob Wilson728eb292010-07-29 20:34:14 +00007898/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7899/// operands N0 and N1. This is a helper for PerformADDCombine that is
7900/// called with the default operands, and if that fails, with commuted
7901/// operands.
7902static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007903 TargetLowering::DAGCombinerInfo &DCI,
7904 const ARMSubtarget *Subtarget){
7905
7906 // Attempt to create vpaddl for this add.
7907 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7908 if (Result.getNode())
7909 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00007910
Chris Lattner4147f082009-03-12 06:52:53 +00007911 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007912 if (N0.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00007913 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7914 if (Result.getNode()) return Result;
7915 }
Chris Lattner4147f082009-03-12 06:52:53 +00007916 return SDValue();
7917}
7918
Bob Wilson728eb292010-07-29 20:34:14 +00007919/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7920///
7921static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007922 TargetLowering::DAGCombinerInfo &DCI,
7923 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00007924 SDValue N0 = N->getOperand(0);
7925 SDValue N1 = N->getOperand(1);
7926
7927 // First try with the default operand order.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007928 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00007929 if (Result.getNode())
7930 return Result;
7931
7932 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007933 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00007934}
7935
Chris Lattner4147f082009-03-12 06:52:53 +00007936/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00007937///
Chris Lattner4147f082009-03-12 06:52:53 +00007938static SDValue PerformSUBCombine(SDNode *N,
7939 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00007940 SDValue N0 = N->getOperand(0);
7941 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00007942
Chris Lattner4147f082009-03-12 06:52:53 +00007943 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007944 if (N1.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00007945 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7946 if (Result.getNode()) return Result;
7947 }
Bob Wilson7117a912009-03-20 22:42:55 +00007948
Chris Lattner4147f082009-03-12 06:52:53 +00007949 return SDValue();
7950}
7951
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007952/// PerformVMULCombine
7953/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7954/// special multiplier accumulator forwarding.
7955/// vmul d3, d0, d2
7956/// vmla d3, d1, d2
7957/// is faster than
7958/// vadd d3, d0, d1
7959/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00007960// However, for (A + B) * (A + B),
7961// vadd d2, d0, d1
7962// vmul d3, d0, d2
7963// vmla d3, d1, d2
7964// is slower than
7965// vadd d2, d0, d1
7966// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007967static SDValue PerformVMULCombine(SDNode *N,
7968 TargetLowering::DAGCombinerInfo &DCI,
7969 const ARMSubtarget *Subtarget) {
7970 if (!Subtarget->hasVMLxForwarding())
7971 return SDValue();
7972
7973 SelectionDAG &DAG = DCI.DAG;
7974 SDValue N0 = N->getOperand(0);
7975 SDValue N1 = N->getOperand(1);
7976 unsigned Opcode = N0.getOpcode();
7977 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7978 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00007979 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007980 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7981 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7982 return SDValue();
7983 std::swap(N0, N1);
7984 }
7985
Weiming Zhao2052f482013-09-25 23:12:06 +00007986 if (N0 == N1)
7987 return SDValue();
7988
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007989 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00007990 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007991 SDValue N00 = N0->getOperand(0);
7992 SDValue N01 = N0->getOperand(1);
7993 return DAG.getNode(Opcode, DL, VT,
7994 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7995 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7996}
7997
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007998static SDValue PerformMULCombine(SDNode *N,
7999 TargetLowering::DAGCombinerInfo &DCI,
8000 const ARMSubtarget *Subtarget) {
8001 SelectionDAG &DAG = DCI.DAG;
8002
8003 if (Subtarget->isThumb1Only())
8004 return SDValue();
8005
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008006 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8007 return SDValue();
8008
8009 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008010 if (VT.is64BitVector() || VT.is128BitVector())
8011 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008012 if (VT != MVT::i32)
8013 return SDValue();
8014
8015 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8016 if (!C)
8017 return SDValue();
8018
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008019 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008020 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008021
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008022 ShiftAmt = ShiftAmt & (32 - 1);
8023 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008024 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008025
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008026 SDValue Res;
8027 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008028
8029 if (MulAmt >= 0) {
8030 if (isPowerOf2_32(MulAmt - 1)) {
8031 // (mul x, 2^N + 1) => (add (shl x, N), x)
8032 Res = DAG.getNode(ISD::ADD, DL, VT,
8033 V,
8034 DAG.getNode(ISD::SHL, DL, VT,
8035 V,
8036 DAG.getConstant(Log2_32(MulAmt - 1),
8037 MVT::i32)));
8038 } else if (isPowerOf2_32(MulAmt + 1)) {
8039 // (mul x, 2^N - 1) => (sub (shl x, N), x)
8040 Res = DAG.getNode(ISD::SUB, DL, VT,
8041 DAG.getNode(ISD::SHL, DL, VT,
8042 V,
8043 DAG.getConstant(Log2_32(MulAmt + 1),
8044 MVT::i32)),
8045 V);
8046 } else
8047 return SDValue();
8048 } else {
8049 uint64_t MulAmtAbs = -MulAmt;
8050 if (isPowerOf2_32(MulAmtAbs + 1)) {
8051 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8052 Res = DAG.getNode(ISD::SUB, DL, VT,
8053 V,
8054 DAG.getNode(ISD::SHL, DL, VT,
8055 V,
8056 DAG.getConstant(Log2_32(MulAmtAbs + 1),
8057 MVT::i32)));
8058 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8059 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8060 Res = DAG.getNode(ISD::ADD, DL, VT,
8061 V,
8062 DAG.getNode(ISD::SHL, DL, VT,
8063 V,
8064 DAG.getConstant(Log2_32(MulAmtAbs-1),
8065 MVT::i32)));
8066 Res = DAG.getNode(ISD::SUB, DL, VT,
8067 DAG.getConstant(0, MVT::i32),Res);
8068
8069 } else
8070 return SDValue();
8071 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008072
8073 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008074 Res = DAG.getNode(ISD::SHL, DL, VT,
8075 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008076
8077 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008078 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008079 return SDValue();
8080}
8081
Owen Anderson30c48922010-11-05 19:27:46 +00008082static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00008083 TargetLowering::DAGCombinerInfo &DCI,
8084 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00008085
Owen Anderson30c48922010-11-05 19:27:46 +00008086 // Attempt to use immediate-form VBIC
8087 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008088 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00008089 EVT VT = N->getValueType(0);
8090 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008091
Tanya Lattner266792a2011-04-07 15:24:20 +00008092 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8093 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008094
Owen Anderson30c48922010-11-05 19:27:46 +00008095 APInt SplatBits, SplatUndef;
8096 unsigned SplatBitSize;
8097 bool HasAnyUndefs;
8098 if (BVN &&
8099 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8100 if (SplatBitSize <= 64) {
8101 EVT VbicVT;
8102 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8103 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00008104 DAG, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008105 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00008106 if (Val.getNode()) {
8107 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008108 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00008109 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008110 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00008111 }
8112 }
8113 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008114
Evan Chenge87681c2012-02-23 01:19:06 +00008115 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008116 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8117 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8118 if (Result.getNode())
8119 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008120 }
8121
Owen Anderson30c48922010-11-05 19:27:46 +00008122 return SDValue();
8123}
8124
Jim Grosbach11013ed2010-07-16 23:05:05 +00008125/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8126static SDValue PerformORCombine(SDNode *N,
8127 TargetLowering::DAGCombinerInfo &DCI,
8128 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008129 // Attempt to use immediate-form VORR
8130 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008131 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008132 EVT VT = N->getValueType(0);
8133 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008134
Tanya Lattner266792a2011-04-07 15:24:20 +00008135 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8136 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008137
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008138 APInt SplatBits, SplatUndef;
8139 unsigned SplatBitSize;
8140 bool HasAnyUndefs;
8141 if (BVN && Subtarget->hasNEON() &&
8142 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8143 if (SplatBitSize <= 64) {
8144 EVT VorrVT;
8145 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8146 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00008147 DAG, VorrVT, VT.is128BitVector(),
8148 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008149 if (Val.getNode()) {
8150 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008151 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008152 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008153 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008154 }
8155 }
8156 }
8157
Evan Chenge87681c2012-02-23 01:19:06 +00008158 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008159 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8160 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8161 if (Result.getNode())
8162 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008163 }
8164
Nadav Rotem3a94c542012-08-13 18:52:44 +00008165 // The code below optimizes (or (and X, Y), Z).
8166 // The AND operand needs to have a single user to make these optimizations
8167 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008168 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00008169 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008170 return SDValue();
8171 SDValue N1 = N->getOperand(1);
8172
8173 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8174 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8175 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8176 APInt SplatUndef;
8177 unsigned SplatBitSize;
8178 bool HasAnyUndefs;
8179
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008180 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008181 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008182 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8183 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008184 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008185 HasAnyUndefs) && !HasAnyUndefs) {
8186 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8187 HasAnyUndefs) && !HasAnyUndefs) {
8188 // Ensure that the bit width of the constants are the same and that
8189 // the splat arguments are logical inverses as per the pattern we
8190 // are trying to simplify.
8191 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
8192 SplatBits0 == ~SplatBits1) {
8193 // Canonicalize the vector type to make instruction selection
8194 // simpler.
8195 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8196 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8197 N0->getOperand(1),
8198 N0->getOperand(0),
8199 N1->getOperand(0));
8200 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8201 }
8202 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008203 }
8204 }
8205
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008206 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8207 // reasonable.
8208
Jim Grosbach11013ed2010-07-16 23:05:05 +00008209 // BFI is only available on V6T2+
8210 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8211 return SDValue();
8212
Andrew Trickef9de2a2013-05-25 02:42:55 +00008213 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008214 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008215 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008216 //
8217 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008218 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008219 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008220 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008221 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008222 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008223
Jim Grosbach11013ed2010-07-16 23:05:05 +00008224 if (VT != MVT::i32)
8225 return SDValue();
8226
Evan Cheng2e51bb42010-12-13 20:32:54 +00008227 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008228
Jim Grosbach11013ed2010-07-16 23:05:05 +00008229 // The value and the mask need to be constants so we can verify this is
8230 // actually a bitfield set. If the mask is 0xffff, we can do better
8231 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00008232 SDValue MaskOp = N0.getOperand(1);
8233 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8234 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008235 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008236 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008237 if (Mask == 0xffff)
8238 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008239 SDValue Res;
8240 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008241 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8242 if (N1C) {
8243 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00008244 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008245 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008246
Evan Cheng34345752010-12-11 04:11:38 +00008247 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008248 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00008249
Evan Cheng2e51bb42010-12-13 20:32:54 +00008250 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Cheng34345752010-12-11 04:11:38 +00008251 DAG.getConstant(Val, MVT::i32),
8252 DAG.getConstant(Mask, MVT::i32));
8253
8254 // Do not add new nodes to DAG combiner worklist.
8255 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008256 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00008257 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008258 } else if (N1.getOpcode() == ISD::AND) {
8259 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008260 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8261 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008262 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008263 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008264
Eric Christopherd5530962011-03-26 01:21:03 +00008265 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8266 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008267 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008268 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008269 // The pack halfword instruction works better for masks that fit it,
8270 // so use that when it's available.
8271 if (Subtarget->hasT2ExtractPack() &&
8272 (Mask == 0xffff || Mask == 0xffff0000))
8273 return SDValue();
8274 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008275 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008276 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopherd5530962011-03-26 01:21:03 +00008277 DAG.getConstant(amt, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008278 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008279 DAG.getConstant(Mask, MVT::i32));
8280 // Do not add new nodes to DAG combiner worklist.
8281 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008282 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008283 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008284 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008285 // The pack halfword instruction works better for masks that fit it,
8286 // so use that when it's available.
8287 if (Subtarget->hasT2ExtractPack() &&
8288 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8289 return SDValue();
8290 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008291 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008292 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008293 DAG.getConstant(lsb, MVT::i32));
8294 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopherd5530962011-03-26 01:21:03 +00008295 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008296 // Do not add new nodes to DAG combiner worklist.
8297 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008298 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008299 }
8300 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008301
Evan Cheng2e51bb42010-12-13 20:32:54 +00008302 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8303 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8304 ARM::isBitFieldInvertedMask(~Mask)) {
8305 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8306 // where lsb(mask) == #shamt and masked bits of B are known zero.
8307 SDValue ShAmt = N00.getOperand(1);
8308 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008309 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008310 if (ShAmtC != LSB)
8311 return SDValue();
8312
8313 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
8314 DAG.getConstant(~Mask, MVT::i32));
8315
8316 // Do not add new nodes to DAG combiner worklist.
8317 DCI.CombineTo(N, Res, false);
8318 }
8319
Jim Grosbach11013ed2010-07-16 23:05:05 +00008320 return SDValue();
8321}
8322
Evan Chenge87681c2012-02-23 01:19:06 +00008323static SDValue PerformXORCombine(SDNode *N,
8324 TargetLowering::DAGCombinerInfo &DCI,
8325 const ARMSubtarget *Subtarget) {
8326 EVT VT = N->getValueType(0);
8327 SelectionDAG &DAG = DCI.DAG;
8328
8329 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8330 return SDValue();
8331
8332 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008333 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8334 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8335 if (Result.getNode())
8336 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008337 }
8338
8339 return SDValue();
8340}
8341
Evan Cheng6d02d902011-06-15 01:12:31 +00008342/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8343/// the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00008344static SDValue PerformBFICombine(SDNode *N,
8345 TargetLowering::DAGCombinerInfo &DCI) {
8346 SDValue N1 = N->getOperand(1);
8347 if (N1.getOpcode() == ISD::AND) {
8348 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8349 if (!N11C)
8350 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008351 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008352 unsigned LSB = countTrailingZeros(~InvMask);
8353 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Evan Cheng6d02d902011-06-15 01:12:31 +00008354 unsigned Mask = (1 << Width)-1;
Evan Chengc1778132010-12-14 03:22:07 +00008355 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008356 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008357 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00008358 N->getOperand(0), N1.getOperand(0),
8359 N->getOperand(2));
8360 }
8361 return SDValue();
8362}
8363
Bob Wilson22806742010-09-22 22:09:21 +00008364/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8365/// ARMISD::VMOVRRD.
8366static SDValue PerformVMOVRRDCombine(SDNode *N,
8367 TargetLowering::DAGCombinerInfo &DCI) {
8368 // vmovrrd(vmovdrr x, y) -> x,y
8369 SDValue InDouble = N->getOperand(0);
8370 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
8371 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008372
8373 // vmovrrd(load f64) -> (load i32), (load i32)
8374 SDNode *InNode = InDouble.getNode();
8375 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8376 InNode->getValueType(0) == MVT::f64 &&
8377 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8378 !cast<LoadSDNode>(InNode)->isVolatile()) {
8379 // TODO: Should this be done for non-FrameIndex operands?
8380 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8381
8382 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008383 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008384 SDValue BasePtr = LD->getBasePtr();
8385 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8386 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008387 LD->isNonTemporal(), LD->isInvariant(),
8388 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008389
8390 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8391 DAG.getConstant(4, MVT::i32));
8392 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8393 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008394 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008395 std::min(4U, LD->getAlignment() / 2));
8396
8397 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
Christian Pirker762b2c62014-06-01 09:30:52 +00008398 if (DCI.DAG.getTargetLoweringInfo().isBigEndian())
8399 std::swap (NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008400 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
8401 DCI.RemoveFromWorklist(LD);
8402 DAG.DeleteNode(LD);
8403 return Result;
8404 }
8405
Bob Wilson22806742010-09-22 22:09:21 +00008406 return SDValue();
8407}
8408
8409/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8410/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8411static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8412 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8413 SDValue Op0 = N->getOperand(0);
8414 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00008415 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008416 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008417 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008418 Op1 = Op1.getOperand(0);
8419 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8420 Op0.getNode() == Op1.getNode() &&
8421 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008422 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00008423 N->getValueType(0), Op0.getOperand(0));
8424 return SDValue();
8425}
8426
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008427/// PerformSTORECombine - Target-specific dag combine xforms for
8428/// ISD::STORE.
8429static SDValue PerformSTORECombine(SDNode *N,
8430 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008431 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008432 if (St->isVolatile())
8433 return SDValue();
8434
Andrew Trickbc325162012-07-18 18:34:24 +00008435 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosiere0e38f62012-04-09 20:32:02 +00008436 // pack all of the elements in one place. Next, store to memory in fewer
8437 // chunks.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008438 SDValue StVal = St->getValue();
Chad Rosiere0e38f62012-04-09 20:32:02 +00008439 EVT VT = StVal.getValueType();
8440 if (St->isTruncatingStore() && VT.isVector()) {
8441 SelectionDAG &DAG = DCI.DAG;
8442 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8443 EVT StVT = St->getMemoryVT();
8444 unsigned NumElems = VT.getVectorNumElements();
8445 assert(StVT != VT && "Cannot truncate to the same type");
8446 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
8447 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
8448
8449 // From, To sizes and ElemCount must be pow of two
8450 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
8451
8452 // We are going to use the original vector elt for storing.
8453 // Accumulated smaller vector elements must be a multiple of the store size.
8454 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
8455
8456 unsigned SizeRatio = FromEltSz / ToEltSz;
8457 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
8458
8459 // Create a type on which we perform the shuffle.
8460 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
8461 NumElems*SizeRatio);
8462 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
8463
Andrew Trickef9de2a2013-05-25 02:42:55 +00008464 SDLoc DL(St);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008465 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
8466 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Christian Pirker2cc1cf02014-06-16 09:17:30 +00008467 for (unsigned i = 0; i < NumElems; ++i)
8468 ShuffleVec[i] = TLI.isBigEndian() ? (i+1) * SizeRatio - 1 : i * SizeRatio;
Chad Rosiere0e38f62012-04-09 20:32:02 +00008469
8470 // Can't shuffle using an illegal type.
8471 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
8472
8473 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
8474 DAG.getUNDEF(WideVec.getValueType()),
8475 ShuffleVec.data());
8476 // At this point all of the data is stored at the bottom of the
8477 // register. We now need to save it to mem.
8478
8479 // Find the largest store unit
8480 MVT StoreType = MVT::i8;
8481 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
8482 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
8483 MVT Tp = (MVT::SimpleValueType)tp;
8484 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
8485 StoreType = Tp;
8486 }
8487 // Didn't find a legal store type.
8488 if (!TLI.isTypeLegal(StoreType))
8489 return SDValue();
8490
8491 // Bitcast the original vector into a vector of store-size units
8492 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
8493 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
8494 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
8495 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
8496 SmallVector<SDValue, 8> Chains;
8497 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
8498 TLI.getPointerTy());
8499 SDValue BasePtr = St->getBasePtr();
8500
8501 // Perform one or more big stores into memory.
8502 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
8503 for (unsigned I = 0; I < E; I++) {
8504 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
8505 StoreType, ShuffWide,
8506 DAG.getIntPtrConstant(I));
8507 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
8508 St->getPointerInfo(), St->isVolatile(),
8509 St->isNonTemporal(), St->getAlignment());
8510 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
8511 Increment);
8512 Chains.push_back(Ch);
8513 }
Craig Topper48d114b2014-04-26 18:35:24 +00008514 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008515 }
8516
8517 if (!ISD::isNormalStore(St))
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008518 return SDValue();
8519
Chad Rosier99cbde92012-04-09 19:38:15 +00008520 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
8521 // ARM stores of arguments in the same cache line.
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008522 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier99cbde92012-04-09 19:38:15 +00008523 StVal.getNode()->hasOneUse()) {
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008524 SelectionDAG &DAG = DCI.DAG;
Christian Pirkerb5728192014-05-08 14:06:24 +00008525 bool isBigEndian = DAG.getTargetLoweringInfo().isBigEndian();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008526 SDLoc DL(St);
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008527 SDValue BasePtr = St->getBasePtr();
8528 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
Christian Pirkerb5728192014-05-08 14:06:24 +00008529 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
8530 BasePtr, St->getPointerInfo(), St->isVolatile(),
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008531 St->isNonTemporal(), St->getAlignment());
8532
8533 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8534 DAG.getConstant(4, MVT::i32));
Christian Pirkerb5728192014-05-08 14:06:24 +00008535 return DAG.getStore(NewST1.getValue(0), DL,
8536 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008537 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
8538 St->isNonTemporal(),
8539 std::min(4U, St->getAlignment() / 2));
8540 }
8541
8542 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008543 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8544 return SDValue();
8545
Chad Rosier99cbde92012-04-09 19:38:15 +00008546 // Bitcast an i64 store extracted from a vector to f64.
8547 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008548 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008549 SDLoc dl(StVal);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008550 SDValue IntVec = StVal.getOperand(0);
8551 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8552 IntVec.getValueType().getVectorNumElements());
8553 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
8554 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
8555 Vec, StVal.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008556 dl = SDLoc(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008557 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
8558 // Make the DAGCombiner fold the bitcasts.
8559 DCI.AddToWorklist(Vec.getNode());
8560 DCI.AddToWorklist(ExtElt.getNode());
8561 DCI.AddToWorklist(V.getNode());
8562 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
8563 St->getPointerInfo(), St->isVolatile(),
8564 St->isNonTemporal(), St->getAlignment(),
8565 St->getTBAAInfo());
8566}
8567
8568/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8569/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8570/// i64 vector to have f64 elements, since the value can then be loaded
8571/// directly into a VFP register.
8572static bool hasNormalLoadOperand(SDNode *N) {
8573 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8574 for (unsigned i = 0; i < NumElts; ++i) {
8575 SDNode *Elt = N->getOperand(i).getNode();
8576 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8577 return true;
8578 }
8579 return false;
8580}
8581
Bob Wilsoncb6db982010-09-17 22:59:05 +00008582/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8583/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008584static SDValue PerformBUILD_VECTORCombine(SDNode *N,
8585 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilsoncb6db982010-09-17 22:59:05 +00008586 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8587 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8588 // into a pair of GPRs, which is fine when the value is used as a scalar,
8589 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008590 SelectionDAG &DAG = DCI.DAG;
8591 if (N->getNumOperands() == 2) {
8592 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8593 if (RV.getNode())
8594 return RV;
8595 }
Bob Wilsoncb6db982010-09-17 22:59:05 +00008596
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008597 // Load i64 elements as f64 values so that type legalization does not split
8598 // them up into i32 values.
8599 EVT VT = N->getValueType(0);
8600 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8601 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008602 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008603 SmallVector<SDValue, 8> Ops;
8604 unsigned NumElts = VT.getVectorNumElements();
8605 for (unsigned i = 0; i < NumElts; ++i) {
8606 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8607 Ops.push_back(V);
8608 // Make the DAGCombiner fold the bitcast.
8609 DCI.AddToWorklist(V.getNode());
8610 }
8611 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00008612 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008613 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8614}
8615
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008616/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
8617static SDValue
8618PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8619 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
8620 // At that time, we may have inserted bitcasts from integer to float.
8621 // If these bitcasts have survived DAGCombine, change the lowering of this
8622 // BUILD_VECTOR in something more vector friendly, i.e., that does not
8623 // force to use floating point types.
8624
8625 // Make sure we can change the type of the vector.
8626 // This is possible iff:
8627 // 1. The vector is only used in a bitcast to a integer type. I.e.,
8628 // 1.1. Vector is used only once.
8629 // 1.2. Use is a bit convert to an integer type.
8630 // 2. The size of its operands are 32-bits (64-bits are not legal).
8631 EVT VT = N->getValueType(0);
8632 EVT EltVT = VT.getVectorElementType();
8633
8634 // Check 1.1. and 2.
8635 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
8636 return SDValue();
8637
8638 // By construction, the input type must be float.
8639 assert(EltVT == MVT::f32 && "Unexpected type!");
8640
8641 // Check 1.2.
8642 SDNode *Use = *N->use_begin();
8643 if (Use->getOpcode() != ISD::BITCAST ||
8644 Use->getValueType(0).isFloatingPoint())
8645 return SDValue();
8646
8647 // Check profitability.
8648 // Model is, if more than half of the relevant operands are bitcast from
8649 // i32, turn the build_vector into a sequence of insert_vector_elt.
8650 // Relevant operands are everything that is not statically
8651 // (i.e., at compile time) bitcasted.
8652 unsigned NumOfBitCastedElts = 0;
8653 unsigned NumElts = VT.getVectorNumElements();
8654 unsigned NumOfRelevantElts = NumElts;
8655 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
8656 SDValue Elt = N->getOperand(Idx);
8657 if (Elt->getOpcode() == ISD::BITCAST) {
8658 // Assume only bit cast to i32 will go away.
8659 if (Elt->getOperand(0).getValueType() == MVT::i32)
8660 ++NumOfBitCastedElts;
8661 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt))
8662 // Constants are statically casted, thus do not count them as
8663 // relevant operands.
8664 --NumOfRelevantElts;
8665 }
8666
8667 // Check if more than half of the elements require a non-free bitcast.
8668 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
8669 return SDValue();
8670
8671 SelectionDAG &DAG = DCI.DAG;
8672 // Create the new vector type.
8673 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
8674 // Check if the type is legal.
8675 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8676 if (!TLI.isTypeLegal(VecVT))
8677 return SDValue();
8678
8679 // Combine:
8680 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
8681 // => BITCAST INSERT_VECTOR_ELT
8682 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
8683 // (BITCAST EN), N.
8684 SDValue Vec = DAG.getUNDEF(VecVT);
8685 SDLoc dl(N);
8686 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
8687 SDValue V = N->getOperand(Idx);
8688 if (V.getOpcode() == ISD::UNDEF)
8689 continue;
8690 if (V.getOpcode() == ISD::BITCAST &&
8691 V->getOperand(0).getValueType() == MVT::i32)
8692 // Fold obvious case.
8693 V = V.getOperand(0);
8694 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00008695 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008696 // Make the DAGCombiner fold the bitcasts.
8697 DCI.AddToWorklist(V.getNode());
8698 }
8699 SDValue LaneIdx = DAG.getConstant(Idx, MVT::i32);
8700 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
8701 }
8702 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
8703 // Make the DAGCombiner fold the bitcasts.
8704 DCI.AddToWorklist(Vec.getNode());
8705 return Vec;
8706}
8707
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008708/// PerformInsertEltCombine - Target-specific dag combine xforms for
8709/// ISD::INSERT_VECTOR_ELT.
8710static SDValue PerformInsertEltCombine(SDNode *N,
8711 TargetLowering::DAGCombinerInfo &DCI) {
8712 // Bitcast an i64 load inserted into a vector to f64.
8713 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8714 EVT VT = N->getValueType(0);
8715 SDNode *Elt = N->getOperand(1).getNode();
8716 if (VT.getVectorElementType() != MVT::i64 ||
8717 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8718 return SDValue();
8719
8720 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008721 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008722 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8723 VT.getVectorNumElements());
8724 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8725 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8726 // Make the DAGCombiner fold the bitcasts.
8727 DCI.AddToWorklist(Vec.getNode());
8728 DCI.AddToWorklist(V.getNode());
8729 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8730 Vec, V, N->getOperand(2));
8731 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00008732}
8733
Bob Wilsonc7334a12010-10-27 20:38:28 +00008734/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8735/// ISD::VECTOR_SHUFFLE.
8736static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8737 // The LLVM shufflevector instruction does not require the shuffle mask
8738 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8739 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8740 // operands do not match the mask length, they are extended by concatenating
8741 // them with undef vectors. That is probably the right thing for other
8742 // targets, but for NEON it is better to concatenate two double-register
8743 // size vector operands into a single quad-register size vector. Do that
8744 // transformation here:
8745 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8746 // shuffle(concat(v1, v2), undef)
8747 SDValue Op0 = N->getOperand(0);
8748 SDValue Op1 = N->getOperand(1);
8749 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8750 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8751 Op0.getNumOperands() != 2 ||
8752 Op1.getNumOperands() != 2)
8753 return SDValue();
8754 SDValue Concat0Op1 = Op0.getOperand(1);
8755 SDValue Concat1Op1 = Op1.getOperand(1);
8756 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8757 Concat1Op1.getOpcode() != ISD::UNDEF)
8758 return SDValue();
8759 // Skip the transformation if any of the types are illegal.
8760 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8761 EVT VT = N->getValueType(0);
8762 if (!TLI.isTypeLegal(VT) ||
8763 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8764 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8765 return SDValue();
8766
Andrew Trickef9de2a2013-05-25 02:42:55 +00008767 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008768 Op0.getOperand(0), Op1.getOperand(0));
8769 // Translate the shuffle mask.
8770 SmallVector<int, 16> NewMask;
8771 unsigned NumElts = VT.getVectorNumElements();
8772 unsigned HalfElts = NumElts/2;
8773 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8774 for (unsigned n = 0; n < NumElts; ++n) {
8775 int MaskElt = SVN->getMaskElt(n);
8776 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00008777 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00008778 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00008779 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00008780 NewElt = HalfElts + MaskElt - NumElts;
8781 NewMask.push_back(NewElt);
8782 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00008783 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008784 DAG.getUNDEF(VT), NewMask.data());
8785}
8786
Bob Wilson06fce872011-02-07 17:43:21 +00008787/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
8788/// NEON load/store intrinsics to merge base address updates.
8789static SDValue CombineBaseUpdate(SDNode *N,
8790 TargetLowering::DAGCombinerInfo &DCI) {
8791 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8792 return SDValue();
8793
8794 SelectionDAG &DAG = DCI.DAG;
8795 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8796 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
8797 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
8798 SDValue Addr = N->getOperand(AddrOpIdx);
8799
8800 // Search for a use of the address operand that is an increment.
8801 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8802 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8803 SDNode *User = *UI;
8804 if (User->getOpcode() != ISD::ADD ||
8805 UI.getUse().getResNo() != Addr.getResNo())
8806 continue;
8807
8808 // Check that the add is independent of the load/store. Otherwise, folding
8809 // it would create a cycle.
8810 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8811 continue;
8812
8813 // Find the new opcode for the updating load/store.
8814 bool isLoad = true;
8815 bool isLaneOp = false;
8816 unsigned NewOpc = 0;
8817 unsigned NumVecs = 0;
8818 if (isIntrinsic) {
8819 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8820 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00008821 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008822 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8823 NumVecs = 1; break;
8824 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8825 NumVecs = 2; break;
8826 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8827 NumVecs = 3; break;
8828 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8829 NumVecs = 4; break;
8830 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8831 NumVecs = 2; isLaneOp = true; break;
8832 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8833 NumVecs = 3; isLaneOp = true; break;
8834 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8835 NumVecs = 4; isLaneOp = true; break;
8836 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
8837 NumVecs = 1; isLoad = false; break;
8838 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
8839 NumVecs = 2; isLoad = false; break;
8840 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8841 NumVecs = 3; isLoad = false; break;
8842 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8843 NumVecs = 4; isLoad = false; break;
8844 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8845 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8846 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8847 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8848 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8849 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8850 }
8851 } else {
8852 isLaneOp = true;
8853 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00008854 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008855 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8856 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8857 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8858 }
8859 }
8860
8861 // Find the size of memory referenced by the load/store.
8862 EVT VecTy;
8863 if (isLoad)
8864 VecTy = N->getValueType(0);
Owen Anderson77aa2662011-04-05 21:48:57 +00008865 else
Bob Wilson06fce872011-02-07 17:43:21 +00008866 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8867 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8868 if (isLaneOp)
8869 NumBytes /= VecTy.getVectorNumElements();
8870
8871 // If the increment is a constant, it must match the memory ref size.
8872 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8873 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8874 uint64_t IncVal = CInc->getZExtValue();
8875 if (IncVal != NumBytes)
8876 continue;
8877 } else if (NumBytes >= 3 * 16) {
8878 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8879 // separate instructions that make it harder to use a non-constant update.
8880 continue;
8881 }
8882
8883 // Create the new updating load/store node.
8884 EVT Tys[6];
8885 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8886 unsigned n;
8887 for (n = 0; n < NumResultVecs; ++n)
8888 Tys[n] = VecTy;
8889 Tys[n++] = MVT::i32;
8890 Tys[n] = MVT::Other;
Craig Topperabb4ac72014-04-16 06:10:51 +00008891 SDVTList SDTys = DAG.getVTList(ArrayRef<EVT>(Tys, NumResultVecs+2));
Bob Wilson06fce872011-02-07 17:43:21 +00008892 SmallVector<SDValue, 8> Ops;
8893 Ops.push_back(N->getOperand(0)); // incoming chain
8894 Ops.push_back(N->getOperand(AddrOpIdx));
8895 Ops.push_back(Inc);
8896 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8897 Ops.push_back(N->getOperand(i));
8898 }
8899 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008900 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, SDLoc(N), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00008901 Ops, MemInt->getMemoryVT(),
Bob Wilson06fce872011-02-07 17:43:21 +00008902 MemInt->getMemOperand());
8903
8904 // Update the uses.
8905 std::vector<SDValue> NewResults;
8906 for (unsigned i = 0; i < NumResultVecs; ++i) {
8907 NewResults.push_back(SDValue(UpdN.getNode(), i));
8908 }
8909 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8910 DCI.CombineTo(N, NewResults);
8911 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8912
8913 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00008914 }
Bob Wilson06fce872011-02-07 17:43:21 +00008915 return SDValue();
8916}
8917
Bob Wilson2d790df2010-11-28 06:51:26 +00008918/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8919/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8920/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8921/// return true.
8922static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8923 SelectionDAG &DAG = DCI.DAG;
8924 EVT VT = N->getValueType(0);
8925 // vldN-dup instructions only support 64-bit vectors for N > 1.
8926 if (!VT.is64BitVector())
8927 return false;
8928
8929 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8930 SDNode *VLD = N->getOperand(0).getNode();
8931 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8932 return false;
8933 unsigned NumVecs = 0;
8934 unsigned NewOpc = 0;
8935 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8936 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8937 NumVecs = 2;
8938 NewOpc = ARMISD::VLD2DUP;
8939 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8940 NumVecs = 3;
8941 NewOpc = ARMISD::VLD3DUP;
8942 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8943 NumVecs = 4;
8944 NewOpc = ARMISD::VLD4DUP;
8945 } else {
8946 return false;
8947 }
8948
8949 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8950 // numbers match the load.
8951 unsigned VLDLaneNo =
8952 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8953 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8954 UI != UE; ++UI) {
8955 // Ignore uses of the chain result.
8956 if (UI.getUse().getResNo() == NumVecs)
8957 continue;
8958 SDNode *User = *UI;
8959 if (User->getOpcode() != ARMISD::VDUPLANE ||
8960 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8961 return false;
8962 }
8963
8964 // Create the vldN-dup node.
8965 EVT Tys[5];
8966 unsigned n;
8967 for (n = 0; n < NumVecs; ++n)
8968 Tys[n] = VT;
8969 Tys[n] = MVT::Other;
Craig Topperabb4ac72014-04-16 06:10:51 +00008970 SDVTList SDTys = DAG.getVTList(ArrayRef<EVT>(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00008971 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8972 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008973 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00008974 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00008975 VLDMemInt->getMemOperand());
8976
8977 // Update the uses.
8978 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8979 UI != UE; ++UI) {
8980 unsigned ResNo = UI.getUse().getResNo();
8981 // Ignore uses of the chain result.
8982 if (ResNo == NumVecs)
8983 continue;
8984 SDNode *User = *UI;
8985 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8986 }
8987
8988 // Now the vldN-lane intrinsic is dead except for its chain result.
8989 // Update uses of the chain.
8990 std::vector<SDValue> VLDDupResults;
8991 for (unsigned n = 0; n < NumVecs; ++n)
8992 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8993 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8994 DCI.CombineTo(VLD, VLDDupResults);
8995
8996 return true;
8997}
8998
Bob Wilson103a0dc2010-07-14 01:22:12 +00008999/// PerformVDUPLANECombine - Target-specific dag combine xforms for
9000/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00009001static SDValue PerformVDUPLANECombine(SDNode *N,
9002 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00009003 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009004
Bob Wilson2d790df2010-11-28 06:51:26 +00009005 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
9006 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
9007 if (CombineVLDDUP(N, DCI))
9008 return SDValue(N, 0);
9009
9010 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
9011 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00009012 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009013 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00009014 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009015 return SDValue();
9016
9017 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
9018 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
9019 // The canonical VMOV for a zero vector uses a 32-bit element size.
9020 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9021 unsigned EltBits;
9022 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
9023 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00009024 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009025 if (EltSize > VT.getVectorElementType().getSizeInBits())
9026 return SDValue();
9027
Andrew Trickef9de2a2013-05-25 02:42:55 +00009028 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009029}
9030
Eric Christopher1b8b94192011-06-29 21:10:36 +00009031// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosierfa8d8932011-06-24 19:23:04 +00009032// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
9033static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
9034{
Chad Rosier6b610b32011-06-28 17:26:57 +00009035 integerPart cN;
9036 integerPart c0 = 0;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009037 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
9038 I != E; I++) {
9039 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
9040 if (!C)
9041 return false;
9042
Eric Christopher1b8b94192011-06-29 21:10:36 +00009043 bool isExact;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009044 APFloat APF = C->getValueAPF();
9045 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
9046 != APFloat::opOK || !isExact)
9047 return false;
9048
9049 c0 = (I == 0) ? cN : c0;
9050 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
9051 return false;
9052 }
9053 C = c0;
9054 return true;
9055}
9056
9057/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
9058/// can replace combinations of VMUL and VCVT (floating-point to integer)
9059/// when the VMUL has a constant operand that is a power of 2.
9060///
9061/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9062/// vmul.f32 d16, d17, d16
9063/// vcvt.s32.f32 d16, d16
9064/// becomes:
9065/// vcvt.s32.f32 d16, d16, #3
9066static SDValue PerformVCVTCombine(SDNode *N,
9067 TargetLowering::DAGCombinerInfo &DCI,
9068 const ARMSubtarget *Subtarget) {
9069 SelectionDAG &DAG = DCI.DAG;
9070 SDValue Op = N->getOperand(0);
9071
9072 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
9073 Op.getOpcode() != ISD::FMUL)
9074 return SDValue();
9075
9076 uint64_t C;
9077 SDValue N0 = Op->getOperand(0);
9078 SDValue ConstVec = Op->getOperand(1);
9079 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
9080
Eric Christopher1b8b94192011-06-29 21:10:36 +00009081 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosierfa8d8932011-06-24 19:23:04 +00009082 !isConstVecPow2(ConstVec, isSigned, C))
9083 return SDValue();
9084
Tim Northover7cbc2152013-06-28 15:29:25 +00009085 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
9086 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
9087 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9088 // These instructions only exist converting from f32 to i32. We can handle
9089 // smaller integers by generating an extra truncate, but larger ones would
9090 // be lossy.
9091 return SDValue();
9092 }
9093
Chad Rosierfa8d8932011-06-24 19:23:04 +00009094 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
9095 Intrinsic::arm_neon_vcvtfp2fxu;
Tim Northover7cbc2152013-06-28 15:29:25 +00009096 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9097 SDValue FixConv = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
9098 NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9099 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
9100 DAG.getConstant(Log2_64(C), MVT::i32));
9101
9102 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9103 FixConv = DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), FixConv);
9104
9105 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009106}
9107
9108/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
9109/// can replace combinations of VCVT (integer to floating-point) and VDIV
9110/// when the VDIV has a constant operand that is a power of 2.
9111///
9112/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9113/// vcvt.f32.s32 d16, d16
9114/// vdiv.f32 d16, d17, d16
9115/// becomes:
9116/// vcvt.f32.s32 d16, d16, #3
9117static SDValue PerformVDIVCombine(SDNode *N,
9118 TargetLowering::DAGCombinerInfo &DCI,
9119 const ARMSubtarget *Subtarget) {
9120 SelectionDAG &DAG = DCI.DAG;
9121 SDValue Op = N->getOperand(0);
9122 unsigned OpOpcode = Op.getNode()->getOpcode();
9123
9124 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
9125 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
9126 return SDValue();
9127
9128 uint64_t C;
9129 SDValue ConstVec = N->getOperand(1);
9130 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
9131
9132 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
9133 !isConstVecPow2(ConstVec, isSigned, C))
9134 return SDValue();
9135
Tim Northover7cbc2152013-06-28 15:29:25 +00009136 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
9137 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
9138 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9139 // These instructions only exist converting from i32 to f32. We can handle
9140 // smaller integers by generating an extra extend, but larger ones would
9141 // be lossy.
9142 return SDValue();
9143 }
9144
9145 SDValue ConvInput = Op.getOperand(0);
9146 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9147 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9148 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
9149 SDLoc(N), NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9150 ConvInput);
9151
Eric Christopher1b8b94192011-06-29 21:10:36 +00009152 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +00009153 Intrinsic::arm_neon_vcvtfxu2fp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009154 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
Chad Rosierfa8d8932011-06-24 19:23:04 +00009155 Op.getValueType(),
Eric Christopher1b8b94192011-06-29 21:10:36 +00009156 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Tim Northover7cbc2152013-06-28 15:29:25 +00009157 ConvInput, DAG.getConstant(Log2_64(C), MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +00009158}
9159
9160/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +00009161/// operand of a vector shift operation, where all the elements of the
9162/// build_vector must have the same constant integer value.
9163static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
9164 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +00009165 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00009166 Op = Op.getOperand(0);
9167 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
9168 APInt SplatBits, SplatUndef;
9169 unsigned SplatBitSize;
9170 bool HasAnyUndefs;
9171 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
9172 HasAnyUndefs, ElementBits) ||
9173 SplatBitSize > ElementBits)
9174 return false;
9175 Cnt = SplatBits.getSExtValue();
9176 return true;
9177}
9178
9179/// isVShiftLImm - Check if this is a valid build_vector for the immediate
9180/// operand of a vector shift left operation. That value must be in the range:
9181/// 0 <= Value < ElementBits for a left shift; or
9182/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009183static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009184 assert(VT.isVector() && "vector shift count is not a vector type");
9185 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9186 if (! getVShiftImm(Op, ElementBits, Cnt))
9187 return false;
9188 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
9189}
9190
9191/// isVShiftRImm - Check if this is a valid build_vector for the immediate
9192/// operand of a vector shift right operation. For a shift opcode, the value
9193/// is positive, but for an intrinsic the value count must be negative. The
9194/// absolute value must be in the range:
9195/// 1 <= |Value| <= ElementBits for a right shift; or
9196/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009197static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +00009198 int64_t &Cnt) {
9199 assert(VT.isVector() && "vector shift count is not a vector type");
9200 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9201 if (! getVShiftImm(Op, ElementBits, Cnt))
9202 return false;
9203 if (isIntrinsic)
9204 Cnt = -Cnt;
9205 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
9206}
9207
9208/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
9209static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
9210 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
9211 switch (IntNo) {
9212 default:
9213 // Don't do anything for most intrinsics.
9214 break;
9215
9216 // Vector shifts: check for immediate versions and lower them.
9217 // Note: This is done during DAG combining instead of DAG legalizing because
9218 // the build_vectors for 64-bit vector element shift counts are generally
9219 // not legal, and it is hard to see their values after they get legalized to
9220 // loads from a constant pool.
9221 case Intrinsic::arm_neon_vshifts:
9222 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +00009223 case Intrinsic::arm_neon_vrshifts:
9224 case Intrinsic::arm_neon_vrshiftu:
9225 case Intrinsic::arm_neon_vrshiftn:
9226 case Intrinsic::arm_neon_vqshifts:
9227 case Intrinsic::arm_neon_vqshiftu:
9228 case Intrinsic::arm_neon_vqshiftsu:
9229 case Intrinsic::arm_neon_vqshiftns:
9230 case Intrinsic::arm_neon_vqshiftnu:
9231 case Intrinsic::arm_neon_vqshiftnsu:
9232 case Intrinsic::arm_neon_vqrshiftns:
9233 case Intrinsic::arm_neon_vqrshiftnu:
9234 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009235 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009236 int64_t Cnt;
9237 unsigned VShiftOpc = 0;
9238
9239 switch (IntNo) {
9240 case Intrinsic::arm_neon_vshifts:
9241 case Intrinsic::arm_neon_vshiftu:
9242 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
9243 VShiftOpc = ARMISD::VSHL;
9244 break;
9245 }
9246 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
9247 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
9248 ARMISD::VSHRs : ARMISD::VSHRu);
9249 break;
9250 }
9251 return SDValue();
9252
Bob Wilson2e076c42009-06-22 23:27:02 +00009253 case Intrinsic::arm_neon_vrshifts:
9254 case Intrinsic::arm_neon_vrshiftu:
9255 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9256 break;
9257 return SDValue();
9258
9259 case Intrinsic::arm_neon_vqshifts:
9260 case Intrinsic::arm_neon_vqshiftu:
9261 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9262 break;
9263 return SDValue();
9264
9265 case Intrinsic::arm_neon_vqshiftsu:
9266 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9267 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +00009268 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009269
Bob Wilson2e076c42009-06-22 23:27:02 +00009270 case Intrinsic::arm_neon_vrshiftn:
9271 case Intrinsic::arm_neon_vqshiftns:
9272 case Intrinsic::arm_neon_vqshiftnu:
9273 case Intrinsic::arm_neon_vqshiftnsu:
9274 case Intrinsic::arm_neon_vqrshiftns:
9275 case Intrinsic::arm_neon_vqrshiftnu:
9276 case Intrinsic::arm_neon_vqrshiftnsu:
9277 // Narrowing shifts require an immediate right shift.
9278 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9279 break;
Jim Grosbach84511e12010-06-02 21:53:11 +00009280 llvm_unreachable("invalid shift count for narrowing vector shift "
9281 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009282
9283 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00009284 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00009285 }
9286
9287 switch (IntNo) {
9288 case Intrinsic::arm_neon_vshifts:
9289 case Intrinsic::arm_neon_vshiftu:
9290 // Opcode already set above.
9291 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00009292 case Intrinsic::arm_neon_vrshifts:
9293 VShiftOpc = ARMISD::VRSHRs; break;
9294 case Intrinsic::arm_neon_vrshiftu:
9295 VShiftOpc = ARMISD::VRSHRu; break;
9296 case Intrinsic::arm_neon_vrshiftn:
9297 VShiftOpc = ARMISD::VRSHRN; break;
9298 case Intrinsic::arm_neon_vqshifts:
9299 VShiftOpc = ARMISD::VQSHLs; break;
9300 case Intrinsic::arm_neon_vqshiftu:
9301 VShiftOpc = ARMISD::VQSHLu; break;
9302 case Intrinsic::arm_neon_vqshiftsu:
9303 VShiftOpc = ARMISD::VQSHLsu; break;
9304 case Intrinsic::arm_neon_vqshiftns:
9305 VShiftOpc = ARMISD::VQSHRNs; break;
9306 case Intrinsic::arm_neon_vqshiftnu:
9307 VShiftOpc = ARMISD::VQSHRNu; break;
9308 case Intrinsic::arm_neon_vqshiftnsu:
9309 VShiftOpc = ARMISD::VQSHRNsu; break;
9310 case Intrinsic::arm_neon_vqrshiftns:
9311 VShiftOpc = ARMISD::VQRSHRNs; break;
9312 case Intrinsic::arm_neon_vqrshiftnu:
9313 VShiftOpc = ARMISD::VQRSHRNu; break;
9314 case Intrinsic::arm_neon_vqrshiftnsu:
9315 VShiftOpc = ARMISD::VQRSHRNsu; break;
9316 }
9317
Andrew Trickef9de2a2013-05-25 02:42:55 +00009318 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009319 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009320 }
9321
9322 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009323 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009324 int64_t Cnt;
9325 unsigned VShiftOpc = 0;
9326
9327 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9328 VShiftOpc = ARMISD::VSLI;
9329 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9330 VShiftOpc = ARMISD::VSRI;
9331 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009332 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009333 }
9334
Andrew Trickef9de2a2013-05-25 02:42:55 +00009335 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +00009336 N->getOperand(1), N->getOperand(2),
Owen Anderson9f944592009-08-11 20:47:22 +00009337 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009338 }
9339
9340 case Intrinsic::arm_neon_vqrshifts:
9341 case Intrinsic::arm_neon_vqrshiftu:
9342 // No immediate versions of these to check for.
9343 break;
9344 }
9345
9346 return SDValue();
9347}
9348
9349/// PerformShiftCombine - Checks for immediate versions of vector shifts and
9350/// lowers them. As with the vector shift intrinsics, this is done during DAG
9351/// combining instead of DAG legalizing because the build_vectors for 64-bit
9352/// vector element shift counts are generally not legal, and it is hard to see
9353/// their values after they get legalized to loads from a constant pool.
9354static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9355 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009356 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +00009357 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9358 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9359 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9360 SDValue N1 = N->getOperand(1);
9361 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9362 SDValue N0 = N->getOperand(0);
9363 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9364 DAG.MaskedValueIsZero(N0.getOperand(0),
9365 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009366 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +00009367 }
9368 }
Bob Wilson2e076c42009-06-22 23:27:02 +00009369
9370 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +00009371 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9372 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +00009373 return SDValue();
9374
9375 assert(ST->hasNEON() && "unexpected vector shift");
9376 int64_t Cnt;
9377
9378 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009379 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009380
9381 case ISD::SHL:
9382 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009383 return DAG.getNode(ARMISD::VSHL, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009384 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009385 break;
9386
9387 case ISD::SRA:
9388 case ISD::SRL:
9389 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
9390 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
9391 ARMISD::VSHRs : ARMISD::VSHRu);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009392 return DAG.getNode(VShiftOpc, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009393 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009394 }
9395 }
9396 return SDValue();
9397}
9398
9399/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
9400/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
9401static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
9402 const ARMSubtarget *ST) {
9403 SDValue N0 = N->getOperand(0);
9404
9405 // Check for sign- and zero-extensions of vector extract operations of 8-
9406 // and 16-bit vector elements. NEON supports these directly. They are
9407 // handled during DAG combining because type legalization will promote them
9408 // to 32-bit types and it is messy to recognize the operations after that.
9409 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9410 SDValue Vec = N0.getOperand(0);
9411 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00009412 EVT VT = N->getValueType(0);
9413 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009414 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9415
Owen Anderson9f944592009-08-11 20:47:22 +00009416 if (VT == MVT::i32 &&
9417 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +00009418 TLI.isTypeLegal(Vec.getValueType()) &&
9419 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009420
9421 unsigned Opc = 0;
9422 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009423 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009424 case ISD::SIGN_EXTEND:
9425 Opc = ARMISD::VGETLANEs;
9426 break;
9427 case ISD::ZERO_EXTEND:
9428 case ISD::ANY_EXTEND:
9429 Opc = ARMISD::VGETLANEu;
9430 break;
9431 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009432 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +00009433 }
9434 }
9435
9436 return SDValue();
9437}
9438
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009439/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9440/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9441static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9442 const ARMSubtarget *ST) {
9443 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng55f0c6b2010-07-15 22:07:12 +00009444 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009445 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9446 // a NaN; only do the transformation when it matches that behavior.
9447
9448 // For now only do this when using NEON for FP operations; if using VFP, it
9449 // is not obvious that the benefit outweighs the cost of switching to the
9450 // NEON pipeline.
9451 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9452 N->getValueType(0) != MVT::f32)
9453 return SDValue();
9454
9455 SDValue CondLHS = N->getOperand(0);
9456 SDValue CondRHS = N->getOperand(1);
9457 SDValue LHS = N->getOperand(2);
9458 SDValue RHS = N->getOperand(3);
9459 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9460
9461 unsigned Opcode = 0;
9462 bool IsReversed;
Bob Wilsonba8ac742010-02-24 22:15:53 +00009463 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009464 IsReversed = false; // x CC y ? x : y
Bob Wilsonba8ac742010-02-24 22:15:53 +00009465 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009466 IsReversed = true ; // x CC y ? y : x
9467 } else {
9468 return SDValue();
9469 }
9470
Bob Wilsonba8ac742010-02-24 22:15:53 +00009471 bool IsUnordered;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009472 switch (CC) {
9473 default: break;
9474 case ISD::SETOLT:
9475 case ISD::SETOLE:
9476 case ISD::SETLT:
9477 case ISD::SETLE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009478 case ISD::SETULT:
9479 case ISD::SETULE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009480 // If LHS is NaN, an ordered comparison will be false and the result will
9481 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
9482 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9483 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
9484 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9485 break;
9486 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
9487 // will return -0, so vmin can only be used for unsafe math or if one of
9488 // the operands is known to be nonzero.
9489 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009490 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009491 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9492 break;
9493 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009494 break;
9495
9496 case ISD::SETOGT:
9497 case ISD::SETOGE:
9498 case ISD::SETGT:
9499 case ISD::SETGE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009500 case ISD::SETUGT:
9501 case ISD::SETUGE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009502 // If LHS is NaN, an ordered comparison will be false and the result will
9503 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
9504 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9505 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
9506 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9507 break;
9508 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
9509 // will return +0, so vmax can only be used for unsafe math or if one of
9510 // the operands is known to be nonzero.
9511 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009512 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009513 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9514 break;
9515 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009516 break;
9517 }
9518
9519 if (!Opcode)
9520 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009521 return DAG.getNode(Opcode, SDLoc(N), N->getValueType(0), LHS, RHS);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009522}
9523
Evan Chengf863e3f2011-07-13 00:42:17 +00009524/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
9525SDValue
9526ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
9527 SDValue Cmp = N->getOperand(4);
9528 if (Cmp.getOpcode() != ARMISD::CMPZ)
9529 // Only looking at EQ and NE cases.
9530 return SDValue();
9531
9532 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009533 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +00009534 SDValue LHS = Cmp.getOperand(0);
9535 SDValue RHS = Cmp.getOperand(1);
9536 SDValue FalseVal = N->getOperand(0);
9537 SDValue TrueVal = N->getOperand(1);
9538 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +00009539 ARMCC::CondCodes CC =
9540 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +00009541
9542 // Simplify
9543 // mov r1, r0
9544 // cmp r1, x
9545 // mov r0, y
9546 // moveq r0, x
9547 // to
9548 // cmp r0, x
9549 // movne r0, y
9550 //
9551 // mov r1, r0
9552 // cmp r1, x
9553 // mov r0, x
9554 // movne r0, y
9555 // to
9556 // cmp r0, x
9557 // movne r0, y
9558 /// FIXME: Turn this into a target neutral optimization?
9559 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +00009560 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +00009561 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
9562 N->getOperand(3), Cmp);
9563 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
9564 SDValue ARMcc;
9565 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
9566 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
9567 N->getOperand(3), NewCmp);
9568 }
9569
9570 if (Res.getNode()) {
9571 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00009572 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +00009573 // Capture demanded bits information that would be otherwise lost.
9574 if (KnownZero == 0xfffffffe)
9575 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9576 DAG.getValueType(MVT::i1));
9577 else if (KnownZero == 0xffffff00)
9578 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9579 DAG.getValueType(MVT::i8));
9580 else if (KnownZero == 0xffff0000)
9581 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9582 DAG.getValueType(MVT::i16));
9583 }
9584
9585 return Res;
9586}
9587
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009588SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +00009589 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009590 switch (N->getOpcode()) {
9591 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009592 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +00009593 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009594 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009595 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +00009596 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +00009597 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
9598 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +00009599 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00009600 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson22806742010-09-22 22:09:21 +00009601 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009602 case ISD::STORE: return PerformSTORECombine(N, DCI);
9603 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
9604 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +00009605 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +00009606 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +00009607 case ISD::FP_TO_SINT:
9608 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
9609 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009610 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00009611 case ISD::SHL:
9612 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009613 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00009614 case ISD::SIGN_EXTEND:
9615 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009616 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
9617 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +00009618 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson06fce872011-02-07 17:43:21 +00009619 case ARMISD::VLD2DUP:
9620 case ARMISD::VLD3DUP:
9621 case ARMISD::VLD4DUP:
9622 return CombineBaseUpdate(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009623 case ARMISD::BUILD_VECTOR:
9624 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +00009625 case ISD::INTRINSIC_VOID:
9626 case ISD::INTRINSIC_W_CHAIN:
9627 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9628 case Intrinsic::arm_neon_vld1:
9629 case Intrinsic::arm_neon_vld2:
9630 case Intrinsic::arm_neon_vld3:
9631 case Intrinsic::arm_neon_vld4:
9632 case Intrinsic::arm_neon_vld2lane:
9633 case Intrinsic::arm_neon_vld3lane:
9634 case Intrinsic::arm_neon_vld4lane:
9635 case Intrinsic::arm_neon_vst1:
9636 case Intrinsic::arm_neon_vst2:
9637 case Intrinsic::arm_neon_vst3:
9638 case Intrinsic::arm_neon_vst4:
9639 case Intrinsic::arm_neon_vst2lane:
9640 case Intrinsic::arm_neon_vst3lane:
9641 case Intrinsic::arm_neon_vst4lane:
9642 return CombineBaseUpdate(N, DCI);
9643 default: break;
9644 }
9645 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009646 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009647 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009648}
9649
Evan Chengd42641c2011-02-02 01:06:55 +00009650bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9651 EVT VT) const {
9652 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9653}
9654
Matt Arsenault25793a32014-02-05 23:15:53 +00009655bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT, unsigned,
9656 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009657 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +00009658 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009659
9660 switch (VT.getSimpleVT().SimpleTy) {
9661 default:
9662 return false;
9663 case MVT::i8:
9664 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009665 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009666 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +00009667 if (AllowsUnaligned) {
9668 if (Fast)
9669 *Fast = Subtarget->hasV7Ops();
9670 return true;
9671 }
9672 return false;
9673 }
Evan Chengeec6bc62012-08-15 17:44:53 +00009674 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009675 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009676 // For any little-endian targets with neon, we can support unaligned ld/st
9677 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +00009678 // A big-endian target may also explicitly support unaligned accesses
Evan Cheng79e2ca92012-12-10 23:21:26 +00009679 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) {
9680 if (Fast)
9681 *Fast = true;
9682 return true;
9683 }
9684 return false;
9685 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009686 }
9687}
9688
Lang Hames9929c422011-11-02 22:52:45 +00009689static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
9690 unsigned AlignCheck) {
9691 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
9692 (DstAlign == 0 || DstAlign % AlignCheck == 0));
9693}
9694
9695EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
9696 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00009697 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +00009698 bool MemcpyStrSrc,
9699 MachineFunction &MF) const {
9700 const Function *F = MF.getFunction();
9701
9702 // See if we can use NEON instructions for this...
Evan Cheng962711e2012-12-12 02:34:41 +00009703 if ((!IsMemset || ZeroMemset) &&
Evan Cheng79e2ca92012-12-10 23:21:26 +00009704 Subtarget->hasNEON() &&
Bill Wendling698e84f2012-12-30 10:32:01 +00009705 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
9706 Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009707 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +00009708 if (Size >= 16 &&
9709 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault25793a32014-02-05 23:15:53 +00009710 (allowsUnalignedMemoryAccesses(MVT::v2f64, 0, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009711 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +00009712 } else if (Size >= 8 &&
9713 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault25793a32014-02-05 23:15:53 +00009714 (allowsUnalignedMemoryAccesses(MVT::f64, 0, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009715 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +00009716 }
9717 }
9718
Lang Hamesb85fcd02011-11-08 18:56:23 +00009719 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +00009720 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +00009721 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +00009722 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +00009723 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +00009724
Lang Hames9929c422011-11-02 22:52:45 +00009725 // Let the target-independent logic figure it out.
9726 return MVT::Other;
9727}
9728
Evan Cheng9ec512d2012-12-06 19:13:27 +00009729bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
9730 if (Val.getOpcode() != ISD::LOAD)
9731 return false;
9732
9733 EVT VT1 = Val.getValueType();
9734 if (!VT1.isSimple() || !VT1.isInteger() ||
9735 !VT2.isSimple() || !VT2.isInteger())
9736 return false;
9737
9738 switch (VT1.getSimpleVT().SimpleTy) {
9739 default: break;
9740 case MVT::i1:
9741 case MVT::i8:
9742 case MVT::i16:
9743 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
9744 return true;
9745 }
9746
9747 return false;
9748}
9749
Tim Northovercc2e9032013-08-06 13:58:03 +00009750bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
9751 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
9752 return false;
9753
9754 if (!isTypeLegal(EVT::getEVT(Ty1)))
9755 return false;
9756
9757 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
9758
9759 // Assuming the caller doesn't have a zeroext or signext return parameter,
9760 // truncation all the way down to i1 is valid.
9761 return true;
9762}
9763
9764
Evan Chengdc49a8d2009-08-14 20:09:37 +00009765static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
9766 if (V < 0)
9767 return false;
9768
9769 unsigned Scale = 1;
9770 switch (VT.getSimpleVT().SimpleTy) {
9771 default: return false;
9772 case MVT::i1:
9773 case MVT::i8:
9774 // Scale == 1;
9775 break;
9776 case MVT::i16:
9777 // Scale == 2;
9778 Scale = 2;
9779 break;
9780 case MVT::i32:
9781 // Scale == 4;
9782 Scale = 4;
9783 break;
9784 }
9785
9786 if ((V & (Scale - 1)) != 0)
9787 return false;
9788 V /= Scale;
9789 return V == (V & ((1LL << 5) - 1));
9790}
9791
9792static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
9793 const ARMSubtarget *Subtarget) {
9794 bool isNeg = false;
9795 if (V < 0) {
9796 isNeg = true;
9797 V = - V;
9798 }
9799
9800 switch (VT.getSimpleVT().SimpleTy) {
9801 default: return false;
9802 case MVT::i1:
9803 case MVT::i8:
9804 case MVT::i16:
9805 case MVT::i32:
9806 // + imm12 or - imm8
9807 if (isNeg)
9808 return V == (V & ((1LL << 8) - 1));
9809 return V == (V & ((1LL << 12) - 1));
9810 case MVT::f32:
9811 case MVT::f64:
9812 // Same as ARM mode. FIXME: NEON?
9813 if (!Subtarget->hasVFP2())
9814 return false;
9815 if ((V & 3) != 0)
9816 return false;
9817 V >>= 2;
9818 return V == (V & ((1LL << 8) - 1));
9819 }
9820}
9821
Evan Cheng2150b922007-03-12 23:30:29 +00009822/// isLegalAddressImmediate - Return true if the integer value can be used
9823/// as the offset of the target addressing mode for load / store of the
9824/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009825static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009826 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +00009827 if (V == 0)
9828 return true;
9829
Evan Chengce5dfb62009-03-09 19:15:00 +00009830 if (!VT.isSimple())
9831 return false;
9832
Evan Chengdc49a8d2009-08-14 20:09:37 +00009833 if (Subtarget->isThumb1Only())
9834 return isLegalT1AddressImmediate(V, VT);
9835 else if (Subtarget->isThumb2())
9836 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +00009837
Evan Chengdc49a8d2009-08-14 20:09:37 +00009838 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +00009839 if (V < 0)
9840 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +00009841 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +00009842 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +00009843 case MVT::i1:
9844 case MVT::i8:
9845 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +00009846 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009847 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +00009848 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +00009849 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009850 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +00009851 case MVT::f32:
9852 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009853 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +00009854 return false;
Evan Chengbef131de2007-05-03 02:00:18 +00009855 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +00009856 return false;
9857 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009858 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +00009859 }
Evan Cheng10043e22007-01-19 07:51:42 +00009860}
9861
Evan Chengdc49a8d2009-08-14 20:09:37 +00009862bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
9863 EVT VT) const {
9864 int Scale = AM.Scale;
9865 if (Scale < 0)
9866 return false;
9867
9868 switch (VT.getSimpleVT().SimpleTy) {
9869 default: return false;
9870 case MVT::i1:
9871 case MVT::i8:
9872 case MVT::i16:
9873 case MVT::i32:
9874 if (Scale == 1)
9875 return true;
9876 // r + r << imm
9877 Scale = Scale & ~1;
9878 return Scale == 2 || Scale == 4 || Scale == 8;
9879 case MVT::i64:
9880 // r + r
9881 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
9882 return true;
9883 return false;
9884 case MVT::isVoid:
9885 // Note, we allow "void" uses (basically, uses that aren't loads or
9886 // stores), because arm allows folding a scale into many arithmetic
9887 // operations. This should be made more precise and revisited later.
9888
9889 // Allow r << imm, but the imm has to be a multiple of two.
9890 if (Scale & 1) return false;
9891 return isPowerOf2_32(Scale);
9892 }
9893}
9894
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009895/// isLegalAddressingMode - Return true if the addressing mode represented
9896/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson7117a912009-03-20 22:42:55 +00009897bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +00009898 Type *Ty) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009899 EVT VT = getValueType(Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +00009900 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +00009901 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009902
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009903 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +00009904 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009905 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009906
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009907 switch (AM.Scale) {
9908 case 0: // no scale reg, must be "r+i" or "r", or "i".
9909 break;
9910 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009911 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009912 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +00009913 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009914 default:
Chris Lattner502c3f42007-04-13 06:50:55 +00009915 // ARM doesn't support any R+R*scale+imm addr modes.
9916 if (AM.BaseOffs)
9917 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009918
Bob Wilson866c1742009-04-08 17:55:28 +00009919 if (!VT.isSimple())
9920 return false;
9921
Evan Chengdc49a8d2009-08-14 20:09:37 +00009922 if (Subtarget->isThumb2())
9923 return isLegalT2ScaledAddressingMode(AM, VT);
9924
Chris Lattner9b6d69e2007-04-10 03:48:29 +00009925 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +00009926 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009927 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +00009928 case MVT::i1:
9929 case MVT::i8:
9930 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +00009931 if (Scale < 0) Scale = -Scale;
9932 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009933 return true;
9934 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +00009935 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +00009936 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009937 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009938 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +00009939 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009940 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +00009941 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009942
Owen Anderson9f944592009-08-11 20:47:22 +00009943 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009944 // Note, we allow "void" uses (basically, uses that aren't loads or
9945 // stores), because arm allows folding a scale into many arithmetic
9946 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +00009947
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009948 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +00009949 if (Scale & 1) return false;
9950 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009951 }
Evan Cheng2150b922007-03-12 23:30:29 +00009952 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009953 return true;
Evan Cheng2150b922007-03-12 23:30:29 +00009954}
9955
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009956/// isLegalICmpImmediate - Return true if the specified immediate is legal
9957/// icmp immediate, that is the target has icmp instructions which can compare
9958/// a register against the immediate without having to materialize the
9959/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +00009960bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +00009961 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009962 if (!Subtarget->isThumb())
Chandler Carruth8a102c22012-04-06 20:10:52 +00009963 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009964 if (Subtarget->isThumb2())
Chandler Carruth8a102c22012-04-06 20:10:52 +00009965 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +00009966 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +00009967 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009968}
9969
Andrew Tricka22cdb72012-07-18 18:34:27 +00009970/// isLegalAddImmediate - Return true if the specified immediate is a legal add
9971/// *or sub* immediate, that is the target has add or sub instructions which can
9972/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +00009973/// immediate into a register.
9974bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +00009975 // Same encoding for add/sub, just flip the sign.
9976 int64_t AbsImm = llvm::abs64(Imm);
9977 if (!Subtarget->isThumb())
9978 return ARM_AM::getSOImmVal(AbsImm) != -1;
9979 if (Subtarget->isThumb2())
9980 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
9981 // Thumb1 only has 8-bit unsigned immediate.
9982 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +00009983}
9984
Owen Anderson53aa7a92009-08-10 22:56:29 +00009985static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +00009986 bool isSEXTLoad, SDValue &Base,
9987 SDValue &Offset, bool &isInc,
9988 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +00009989 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9990 return false;
9991
Owen Anderson9f944592009-08-11 20:47:22 +00009992 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +00009993 // AddressingMode 3
9994 Base = Ptr->getOperand(0);
9995 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00009996 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +00009997 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +00009998 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +00009999 isInc = false;
10000 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10001 return true;
10002 }
10003 }
10004 isInc = (Ptr->getOpcode() == ISD::ADD);
10005 Offset = Ptr->getOperand(1);
10006 return true;
Owen Anderson9f944592009-08-11 20:47:22 +000010007 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +000010008 // AddressingMode 2
10009 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010010 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010011 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010012 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010013 isInc = false;
10014 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10015 Base = Ptr->getOperand(0);
10016 return true;
10017 }
10018 }
10019
10020 if (Ptr->getOpcode() == ISD::ADD) {
10021 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +000010022 ARM_AM::ShiftOpc ShOpcVal=
10023 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +000010024 if (ShOpcVal != ARM_AM::no_shift) {
10025 Base = Ptr->getOperand(1);
10026 Offset = Ptr->getOperand(0);
10027 } else {
10028 Base = Ptr->getOperand(0);
10029 Offset = Ptr->getOperand(1);
10030 }
10031 return true;
10032 }
10033
10034 isInc = (Ptr->getOpcode() == ISD::ADD);
10035 Base = Ptr->getOperand(0);
10036 Offset = Ptr->getOperand(1);
10037 return true;
10038 }
10039
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000010040 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000010041 return false;
10042}
10043
Owen Anderson53aa7a92009-08-10 22:56:29 +000010044static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010045 bool isSEXTLoad, SDValue &Base,
10046 SDValue &Offset, bool &isInc,
10047 SelectionDAG &DAG) {
10048 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10049 return false;
10050
10051 Base = Ptr->getOperand(0);
10052 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
10053 int RHSC = (int)RHS->getZExtValue();
10054 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
10055 assert(Ptr->getOpcode() == ISD::ADD);
10056 isInc = false;
10057 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10058 return true;
10059 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
10060 isInc = Ptr->getOpcode() == ISD::ADD;
10061 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
10062 return true;
10063 }
10064 }
10065
10066 return false;
10067}
10068
Evan Cheng10043e22007-01-19 07:51:42 +000010069/// getPreIndexedAddressParts - returns true by value, base pointer and
10070/// offset pointer and addressing mode by reference if the node's address
10071/// can be legally represented as pre-indexed load / store address.
10072bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010073ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
10074 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010075 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010076 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010077 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010078 return false;
10079
Owen Anderson53aa7a92009-08-10 22:56:29 +000010080 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010081 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010082 bool isSEXTLoad = false;
10083 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
10084 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010085 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010086 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10087 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
10088 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010089 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010090 } else
10091 return false;
10092
10093 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010094 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010095 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010096 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
10097 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010098 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010099 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000010100 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000010101 if (!isLegal)
10102 return false;
10103
10104 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
10105 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010106}
10107
10108/// getPostIndexedAddressParts - returns true by value, base pointer and
10109/// offset pointer and addressing mode by reference if this node can be
10110/// combined with a load / store to form a post-indexed load / store.
10111bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010112 SDValue &Base,
10113 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010114 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010115 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010116 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010117 return false;
10118
Owen Anderson53aa7a92009-08-10 22:56:29 +000010119 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010120 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010121 bool isSEXTLoad = false;
10122 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010123 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010124 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010125 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10126 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010127 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010128 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010129 } else
10130 return false;
10131
10132 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010133 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010134 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010135 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000010136 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010137 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010138 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
10139 isInc, DAG);
10140 if (!isLegal)
10141 return false;
10142
Evan Chengf19384d2010-05-18 21:31:17 +000010143 if (Ptr != Base) {
10144 // Swap base ptr and offset to catch more post-index load / store when
10145 // it's legal. In Thumb2 mode, offset must be an immediate.
10146 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
10147 !Subtarget->isThumb2())
10148 std::swap(Base, Offset);
10149
10150 // Post-indexed load / store update the base pointer.
10151 if (Ptr != Base)
10152 return false;
10153 }
10154
Evan Cheng84c6cda2009-07-02 07:28:31 +000010155 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
10156 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010157}
10158
Jay Foada0653a32014-05-14 21:14:37 +000010159void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
10160 APInt &KnownZero,
10161 APInt &KnownOne,
10162 const SelectionDAG &DAG,
10163 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000010164 unsigned BitWidth = KnownOne.getBitWidth();
10165 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000010166 switch (Op.getOpcode()) {
10167 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000010168 case ARMISD::ADDC:
10169 case ARMISD::ADDE:
10170 case ARMISD::SUBC:
10171 case ARMISD::SUBE:
10172 // These nodes' second result is a boolean
10173 if (Op.getResNo() == 0)
10174 break;
10175 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
10176 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010177 case ARMISD::CMOV: {
10178 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000010179 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010180 if (KnownZero == 0 && KnownOne == 0) return;
10181
Dan Gohmanf990faf2008-02-13 00:35:47 +000010182 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000010183 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010184 KnownZero &= KnownZeroRHS;
10185 KnownOne &= KnownOneRHS;
10186 return;
10187 }
Tim Northover01b4aa92014-04-03 15:10:35 +000010188 case ISD::INTRINSIC_W_CHAIN: {
10189 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
10190 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
10191 switch (IntID) {
10192 default: return;
10193 case Intrinsic::arm_ldaex:
10194 case Intrinsic::arm_ldrex: {
10195 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
10196 unsigned MemBits = VT.getScalarType().getSizeInBits();
10197 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
10198 return;
10199 }
10200 }
10201 }
Evan Cheng10043e22007-01-19 07:51:42 +000010202 }
10203}
10204
10205//===----------------------------------------------------------------------===//
10206// ARM Inline Assembly Support
10207//===----------------------------------------------------------------------===//
10208
Evan Cheng078b0b02011-01-08 01:24:27 +000010209bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
10210 // Looking for "rev" which is V6+.
10211 if (!Subtarget->hasV6Ops())
10212 return false;
10213
10214 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10215 std::string AsmStr = IA->getAsmString();
10216 SmallVector<StringRef, 4> AsmPieces;
10217 SplitString(AsmStr, AsmPieces, ";\n");
10218
10219 switch (AsmPieces.size()) {
10220 default: return false;
10221 case 1:
10222 AsmStr = AsmPieces[0];
10223 AsmPieces.clear();
10224 SplitString(AsmStr, AsmPieces, " \t,");
10225
10226 // rev $0, $1
10227 if (AsmPieces.size() == 3 &&
10228 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
10229 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000010230 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000010231 if (Ty && Ty->getBitWidth() == 32)
10232 return IntrinsicLowering::LowerToByteSwap(CI);
10233 }
10234 break;
10235 }
10236
10237 return false;
10238}
10239
Evan Cheng10043e22007-01-19 07:51:42 +000010240/// getConstraintType - Given a constraint letter, return the type of
10241/// constraint it is for this target.
10242ARMTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +000010243ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
10244 if (Constraint.size() == 1) {
10245 switch (Constraint[0]) {
10246 default: break;
10247 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000010248 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000010249 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000010250 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000010251 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000010252 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000010253 // An address with a single base register. Due to the way we
10254 // currently handle addresses it is the same as an 'r' memory constraint.
10255 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000010256 }
Eric Christophere256cd02011-06-21 22:10:57 +000010257 } else if (Constraint.size() == 2) {
10258 switch (Constraint[0]) {
10259 default: break;
10260 // All 'U+' constraints are addresses.
10261 case 'U': return C_Memory;
10262 }
Evan Cheng10043e22007-01-19 07:51:42 +000010263 }
Chris Lattnerd6855142007-03-25 02:14:49 +000010264 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000010265}
10266
John Thompsone8360b72010-10-29 17:29:13 +000010267/// Examine constraint type and operand type and determine a weight value.
10268/// This object must already have been set up with the operand type
10269/// and the current alternative constraint selected.
10270TargetLowering::ConstraintWeight
10271ARMTargetLowering::getSingleConstraintMatchWeight(
10272 AsmOperandInfo &info, const char *constraint) const {
10273 ConstraintWeight weight = CW_Invalid;
10274 Value *CallOperandVal = info.CallOperandVal;
10275 // If we don't have a value, we can't do a match,
10276 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000010277 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000010278 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000010279 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000010280 // Look at the constraint type.
10281 switch (*constraint) {
10282 default:
10283 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10284 break;
10285 case 'l':
10286 if (type->isIntegerTy()) {
10287 if (Subtarget->isThumb())
10288 weight = CW_SpecificReg;
10289 else
10290 weight = CW_Register;
10291 }
10292 break;
10293 case 'w':
10294 if (type->isFloatingPointTy())
10295 weight = CW_Register;
10296 break;
10297 }
10298 return weight;
10299}
10300
Eric Christophercf2007c2011-06-30 23:50:52 +000010301typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
10302RCPair
Evan Cheng10043e22007-01-19 07:51:42 +000010303ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +000010304 MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000010305 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010306 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000010307 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000010308 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010309 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010310 return RCPair(0U, &ARM::tGPRRegClass);
10311 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000010312 case 'h': // High regs or no regs.
10313 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010314 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000010315 break;
Chris Lattner6223e832007-04-02 17:24:08 +000010316 case 'r':
Craig Topperc7242e02012-04-20 07:30:17 +000010317 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010318 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000010319 if (VT == MVT::Other)
10320 break;
Owen Anderson9f944592009-08-11 20:47:22 +000010321 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010322 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000010323 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010324 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000010325 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010326 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010327 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010328 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000010329 if (VT == MVT::Other)
10330 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010331 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010332 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010333 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010334 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010335 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010336 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010337 break;
Eric Christopherc011d312011-07-01 00:30:46 +000010338 case 't':
10339 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010340 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000010341 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010342 }
10343 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000010344 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000010345 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000010346
Evan Cheng10043e22007-01-19 07:51:42 +000010347 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
10348}
10349
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010350/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10351/// vector. If it is invalid, don't add anything to Ops.
10352void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000010353 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010354 std::vector<SDValue>&Ops,
10355 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000010356 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010357
Eric Christopherde9399b2011-06-02 23:16:42 +000010358 // Currently only support length 1 constraints.
10359 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000010360
Eric Christopherde9399b2011-06-02 23:16:42 +000010361 char ConstraintLetter = Constraint[0];
10362 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010363 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000010364 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010365 case 'I': case 'J': case 'K': case 'L':
10366 case 'M': case 'N': case 'O':
10367 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10368 if (!C)
10369 return;
10370
10371 int64_t CVal64 = C->getSExtValue();
10372 int CVal = (int) CVal64;
10373 // None of these constraints allow values larger than 32 bits. Check
10374 // that the value fits in an int.
10375 if (CVal != CVal64)
10376 return;
10377
Eric Christopherde9399b2011-06-02 23:16:42 +000010378 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000010379 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000010380 // Constant suitable for movw, must be between 0 and
10381 // 65535.
10382 if (Subtarget->hasV6T2Ops())
10383 if (CVal >= 0 && CVal <= 65535)
10384 break;
10385 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010386 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000010387 if (Subtarget->isThumb1Only()) {
10388 // This must be a constant between 0 and 255, for ADD
10389 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010390 if (CVal >= 0 && CVal <= 255)
10391 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010392 } else if (Subtarget->isThumb2()) {
10393 // A constant that can be used as an immediate value in a
10394 // data-processing instruction.
10395 if (ARM_AM::getT2SOImmVal(CVal) != -1)
10396 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010397 } else {
10398 // A constant that can be used as an immediate value in a
10399 // data-processing instruction.
10400 if (ARM_AM::getSOImmVal(CVal) != -1)
10401 break;
10402 }
10403 return;
10404
10405 case 'J':
David Goodwin22c2fba2009-07-08 23:10:31 +000010406 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010407 // This must be a constant between -255 and -1, for negated ADD
10408 // immediates. This can be used in GCC with an "n" modifier that
10409 // prints the negated value, for use with SUB instructions. It is
10410 // not useful otherwise but is implemented for compatibility.
10411 if (CVal >= -255 && CVal <= -1)
10412 break;
10413 } else {
10414 // This must be a constant between -4095 and 4095. It is not clear
10415 // what this constraint is intended for. Implemented for
10416 // compatibility with GCC.
10417 if (CVal >= -4095 && CVal <= 4095)
10418 break;
10419 }
10420 return;
10421
10422 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000010423 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010424 // A 32-bit value where only one byte has a nonzero value. Exclude
10425 // zero to match GCC. This constraint is used by GCC internally for
10426 // constants that can be loaded with a move/shift combination.
10427 // It is not useful otherwise but is implemented for compatibility.
10428 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
10429 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010430 } else if (Subtarget->isThumb2()) {
10431 // A constant whose bitwise inverse can be used as an immediate
10432 // value in a data-processing instruction. This can be used in GCC
10433 // with a "B" modifier that prints the inverted value, for use with
10434 // BIC and MVN instructions. It is not useful otherwise but is
10435 // implemented for compatibility.
10436 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
10437 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010438 } else {
10439 // A constant whose bitwise inverse can be used as an immediate
10440 // value in a data-processing instruction. This can be used in GCC
10441 // with a "B" modifier that prints the inverted value, for use with
10442 // BIC and MVN instructions. It is not useful otherwise but is
10443 // implemented for compatibility.
10444 if (ARM_AM::getSOImmVal(~CVal) != -1)
10445 break;
10446 }
10447 return;
10448
10449 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000010450 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010451 // This must be a constant between -7 and 7,
10452 // for 3-operand ADD/SUB immediate instructions.
10453 if (CVal >= -7 && CVal < 7)
10454 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010455 } else if (Subtarget->isThumb2()) {
10456 // A constant whose negation can be used as an immediate value in a
10457 // data-processing instruction. This can be used in GCC with an "n"
10458 // modifier that prints the negated value, for use with SUB
10459 // instructions. It is not useful otherwise but is implemented for
10460 // compatibility.
10461 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
10462 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010463 } else {
10464 // A constant whose negation can be used as an immediate value in a
10465 // data-processing instruction. This can be used in GCC with an "n"
10466 // modifier that prints the negated value, for use with SUB
10467 // instructions. It is not useful otherwise but is implemented for
10468 // compatibility.
10469 if (ARM_AM::getSOImmVal(-CVal) != -1)
10470 break;
10471 }
10472 return;
10473
10474 case 'M':
David Goodwin22c2fba2009-07-08 23:10:31 +000010475 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010476 // This must be a multiple of 4 between 0 and 1020, for
10477 // ADD sp + immediate.
10478 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
10479 break;
10480 } else {
10481 // A power of two or a constant between 0 and 32. This is used in
10482 // GCC for the shift amount on shifted register operands, but it is
10483 // useful in general for any shift amounts.
10484 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
10485 break;
10486 }
10487 return;
10488
10489 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000010490 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010491 // This must be a constant between 0 and 31, for shift amounts.
10492 if (CVal >= 0 && CVal <= 31)
10493 break;
10494 }
10495 return;
10496
10497 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000010498 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010499 // This must be a multiple of 4 between -508 and 508, for
10500 // ADD/SUB sp = sp + immediate.
10501 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
10502 break;
10503 }
10504 return;
10505 }
10506 Result = DAG.getTargetConstant(CVal, Op.getValueType());
10507 break;
10508 }
10509
10510 if (Result.getNode()) {
10511 Ops.push_back(Result);
10512 return;
10513 }
Dale Johannesence97d552010-06-25 21:55:36 +000010514 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010515}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010516
Renato Golin87610692013-07-16 09:32:17 +000010517SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
10518 assert(Subtarget->isTargetAEABI() && "Register-based DivRem lowering only");
10519 unsigned Opcode = Op->getOpcode();
10520 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
10521 "Invalid opcode for Div/Rem lowering");
10522 bool isSigned = (Opcode == ISD::SDIVREM);
10523 EVT VT = Op->getValueType(0);
10524 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
10525
10526 RTLIB::Libcall LC;
10527 switch (VT.getSimpleVT().SimpleTy) {
10528 default: llvm_unreachable("Unexpected request for libcall!");
10529 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
10530 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
10531 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
10532 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
10533 }
10534
10535 SDValue InChain = DAG.getEntryNode();
10536
10537 TargetLowering::ArgListTy Args;
10538 TargetLowering::ArgListEntry Entry;
10539 for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) {
10540 EVT ArgVT = Op->getOperand(i).getValueType();
10541 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
10542 Entry.Node = Op->getOperand(i);
10543 Entry.Ty = ArgTy;
10544 Entry.isSExt = isSigned;
10545 Entry.isZExt = !isSigned;
10546 Args.push_back(Entry);
10547 }
10548
10549 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
10550 getPointerTy());
10551
10552 Type *RetTy = (Type*)StructType::get(Ty, Ty, NULL);
10553
10554 SDLoc dl(Op);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010555 TargetLowering::CallLoweringInfo CLI(DAG);
10556 CLI.setDebugLoc(dl).setChain(InChain)
10557 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, &Args, 0)
10558 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000010559
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010560 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000010561 return CallInfo.first;
10562}
10563
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000010564SDValue
10565ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
10566 assert(Subtarget->isTargetWindows() && "unsupported target platform");
10567 SDLoc DL(Op);
10568
10569 // Get the inputs.
10570 SDValue Chain = Op.getOperand(0);
10571 SDValue Size = Op.getOperand(1);
10572
10573 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
10574 DAG.getConstant(2, MVT::i32));
10575
10576 SDValue Flag;
10577 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
10578 Flag = Chain.getValue(1);
10579
10580 SDVTList NodeTys = DAG.getVTList(MVT::i32, MVT::Glue);
10581 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
10582
10583 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
10584 Chain = NewSP.getValue(1);
10585
10586 SDValue Ops[2] = { NewSP, Chain };
10587 return DAG.getMergeValues(Ops, DL);
10588}
10589
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010590bool
10591ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
10592 // The ARM target isn't yet aware of offsets.
10593 return false;
10594}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010595
Jim Grosbach11013ed2010-07-16 23:05:05 +000010596bool ARM::isBitFieldInvertedMask(unsigned v) {
10597 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010598 return false;
10599
Jim Grosbach11013ed2010-07-16 23:05:05 +000010600 // there can be 1's on either or both "outsides", all the "inside"
10601 // bits must be 0's
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010602 unsigned TO = CountTrailingOnes_32(v);
10603 unsigned LO = CountLeadingOnes_32(v);
10604 v = (v >> TO) << TO;
10605 v = (v << LO) >> LO;
10606 return v == 0;
Jim Grosbach11013ed2010-07-16 23:05:05 +000010607}
10608
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010609/// isFPImmLegal - Returns true if the target can instruction select the
10610/// specified FP immediate natively. If false, the legalizer will
10611/// materialize the FP immediate as a load from a constant pool.
10612bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
10613 if (!Subtarget->hasVFP3())
10614 return false;
10615 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000010616 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010617 if (VT == MVT::f64)
Jim Grosbachefc761a2011-09-30 00:50:06 +000010618 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010619 return false;
10620}
Bob Wilson5549d492010-09-21 17:56:22 +000010621
Wesley Peck527da1b2010-11-23 03:31:01 +000010622/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000010623/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
10624/// specified in the intrinsic calls.
10625bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
10626 const CallInst &I,
10627 unsigned Intrinsic) const {
10628 switch (Intrinsic) {
10629 case Intrinsic::arm_neon_vld1:
10630 case Intrinsic::arm_neon_vld2:
10631 case Intrinsic::arm_neon_vld3:
10632 case Intrinsic::arm_neon_vld4:
10633 case Intrinsic::arm_neon_vld2lane:
10634 case Intrinsic::arm_neon_vld3lane:
10635 case Intrinsic::arm_neon_vld4lane: {
10636 Info.opc = ISD::INTRINSIC_W_CHAIN;
10637 // Conservatively set memVT to the entire set of vectors loaded.
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010638 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010639 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10640 Info.ptrVal = I.getArgOperand(0);
10641 Info.offset = 0;
10642 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10643 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10644 Info.vol = false; // volatile loads with NEON intrinsics not supported
10645 Info.readMem = true;
10646 Info.writeMem = false;
10647 return true;
10648 }
10649 case Intrinsic::arm_neon_vst1:
10650 case Intrinsic::arm_neon_vst2:
10651 case Intrinsic::arm_neon_vst3:
10652 case Intrinsic::arm_neon_vst4:
10653 case Intrinsic::arm_neon_vst2lane:
10654 case Intrinsic::arm_neon_vst3lane:
10655 case Intrinsic::arm_neon_vst4lane: {
10656 Info.opc = ISD::INTRINSIC_VOID;
10657 // Conservatively set memVT to the entire set of vectors stored.
10658 unsigned NumElts = 0;
10659 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000010660 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000010661 if (!ArgTy->isVectorTy())
10662 break;
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010663 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010664 }
10665 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10666 Info.ptrVal = I.getArgOperand(0);
10667 Info.offset = 0;
10668 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10669 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10670 Info.vol = false; // volatile stores with NEON intrinsics not supported
10671 Info.readMem = false;
10672 Info.writeMem = true;
10673 return true;
10674 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010675 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000010676 case Intrinsic::arm_ldrex: {
10677 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
10678 Info.opc = ISD::INTRINSIC_W_CHAIN;
10679 Info.memVT = MVT::getVT(PtrTy->getElementType());
10680 Info.ptrVal = I.getArgOperand(0);
10681 Info.offset = 0;
10682 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10683 Info.vol = true;
10684 Info.readMem = true;
10685 Info.writeMem = false;
10686 return true;
10687 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010688 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000010689 case Intrinsic::arm_strex: {
10690 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
10691 Info.opc = ISD::INTRINSIC_W_CHAIN;
10692 Info.memVT = MVT::getVT(PtrTy->getElementType());
10693 Info.ptrVal = I.getArgOperand(1);
10694 Info.offset = 0;
10695 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10696 Info.vol = true;
10697 Info.readMem = false;
10698 Info.writeMem = true;
10699 return true;
10700 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010701 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010702 case Intrinsic::arm_strexd: {
10703 Info.opc = ISD::INTRINSIC_W_CHAIN;
10704 Info.memVT = MVT::i64;
10705 Info.ptrVal = I.getArgOperand(2);
10706 Info.offset = 0;
10707 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000010708 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010709 Info.readMem = false;
10710 Info.writeMem = true;
10711 return true;
10712 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010713 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010714 case Intrinsic::arm_ldrexd: {
10715 Info.opc = ISD::INTRINSIC_W_CHAIN;
10716 Info.memVT = MVT::i64;
10717 Info.ptrVal = I.getArgOperand(0);
10718 Info.offset = 0;
10719 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000010720 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010721 Info.readMem = true;
10722 Info.writeMem = false;
10723 return true;
10724 }
Bob Wilson5549d492010-09-21 17:56:22 +000010725 default:
10726 break;
10727 }
10728
10729 return false;
10730}
Juergen Ributzka659ce002014-01-28 01:20:14 +000010731
10732/// \brief Returns true if it is beneficial to convert a load of a constant
10733/// to just the constant itself.
10734bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
10735 Type *Ty) const {
10736 assert(Ty->isIntegerTy());
10737
10738 unsigned Bits = Ty->getPrimitiveSizeInBits();
10739 if (Bits == 0 || Bits > 32)
10740 return false;
10741 return true;
10742}
Tim Northover037f26f22014-04-17 18:22:47 +000010743
10744bool ARMTargetLowering::shouldExpandAtomicInIR(Instruction *Inst) const {
10745 // Loads and stores less than 64-bits are already atomic; ones above that
10746 // are doomed anyway, so defer to the default libcall and blame the OS when
Tim Northoverb45c3b72014-06-16 18:49:36 +000010747 // things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
10748 // anything for those.
10749 bool IsMClass = Subtarget->isMClass();
10750 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
10751 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
10752 return Size == 64 && !IsMClass;
10753 } else if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
10754 return LI->getType()->getPrimitiveSizeInBits() == 64 && !IsMClass;
10755 }
Tim Northover037f26f22014-04-17 18:22:47 +000010756
Tim Northoverb45c3b72014-06-16 18:49:36 +000010757 // For the real atomic operations, we have ldrex/strex up to 32 bits,
10758 // and up to 64 bits on the non-M profiles
10759 unsigned AtomicLimit = IsMClass ? 32 : 64;
10760 return Inst->getType()->getPrimitiveSizeInBits() <= AtomicLimit;
Tim Northover037f26f22014-04-17 18:22:47 +000010761}
10762
10763Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
10764 AtomicOrdering Ord) const {
10765 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
10766 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
10767 bool IsAcquire =
10768 Ord == Acquire || Ord == AcquireRelease || Ord == SequentiallyConsistent;
10769
10770 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
10771 // intrinsic must return {i32, i32} and we have to recombine them into a
10772 // single i64 here.
10773 if (ValTy->getPrimitiveSizeInBits() == 64) {
10774 Intrinsic::ID Int =
10775 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
10776 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
10777
10778 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
10779 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
10780
10781 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
10782 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000010783 if (!Subtarget->isLittle())
10784 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000010785 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
10786 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
10787 return Builder.CreateOr(
10788 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
10789 }
10790
10791 Type *Tys[] = { Addr->getType() };
10792 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
10793 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
10794
10795 return Builder.CreateTruncOrBitCast(
10796 Builder.CreateCall(Ldrex, Addr),
10797 cast<PointerType>(Addr->getType())->getElementType());
10798}
10799
10800Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
10801 Value *Addr,
10802 AtomicOrdering Ord) const {
10803 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
10804 bool IsRelease =
10805 Ord == Release || Ord == AcquireRelease || Ord == SequentiallyConsistent;
10806
10807 // Since the intrinsics must have legal type, the i64 intrinsics take two
10808 // parameters: "i32, i32". We must marshal Val into the appropriate form
10809 // before the call.
10810 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
10811 Intrinsic::ID Int =
10812 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
10813 Function *Strex = Intrinsic::getDeclaration(M, Int);
10814 Type *Int32Ty = Type::getInt32Ty(M->getContext());
10815
10816 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
10817 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000010818 if (!Subtarget->isLittle())
10819 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000010820 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
10821 return Builder.CreateCall3(Strex, Lo, Hi, Addr);
10822 }
10823
10824 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
10825 Type *Tys[] = { Addr->getType() };
10826 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
10827
10828 return Builder.CreateCall2(
10829 Strex, Builder.CreateZExtOrBitCast(
10830 Val, Strex->getFunctionType()->getParamType(0)),
10831 Addr);
10832}
Oliver Stannardc24f2172014-05-09 14:01:47 +000010833
10834enum HABaseType {
10835 HA_UNKNOWN = 0,
10836 HA_FLOAT,
10837 HA_DOUBLE,
10838 HA_VECT64,
10839 HA_VECT128
10840};
10841
10842static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
10843 uint64_t &Members) {
10844 if (const StructType *ST = dyn_cast<StructType>(Ty)) {
10845 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
10846 uint64_t SubMembers = 0;
10847 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
10848 return false;
10849 Members += SubMembers;
10850 }
10851 } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
10852 uint64_t SubMembers = 0;
10853 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
10854 return false;
10855 Members += SubMembers * AT->getNumElements();
10856 } else if (Ty->isFloatTy()) {
10857 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
10858 return false;
10859 Members = 1;
10860 Base = HA_FLOAT;
10861 } else if (Ty->isDoubleTy()) {
10862 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
10863 return false;
10864 Members = 1;
10865 Base = HA_DOUBLE;
10866 } else if (const VectorType *VT = dyn_cast<VectorType>(Ty)) {
10867 Members = 1;
10868 switch (Base) {
10869 case HA_FLOAT:
10870 case HA_DOUBLE:
10871 return false;
10872 case HA_VECT64:
10873 return VT->getBitWidth() == 64;
10874 case HA_VECT128:
10875 return VT->getBitWidth() == 128;
10876 case HA_UNKNOWN:
10877 switch (VT->getBitWidth()) {
10878 case 64:
10879 Base = HA_VECT64;
10880 return true;
10881 case 128:
10882 Base = HA_VECT128;
10883 return true;
10884 default:
10885 return false;
10886 }
10887 }
10888 }
10889
10890 return (Members > 0 && Members <= 4);
10891}
10892
10893/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate.
10894bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
10895 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000010896 if (getEffectiveCallingConv(CallConv, isVarArg) !=
10897 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000010898 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000010899
10900 HABaseType Base = HA_UNKNOWN;
10901 uint64_t Members = 0;
10902 bool result = isHomogeneousAggregate(Ty, Base, Members);
10903 DEBUG(dbgs() << "isHA: " << result << " "; Ty->dump(); dbgs() << "\n");
10904 return result;
Oliver Stannardc24f2172014-05-09 14:01:47 +000010905}