blob: 81e15719154e3ad8ecbb877a6990453ca7864cd6 [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,
Eric Christopherb5217502014-08-06 18:45:26 +000073 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
74 ParmContext PC)
75 : CCState(CC, isVarArg, MF, locs, C) {
Cameron Zwarich89019782011-06-10 20:59:24 +000076 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>();
Eric Christopherd9134482014-08-04 21:25:23 +0000169 RegInfo = TM.getSubtargetImpl()->getRegisterInfo();
170 Itins = TM.getSubtargetImpl()->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 },
Saleem Abdulrasool017bd572014-08-17 22:51:02 +0000316 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000317
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000318 // Integer to floating-point conversions.
319 // RTABI chapter 4.1.2, Table 8
320 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
321 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
322 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
323 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
324 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
325 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
326 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
327 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000328
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000329 // Long long helper functions
330 // RTABI chapter 4.2, Table 9
Saleem Abdulrasool017bd572014-08-17 22:51:02 +0000331 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
332 // FIXME: __aeabi_ldivmod is SDIVREM not SDIV; we should custom lower this
333 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
334 { RTLIB::SDIVREM_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
335 // FIXME: __aeabi_uldivmod is UDIVREM not UDIV; we should custom lower this
336 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
337 { RTLIB::UDIVREM_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
338 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
339 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
340 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000341
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000342 // Integer division functions
343 // RTABI chapter 4.3.1
Saleem Abdulrasool017bd572014-08-17 22:51:02 +0000344 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
345 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
346 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
347 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
348 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
349 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
350 { RTLIB::SDIVREM_I8, "__aeabi_idivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
351 { RTLIB::SDIVREM_I16, "__aeabi_idivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
352 { RTLIB::SDIVREM_I32, "__aeabi_idivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
353 { RTLIB::UDIVREM_I8, "__aeabi_uidivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
354 { RTLIB::UDIVREM_I16, "__aeabi_uidivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
355 { RTLIB::UDIVREM_I32, "__aeabi_uidivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Renato Golin4cd51872011-05-22 21:41:23 +0000356
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000357 // Memory operations
358 // RTABI chapter 4.3.4
359 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
360 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
361 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
362 };
363
364 for (const auto &LC : LibraryCalls) {
365 setLibcallName(LC.Op, LC.Name);
366 setLibcallCallingConv(LC.Op, LC.CC);
367 if (LC.Cond != ISD::SETCC_INVALID)
368 setCmpLibcallCC(LC.Op, LC.Cond);
369 }
Saleem Abdulrasool017bd572014-08-17 22:51:02 +0000370
371 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
372 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +0000373 }
374
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000375 if (Subtarget->isTargetWindows()) {
376 static const struct {
377 const RTLIB::Libcall Op;
378 const char * const Name;
379 const CallingConv::ID CC;
380 } LibraryCalls[] = {
381 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP },
382 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP },
383 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP },
384 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP },
385 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP },
386 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP },
387 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP },
388 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP },
389 };
390
391 for (const auto &LC : LibraryCalls) {
392 setLibcallName(LC.Op, LC.Name);
393 setLibcallCallingConv(LC.Op, LC.CC);
394 }
395 }
396
Bob Wilsonbc158992011-10-07 16:59:21 +0000397 // Use divmod compiler-rt calls for iOS 5.0 and later.
Cameron Esfahani943908b2013-08-29 20:23:14 +0000398 if (Subtarget->getTargetTriple().isiOS() &&
Bob Wilsonbc158992011-10-07 16:59:21 +0000399 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
400 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
401 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
402 }
403
Oliver Stannard11790b22014-08-11 09:12:32 +0000404 // The half <-> float conversion functions are always soft-float, but are
405 // needed for some targets which use a hard-float calling convention by
406 // default.
407 if (Subtarget->isAAPCS_ABI()) {
408 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS);
409 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS);
410 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS);
411 } else {
412 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS);
413 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS);
414 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS);
415 }
416
David Goodwin22c2fba2009-07-08 23:10:31 +0000417 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000418 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000419 else
Craig Topperc7242e02012-04-20 07:30:17 +0000420 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000421 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
422 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000423 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Jim Grosbach4d5dc3e2010-08-11 15:44:15 +0000424 if (!Subtarget->isFPOnlySP())
Craig Topperc7242e02012-04-20 07:30:17 +0000425 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Evan Cheng10043e22007-01-19 07:51:42 +0000426 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000427
Eli Friedman6f84fed2011-11-08 01:43:53 +0000428 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
429 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
430 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
431 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
432 setTruncStoreAction((MVT::SimpleValueType)VT,
433 (MVT::SimpleValueType)InnerVT, Expand);
434 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
435 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
436 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Benjamin Kramer4dae5982014-04-26 12:06:28 +0000437
438 setOperationAction(ISD::MULHS, (MVT::SimpleValueType)VT, Expand);
439 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
440 setOperationAction(ISD::MULHU, (MVT::SimpleValueType)VT, Expand);
441 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000442
443 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Eli Friedman6f84fed2011-11-08 01:43:53 +0000444 }
445
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000446 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000447 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000448
Bob Wilson2e076c42009-06-22 23:27:02 +0000449 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000450 addDRTypeForNEON(MVT::v2f32);
451 addDRTypeForNEON(MVT::v8i8);
452 addDRTypeForNEON(MVT::v4i16);
453 addDRTypeForNEON(MVT::v2i32);
454 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000455
Owen Anderson9f944592009-08-11 20:47:22 +0000456 addQRTypeForNEON(MVT::v4f32);
457 addQRTypeForNEON(MVT::v2f64);
458 addQRTypeForNEON(MVT::v16i8);
459 addQRTypeForNEON(MVT::v8i16);
460 addQRTypeForNEON(MVT::v4i32);
461 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000462
Bob Wilson194a2512009-09-15 23:55:57 +0000463 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
464 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000465 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
466 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000467 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
468 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
469 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000470 // FIXME: Code duplication: FDIV and FREM are expanded always, see
471 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000472 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
473 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000474 // FIXME: Create unittest.
475 // In another words, find a way when "copysign" appears in DAG with vector
476 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000477 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000478 // FIXME: Code duplication: SETCC has custom operation action, see
479 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000480 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000481 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000482 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
483 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
484 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
485 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
486 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
487 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
488 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
489 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
490 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
491 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
492 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
493 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000494 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000495 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
496 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
497 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
498 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
499 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000500 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000501
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000502 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
503 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
504 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
505 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
506 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
507 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
508 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
509 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
510 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
511 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000512 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
513 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
514 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
515 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000516 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000517
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000518 // Mark v2f32 intrinsics.
519 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
520 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
521 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
522 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
523 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
524 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
525 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
526 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
527 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
528 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
529 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
530 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
531 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
532 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
533 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
534
Bob Wilson6cc46572009-09-16 00:32:15 +0000535 // Neon does not support some operations on v1i64 and v2i64 types.
536 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000537 // Custom handling for some quad-vector types to detect VMULL.
538 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
539 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
540 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000541 // Custom handling for some vector types to avoid expensive expansions
542 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
543 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
544 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
545 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000546 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
547 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000548 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000549 // a destination type that is wider than the source, and nor does
550 // it have a FP_TO_[SU]INT instruction with a narrower destination than
551 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000552 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
553 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000554 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
555 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000556
Eli Friedmane6385e62012-11-15 22:44:27 +0000557 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000558 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000559
Evan Chengb4eae132012-12-04 22:41:50 +0000560 // NEON does not have single instruction CTPOP for vectors with element
561 // types wider than 8-bits. However, custom lowering can leverage the
562 // v8i8/v16i8 vcnt instruction.
563 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
564 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
565 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
566 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
567
Jim Grosbach5f215872013-02-27 21:31:12 +0000568 // NEON only has FMA instructions as of VFP4.
569 if (!Subtarget->hasVFP4()) {
570 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
571 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
572 }
573
Bob Wilson06fce872011-02-07 17:43:21 +0000574 setTargetDAGCombine(ISD::INTRINSIC_VOID);
575 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000576 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
577 setTargetDAGCombine(ISD::SHL);
578 setTargetDAGCombine(ISD::SRL);
579 setTargetDAGCombine(ISD::SRA);
580 setTargetDAGCombine(ISD::SIGN_EXTEND);
581 setTargetDAGCombine(ISD::ZERO_EXTEND);
582 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsonc6c13a32010-02-18 06:05:53 +0000583 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000584 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000585 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000586 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
587 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000588 setTargetDAGCombine(ISD::FP_TO_SINT);
589 setTargetDAGCombine(ISD::FP_TO_UINT);
590 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem097106b2011-10-15 20:03:12 +0000591
James Molloy547d4c02012-02-20 09:24:05 +0000592 // It is legal to extload from v4i8 to v4i16 or v4i32.
593 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
594 MVT::v4i16, MVT::v2i16,
595 MVT::v2i32};
596 for (unsigned i = 0; i < 6; ++i) {
597 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
598 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
599 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
600 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000601 }
602
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000603 // ARM and Thumb2 support UMLAL/SMLAL.
604 if (!Subtarget->isThumb1Only())
605 setTargetDAGCombine(ISD::ADDC);
606
607
Evan Cheng6addd652007-05-18 00:19:34 +0000608 computeRegisterProperties();
Evan Cheng10043e22007-01-19 07:51:42 +0000609
Tim Northover4e80b582014-07-18 13:01:19 +0000610 // ARM does not have floating-point extending loads.
Owen Anderson9f944592009-08-11 20:47:22 +0000611 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Tim Northover4e80b582014-07-18 13:01:19 +0000612 setLoadExtAction(ISD::EXTLOAD, MVT::f16, Expand);
613
614 // ... or truncating stores
615 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
616 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
617 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000618
Duncan Sands95d46ef2008-01-23 20:39:46 +0000619 // ARM does not have i1 sign extending load.
Owen Anderson9f944592009-08-11 20:47:22 +0000620 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000621
Evan Cheng10043e22007-01-19 07:51:42 +0000622 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000623 if (!Subtarget->isThumb1Only()) {
624 for (unsigned im = (unsigned)ISD::PRE_INC;
625 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000626 setIndexedLoadAction(im, MVT::i1, Legal);
627 setIndexedLoadAction(im, MVT::i8, Legal);
628 setIndexedLoadAction(im, MVT::i16, Legal);
629 setIndexedLoadAction(im, MVT::i32, Legal);
630 setIndexedStoreAction(im, MVT::i1, Legal);
631 setIndexedStoreAction(im, MVT::i8, Legal);
632 setIndexedStoreAction(im, MVT::i16, Legal);
633 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000634 }
Evan Cheng10043e22007-01-19 07:51:42 +0000635 }
636
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000637 setOperationAction(ISD::SADDO, MVT::i32, Custom);
638 setOperationAction(ISD::UADDO, MVT::i32, Custom);
639 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
640 setOperationAction(ISD::USUBO, MVT::i32, Custom);
641
Evan Cheng10043e22007-01-19 07:51:42 +0000642 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000643 setOperationAction(ISD::MUL, MVT::i64, Expand);
644 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000645 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000646 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
647 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000648 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000649 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
650 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000651 setOperationAction(ISD::MULHS, MVT::i32, Expand);
652
Jim Grosbach5d994042009-10-31 19:38:01 +0000653 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000654 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000655 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000656 setOperationAction(ISD::SRL, MVT::i64, Custom);
657 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000658
Evan Chenge8916542011-08-30 01:34:54 +0000659 if (!Subtarget->isThumb1Only()) {
660 // FIXME: We should do this for Thumb1 as well.
661 setOperationAction(ISD::ADDC, MVT::i32, Custom);
662 setOperationAction(ISD::ADDE, MVT::i32, Custom);
663 setOperationAction(ISD::SUBC, MVT::i32, Custom);
664 setOperationAction(ISD::SUBE, MVT::i32, Custom);
665 }
666
Evan Cheng10043e22007-01-19 07:51:42 +0000667 // ARM does not have ROTL.
Owen Anderson9f944592009-08-11 20:47:22 +0000668 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach8546ec92010-01-18 19:58:49 +0000669 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000670 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000671 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000672 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000673
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000674 // These just redirect to CTTZ and CTLZ on ARM.
675 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
676 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
677
Tim Northoverbc933082013-05-23 19:11:20 +0000678 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
679
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000680 // Only ARMv6 has BSWAP.
681 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000682 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000683
Bob Wilsone8a549c2012-09-29 21:43:49 +0000684 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
685 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
686 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbach92d999002010-05-05 20:44:35 +0000687 setOperationAction(ISD::SDIV, MVT::i32, Expand);
688 setOperationAction(ISD::UDIV, MVT::i32, Expand);
689 }
Renato Golin87610692013-07-16 09:32:17 +0000690
691 // FIXME: Also set divmod for SREM on EABI
Saleem Abdulrasool017bd572014-08-17 22:51:02 +0000692 setOperationAction(ISD::SREM, MVT::i32, Expand);
693 setOperationAction(ISD::UREM, MVT::i32, Expand);
694 if (!Subtarget->isTargetAEABI()) {
Renato Golin87610692013-07-16 09:32:17 +0000695 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
696 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
697 }
Bob Wilson7117a912009-03-20 22:42:55 +0000698
Owen Anderson9f944592009-08-11 20:47:22 +0000699 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
700 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
701 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
702 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000703 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000704
Evan Cheng74d92c12011-04-08 21:37:21 +0000705 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000706
Evan Cheng10043e22007-01-19 07:51:42 +0000707 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000708 setOperationAction(ISD::VASTART, MVT::Other, Custom);
709 setOperationAction(ISD::VAARG, MVT::Other, Expand);
710 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
711 setOperationAction(ISD::VAEND, MVT::Other, Expand);
712 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
713 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000714
Tim Northoverd6a729b2014-01-06 14:28:05 +0000715 if (!Subtarget->isTargetMachO()) {
716 // Non-MachO platforms may return values in these registers via the
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000717 // personality function.
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000718 setExceptionPointerRegister(ARM::R0);
719 setExceptionSelectorRegister(ARM::R1);
720 }
Anton Korobeynikovf3a62312011-01-24 22:38:45 +0000721
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +0000722 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
723 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
724 else
725 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
726
Evan Cheng6e809de2010-08-11 06:22:01 +0000727 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
728 // the default expansion.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000729 if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000730 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
731 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000732 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000733
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000734 // On v8, we have particularly efficient implementations of atomic fences
735 // if they can be combined with nearby atomic loads and stores.
736 if (!Subtarget->hasV8Ops()) {
Robin Morissetd18cda62014-08-15 22:17:28 +0000737 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc.
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000738 setInsertFencesForAtomic(true);
739 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000740 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000741 // If there's anything we can use as a barrier, go through custom lowering
742 // for ATOMIC_FENCE.
743 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
744 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
745
Jim Grosbach6860bb72010-06-18 22:35:32 +0000746 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000747 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000748 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000749 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000750 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000751 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000752 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000753 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000754 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000755 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000756 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000757 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000758 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000759 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
760 // Unordered/Monotonic case.
761 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
762 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000763 }
Evan Cheng10043e22007-01-19 07:51:42 +0000764
Evan Cheng21acf9f2010-11-04 05:19:35 +0000765 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000766
Eli Friedman8cfa7712010-06-26 04:36:50 +0000767 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
768 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000769 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
770 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000771 }
Owen Anderson9f944592009-08-11 20:47:22 +0000772 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000773
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000774 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
775 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000776 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000777 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000778 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000779 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
780 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000781
782 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000783 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbach31984832010-07-07 00:07:57 +0000784 if (Subtarget->isTargetDarwin()) {
785 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
786 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall7d84ece2011-05-29 19:50:32 +0000787 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbach31984832010-07-07 00:07:57 +0000788 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000789
Owen Anderson9f944592009-08-11 20:47:22 +0000790 setOperationAction(ISD::SETCC, MVT::i32, Expand);
791 setOperationAction(ISD::SETCC, MVT::f32, Expand);
792 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000793 setOperationAction(ISD::SELECT, MVT::i32, Custom);
794 setOperationAction(ISD::SELECT, MVT::f32, Custom);
795 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000796 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
797 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
798 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000799
Owen Anderson9f944592009-08-11 20:47:22 +0000800 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
801 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
802 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
803 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
804 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000805
Dan Gohman482732a2007-10-11 23:21:31 +0000806 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000807 setOperationAction(ISD::FSIN, MVT::f64, Expand);
808 setOperationAction(ISD::FSIN, MVT::f32, Expand);
809 setOperationAction(ISD::FCOS, MVT::f32, Expand);
810 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000811 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
812 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000813 setOperationAction(ISD::FREM, MVT::f64, Expand);
814 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000815 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
816 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000817 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
818 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000819 }
Owen Anderson9f944592009-08-11 20:47:22 +0000820 setOperationAction(ISD::FPOW, MVT::f64, Expand);
821 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000822
Evan Chengd0007f32012-04-10 21:40:28 +0000823 if (!Subtarget->hasVFP4()) {
824 setOperationAction(ISD::FMA, MVT::f64, Expand);
825 setOperationAction(ISD::FMA, MVT::f32, Expand);
826 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000827
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000828 // Various VFP goodness
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000829 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilsone4191e72010-03-19 22:51:32 +0000830 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
831 if (Subtarget->hasVFP2()) {
832 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
833 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
834 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
835 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
836 }
Tim Northover53f3bcf2014-07-17 11:27:04 +0000837
838 // v8 adds f64 <-> f16 conversion. Before that it should be expanded.
839 if (!Subtarget->hasV8Ops()) {
840 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
841 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
842 }
843
844 // fp16 is a special v7 extension that adds f16 <-> f32 conversions.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000845 if (!Subtarget->hasFP16()) {
Tim Northoverfd7e4242014-07-17 10:51:23 +0000846 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
847 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000848 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000849 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000850
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000851 // Combine sin / cos into one node or libcall if possible.
852 if (Subtarget->hasSinCos()) {
853 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
854 setLibcallName(RTLIB::SINCOS_F64, "sincos");
855 if (Subtarget->getTargetTriple().getOS() == Triple::IOS) {
856 // For iOS, we don't want to the normal expansion of a libcall to
857 // sincos. We want to issue a libcall to __sincos_stret.
858 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
859 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
860 }
861 }
Evan Cheng10043e22007-01-19 07:51:42 +0000862
Chad Rosierb1bbf6f2014-08-15 21:38:16 +0000863 // ARMv8 implements a lot of rounding-like FP operations.
864 if (Subtarget->hasV8Ops()) {
865 static MVT RoundingTypes[] = {MVT::f32, MVT::f64};
866 for (const auto Ty : RoundingTypes) {
867 setOperationAction(ISD::FFLOOR, Ty, Legal);
868 setOperationAction(ISD::FCEIL, Ty, Legal);
869 setOperationAction(ISD::FROUND, Ty, Legal);
870 setOperationAction(ISD::FTRUNC, Ty, Legal);
871 setOperationAction(ISD::FNEARBYINT, Ty, Legal);
872 setOperationAction(ISD::FRINT, Ty, Legal);
873 }
874 }
Chris Lattnerf3f4ad92007-11-27 22:36:16 +0000875 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000876 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +0000877 setTargetDAGCombine(ISD::ADD);
878 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +0000879 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +0000880 setTargetDAGCombine(ISD::AND);
881 setTargetDAGCombine(ISD::OR);
882 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +0000883
Evan Chengf258a152012-02-23 02:58:19 +0000884 if (Subtarget->hasV6Ops())
885 setTargetDAGCombine(ISD::SRL);
886
Evan Cheng10043e22007-01-19 07:51:42 +0000887 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +0000888
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000889 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
890 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +0000891 setSchedulingPreference(Sched::RegPressure);
892 else
893 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +0000894
Evan Cheng3ae2b792011-01-06 06:52:41 +0000895 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000896 MaxStoresPerMemset = 8;
897 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
898 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
899 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
900 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
901 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
Evan Chengb71233f2010-06-26 01:52:05 +0000902
Rafael Espindolaa76eccf2010-07-11 04:01:49 +0000903 // On ARM arguments smaller than 4 bytes are extended, so all arguments
904 // are at least 4 bytes aligned.
905 setMinStackArgumentAlignment(4);
906
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000907 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000908 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000909
Eli Friedman2518f832011-05-06 20:34:06 +0000910 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +0000911}
912
Andrew Trick43f25632011-01-19 02:35:27 +0000913// FIXME: It might make sense to define the representative register class as the
914// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
915// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
916// SPR's representative would be DPR_VFP2. This should work well if register
917// pressure tracking were modified such that a register use would increment the
918// pressure of the register class's representative and all of it's super
919// classes' representatives transitively. We have not implemented this because
920// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000921// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +0000922// and extractions.
Evan Chenga77f3d32010-07-21 06:09:07 +0000923std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000924ARMTargetLowering::findRepresentativeClass(MVT VT) const{
Craig Topper062a2ba2014-04-25 05:30:21 +0000925 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +0000926 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000927 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +0000928 default:
Evan Chenga77f3d32010-07-21 06:09:07 +0000929 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng28590382010-07-21 23:53:58 +0000930 // Use DPR as representative register class for all floating point
931 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
932 // the cost is 1 for both f32 and f64.
933 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +0000934 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +0000935 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +0000936 // When NEON is used for SP, only half of the register file is available
937 // because operations that define both SP and DP results will be constrained
938 // to the VFP2 class (D0-D15). We currently model this constraint prior to
939 // coalescing by double-counting the SP regs. See the FIXME above.
940 if (Subtarget->useNEONForSinglePrecisionFP())
941 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000942 break;
943 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
944 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +0000945 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000946 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000947 break;
948 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +0000949 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000950 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +0000951 break;
952 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +0000953 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000954 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +0000955 break;
Evan Cheng10f99a32010-07-19 22:15:08 +0000956 }
Evan Chenga77f3d32010-07-21 06:09:07 +0000957 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +0000958}
959
Evan Cheng10043e22007-01-19 07:51:42 +0000960const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
961 switch (Opcode) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000962 default: return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +0000963 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +0000964 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +0000965 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
966 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +0000967 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +0000968 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
969 case ARMISD::tCALL: return "ARMISD::tCALL";
970 case ARMISD::BRCOND: return "ARMISD::BRCOND";
971 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +0000972 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +0000973 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +0000974 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +0000975 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
976 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +0000977 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +0000978 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +0000979 case ARMISD::CMPFP: return "ARMISD::CMPFP";
980 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +0000981 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +0000982 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +0000983
Evan Cheng10043e22007-01-19 07:51:42 +0000984 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +0000985
Jim Grosbach8546ec92010-01-18 19:58:49 +0000986 case ARMISD::RBIT: return "ARMISD::RBIT";
987
Bob Wilsone4191e72010-03-19 22:51:32 +0000988 case ARMISD::FTOSI: return "ARMISD::FTOSI";
989 case ARMISD::FTOUI: return "ARMISD::FTOUI";
990 case ARMISD::SITOF: return "ARMISD::SITOF";
991 case ARMISD::UITOF: return "ARMISD::UITOF";
992
Evan Cheng10043e22007-01-19 07:51:42 +0000993 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
994 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
995 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +0000996
Evan Chenge8916542011-08-30 01:34:54 +0000997 case ARMISD::ADDC: return "ARMISD::ADDC";
998 case ARMISD::ADDE: return "ARMISD::ADDE";
999 case ARMISD::SUBC: return "ARMISD::SUBC";
1000 case ARMISD::SUBE: return "ARMISD::SUBE";
1001
Bob Wilson22806742010-09-22 22:09:21 +00001002 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
1003 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001004
Evan Chengec6d7c92009-10-28 06:55:03 +00001005 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
1006 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
1007
Dale Johannesend679ff72010-06-03 21:09:53 +00001008 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001009
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001010 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001011
Evan Chengb972e562009-08-07 00:34:42 +00001012 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1013
Bob Wilson7ed59712010-10-30 00:54:37 +00001014 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001015
Evan Cheng8740ee32010-11-03 06:34:55 +00001016 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1017
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001018 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK";
1019
Bob Wilson2e076c42009-06-22 23:27:02 +00001020 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001021 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001022 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001023 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1024 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001025 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1026 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001027 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1028 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001029 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1030 case ARMISD::VTST: return "ARMISD::VTST";
1031
1032 case ARMISD::VSHL: return "ARMISD::VSHL";
1033 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1034 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001035 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1036 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1037 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1038 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1039 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1040 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1041 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1042 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1043 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1044 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1045 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1046 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
1047 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1048 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001049 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001050 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001051 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001052 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001053 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001054 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001055 case ARMISD::VREV64: return "ARMISD::VREV64";
1056 case ARMISD::VREV32: return "ARMISD::VREV32";
1057 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001058 case ARMISD::VZIP: return "ARMISD::VZIP";
1059 case ARMISD::VUZP: return "ARMISD::VUZP";
1060 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001061 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1062 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001063 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1064 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001065 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1066 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001067 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilsonc6c13a32010-02-18 06:05:53 +00001068 case ARMISD::FMAX: return "ARMISD::FMAX";
1069 case ARMISD::FMIN: return "ARMISD::FMIN";
Joey Goulye3dd6842013-08-23 12:01:13 +00001070 case ARMISD::VMAXNM: return "ARMISD::VMAX";
1071 case ARMISD::VMINNM: return "ARMISD::VMIN";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001072 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001073 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1074 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001075 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilson2d790df2010-11-28 06:51:26 +00001076 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1077 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1078 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001079 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1080 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1081 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1082 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1083 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1084 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1085 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1086 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1087 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1088 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1089 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1090 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1091 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1092 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1093 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1094 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1095 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001096 }
1097}
1098
Matt Arsenault758659232013-05-18 00:21:46 +00001099EVT ARMTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Duncan Sandsf2641e12011-09-06 19:07:46 +00001100 if (!VT.isVector()) return getPointerTy();
1101 return VT.changeVectorElementTypeToInteger();
1102}
1103
Evan Cheng4cad68e2010-05-15 02:18:07 +00001104/// getRegClassFor - Return the register class that should be used for the
1105/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001106const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001107 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1108 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1109 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001110 if (Subtarget->hasNEON()) {
1111 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001112 return &ARM::QQPRRegClass;
1113 if (VT == MVT::v8i64)
1114 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001115 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001116 return TargetLowering::getRegClassFor(VT);
1117}
1118
Eric Christopher84bdfd82010-07-21 22:26:11 +00001119// Create a fast isel object.
1120FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001121ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1122 const TargetLibraryInfo *libInfo) const {
1123 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001124}
1125
Anton Korobeynikov19edda02010-07-24 21:52:08 +00001126/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1127/// be used for loads / stores from the global.
1128unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1129 return (Subtarget->isThumb1Only() ? 127 : 4095);
1130}
1131
Evan Cheng4401f882010-05-20 23:26:43 +00001132Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001133 unsigned NumVals = N->getNumValues();
1134 if (!NumVals)
1135 return Sched::RegPressure;
1136
1137 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001138 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001139 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001140 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001141 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001142 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001143 }
Evan Chengbf914992010-05-28 23:25:23 +00001144
1145 if (!N->isMachineOpcode())
1146 return Sched::RegPressure;
1147
1148 // Load are scheduled for latency even if there instruction itinerary
1149 // is not available.
Eric Christopherd9134482014-08-04 21:25:23 +00001150 const TargetInstrInfo *TII =
1151 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001152 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001153
Evan Cheng6cc775f2011-06-28 19:10:37 +00001154 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001155 return Sched::RegPressure;
1156 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001157 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001158 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001159
Evan Cheng4401f882010-05-20 23:26:43 +00001160 return Sched::RegPressure;
1161}
1162
Evan Cheng10043e22007-01-19 07:51:42 +00001163//===----------------------------------------------------------------------===//
1164// Lowering Code
1165//===----------------------------------------------------------------------===//
1166
Evan Cheng10043e22007-01-19 07:51:42 +00001167/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1168static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1169 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001170 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001171 case ISD::SETNE: return ARMCC::NE;
1172 case ISD::SETEQ: return ARMCC::EQ;
1173 case ISD::SETGT: return ARMCC::GT;
1174 case ISD::SETGE: return ARMCC::GE;
1175 case ISD::SETLT: return ARMCC::LT;
1176 case ISD::SETLE: return ARMCC::LE;
1177 case ISD::SETUGT: return ARMCC::HI;
1178 case ISD::SETUGE: return ARMCC::HS;
1179 case ISD::SETULT: return ARMCC::LO;
1180 case ISD::SETULE: return ARMCC::LS;
1181 }
1182}
1183
Bob Wilsona2e83332009-09-09 23:14:54 +00001184/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1185static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001186 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001187 CondCode2 = ARMCC::AL;
1188 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001189 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001190 case ISD::SETEQ:
1191 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1192 case ISD::SETGT:
1193 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1194 case ISD::SETGE:
1195 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1196 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001197 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001198 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1199 case ISD::SETO: CondCode = ARMCC::VC; break;
1200 case ISD::SETUO: CondCode = ARMCC::VS; break;
1201 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1202 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1203 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1204 case ISD::SETLT:
1205 case ISD::SETULT: CondCode = ARMCC::LT; break;
1206 case ISD::SETLE:
1207 case ISD::SETULE: CondCode = ARMCC::LE; break;
1208 case ISD::SETNE:
1209 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1210 }
Evan Cheng10043e22007-01-19 07:51:42 +00001211}
1212
Bob Wilsona4c22902009-04-17 19:07:39 +00001213//===----------------------------------------------------------------------===//
1214// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001215//===----------------------------------------------------------------------===//
1216
1217#include "ARMGenCallingConv.inc"
1218
Oliver Stannardc24f2172014-05-09 14:01:47 +00001219/// getEffectiveCallingConv - Get the effective calling convention, taking into
1220/// account presence of floating point hardware and calling convention
1221/// limitations, such as support for variadic functions.
1222CallingConv::ID
1223ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1224 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001225 switch (CC) {
1226 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001227 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001228 case CallingConv::ARM_AAPCS:
1229 case CallingConv::ARM_APCS:
1230 case CallingConv::GHC:
1231 return CC;
1232 case CallingConv::ARM_AAPCS_VFP:
1233 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1234 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001235 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001236 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001237 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001238 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1239 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001240 return CallingConv::ARM_AAPCS_VFP;
1241 else
1242 return CallingConv::ARM_AAPCS;
1243 case CallingConv::Fast:
1244 if (!Subtarget->isAAPCS_ABI()) {
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001245 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001246 return CallingConv::Fast;
1247 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001248 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001249 return CallingConv::ARM_AAPCS_VFP;
1250 else
1251 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001252 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001253}
1254
1255/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1256/// CallingConvention.
1257CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1258 bool Return,
1259 bool isVarArg) const {
1260 switch (getEffectiveCallingConv(CC, isVarArg)) {
1261 default:
1262 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001263 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001264 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001265 case CallingConv::ARM_AAPCS:
1266 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1267 case CallingConv::ARM_AAPCS_VFP:
1268 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1269 case CallingConv::Fast:
1270 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001271 case CallingConv::GHC:
1272 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001273 }
1274}
1275
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001276/// LowerCallResult - Lower the result values of a call into the
1277/// appropriate copies out of appropriate physical registers.
1278SDValue
1279ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001280 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001281 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001282 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001283 SmallVectorImpl<SDValue> &InVals,
1284 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001285
Bob Wilsona4c22902009-04-17 19:07:39 +00001286 // Assign locations to each value returned by this call.
1287 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001288 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1289 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001290 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001291 CCAssignFnForNode(CallConv, /* Return*/ true,
1292 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001293
1294 // Copy all of the result registers out of their specified physreg.
1295 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1296 CCValAssign VA = RVLocs[i];
1297
Stephen Linb8bd2322013-04-20 05:14:40 +00001298 // Pass 'this' value directly from the argument to return value, to avoid
1299 // reg unit interference
1300 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001301 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1302 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001303 InVals.push_back(ThisVal);
1304 continue;
1305 }
1306
Bob Wilson0041bd32009-04-25 00:33:20 +00001307 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001308 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001309 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001310 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001311 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001312 Chain = Lo.getValue(1);
1313 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001314 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001315 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001316 InFlag);
1317 Chain = Hi.getValue(1);
1318 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001319 if (!Subtarget->isLittle())
1320 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001321 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001322
Owen Anderson9f944592009-08-11 20:47:22 +00001323 if (VA.getLocVT() == MVT::v2f64) {
1324 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1325 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1326 DAG.getConstant(0, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001327
1328 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001329 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001330 Chain = Lo.getValue(1);
1331 InFlag = Lo.getValue(2);
1332 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001333 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001334 Chain = Hi.getValue(1);
1335 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001336 if (!Subtarget->isLittle())
1337 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001338 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001339 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1340 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001341 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001342 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001343 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1344 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001345 Chain = Val.getValue(1);
1346 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001347 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001348
1349 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001350 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001351 case CCValAssign::Full: break;
1352 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001353 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001354 break;
1355 }
1356
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001357 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001358 }
1359
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001360 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001361}
1362
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001363/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001364SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001365ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1366 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001367 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001368 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001369 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001370 unsigned LocMemOffset = VA.getLocMemOffset();
1371 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1372 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilsona4c22902009-04-17 19:07:39 +00001373 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner886250c2010-09-21 18:51:21 +00001374 MachinePointerInfo::getStack(LocMemOffset),
David Greene0d0149f2010-02-15 16:55:24 +00001375 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001376}
1377
Andrew Trickef9de2a2013-05-25 02:42:55 +00001378void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001379 SDValue Chain, SDValue &Arg,
1380 RegsToPassVector &RegsToPass,
1381 CCValAssign &VA, CCValAssign &NextVA,
1382 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001383 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001384 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001385
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001386 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001387 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001388 unsigned id = Subtarget->isLittle() ? 0 : 1;
1389 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001390
1391 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001392 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001393 else {
1394 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001395 if (!StackPtr.getNode())
Bob Wilson2e076c42009-06-22 23:27:02 +00001396 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1397
Christian Pirkerb5728192014-05-08 14:06:24 +00001398 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001399 dl, DAG, NextVA,
1400 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001401 }
1402}
1403
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001404/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001405/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1406/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001407SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001408ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001409 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001410 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001411 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001412 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1413 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1414 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001415 SDValue Chain = CLI.Chain;
1416 SDValue Callee = CLI.Callee;
1417 bool &isTailCall = CLI.IsTailCall;
1418 CallingConv::ID CallConv = CLI.CallConv;
1419 bool doesNotRet = CLI.DoesNotReturn;
1420 bool isVarArg = CLI.IsVarArg;
1421
Dale Johannesend679ff72010-06-03 21:09:53 +00001422 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001423 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1424 bool isThisReturn = false;
1425 bool isSibCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001426
Bob Wilson8decdc42011-10-07 17:17:49 +00001427 // Disable tail calls if they're not supported.
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001428 if (!Subtarget->supportsTailCall() || MF.getTarget().Options.DisableTailCalls)
Bob Wilson3c9ed762010-08-13 22:43:33 +00001429 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001430
Dale Johannesend679ff72010-06-03 21:09:53 +00001431 if (isTailCall) {
1432 // Check if it's really possible to do a tail call.
1433 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001434 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001435 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001436 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1437 report_fatal_error("failed to perform tail call elimination on a call "
1438 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001439 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1440 // detected sibcalls.
1441 if (isTailCall) {
1442 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001443 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001444 }
1445 }
Evan Cheng10043e22007-01-19 07:51:42 +00001446
Bob Wilsona4c22902009-04-17 19:07:39 +00001447 // Analyze operands of the call, assigning locations to each operand.
1448 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001449 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1450 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001451 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001452 CCAssignFnForNode(CallConv, /* Return*/ false,
1453 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001454
Bob Wilsona4c22902009-04-17 19:07:39 +00001455 // Get a count of how many bytes are to be pushed on the stack.
1456 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001457
Dale Johannesend679ff72010-06-03 21:09:53 +00001458 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001459 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001460 NumBytes = 0;
1461
Evan Cheng10043e22007-01-19 07:51:42 +00001462 // Adjust the stack pointer for the new arguments...
1463 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001464 if (!isSibCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00001465 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
1466 dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001467
Jim Grosbach6ad4bcb2010-02-24 01:43:03 +00001468 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Cheng10043e22007-01-19 07:51:42 +00001469
Bob Wilson2e076c42009-06-22 23:27:02 +00001470 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001471 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001472
Bob Wilsona4c22902009-04-17 19:07:39 +00001473 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001474 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001475 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1476 i != e;
1477 ++i, ++realArgIdx) {
1478 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001479 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001480 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001481 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001482
Bob Wilsona4c22902009-04-17 19:07:39 +00001483 // Promote the value if needed.
1484 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001485 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001486 case CCValAssign::Full: break;
1487 case CCValAssign::SExt:
1488 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1489 break;
1490 case CCValAssign::ZExt:
1491 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1492 break;
1493 case CCValAssign::AExt:
1494 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1495 break;
1496 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001497 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001498 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001499 }
1500
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001501 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001502 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001503 if (VA.getLocVT() == MVT::v2f64) {
1504 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1505 DAG.getConstant(0, MVT::i32));
1506 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1507 DAG.getConstant(1, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001508
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001509 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001510 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1511
1512 VA = ArgLocs[++i]; // skip ahead to next loc
1513 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001514 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001515 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1516 } else {
1517 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001518
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001519 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1520 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001521 }
1522 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001523 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001524 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001525 }
1526 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001527 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1528 assert(VA.getLocVT() == MVT::i32 &&
1529 "unexpected calling convention register assignment");
1530 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001531 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001532 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001533 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001534 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001535 } else if (isByVal) {
1536 assert(VA.isMemLoc());
1537 unsigned offset = 0;
1538
1539 // True if this byval aggregate will be split between registers
1540 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001541 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
1542 unsigned CurByValIdx = CCInfo.getInRegsParamsProceed();
1543
1544 if (CurByValIdx < ByValArgsCount) {
1545
1546 unsigned RegBegin, RegEnd;
1547 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1548
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001549 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1550 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001551 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001552 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1553 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1554 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1555 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001556 false, false, false,
1557 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001558 MemOpChains.push_back(Load.getValue(1));
1559 RegsToPass.push_back(std::make_pair(j, Load));
1560 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001561
1562 // If parameter size outsides register area, "offset" value
1563 // helps us to calculate stack slot for remained part properly.
1564 offset = RegEnd - RegBegin;
1565
1566 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001567 }
1568
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001569 if (Flags.getByValSize() > 4*offset) {
Manman Ren9f911162012-06-01 02:44:42 +00001570 unsigned LocMemOffset = VA.getLocMemOffset();
1571 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1572 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1573 StkPtrOff);
1574 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1575 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1576 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1577 MVT::i32);
Manman Rene8735522012-06-01 19:33:18 +00001578 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001579
Manman Ren9f911162012-06-01 02:44:42 +00001580 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001581 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001582 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001583 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001584 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001585 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001586 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001587
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001588 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1589 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001590 }
Evan Cheng10043e22007-01-19 07:51:42 +00001591 }
1592
1593 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001594 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001595
1596 // Build a sequence of copy-to-reg nodes chained together with token chain
1597 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001598 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001599 // Tail call byval lowering might overwrite argument registers so in case of
1600 // tail call optimization the copies to registers are lowered later.
1601 if (!isTailCall)
1602 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1603 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1604 RegsToPass[i].second, InFlag);
1605 InFlag = Chain.getValue(1);
1606 }
Evan Cheng10043e22007-01-19 07:51:42 +00001607
Dale Johannesend679ff72010-06-03 21:09:53 +00001608 // For tail calls lower the arguments to the 'real' stack slot.
1609 if (isTailCall) {
1610 // Force all the incoming stack arguments to be loaded from the stack
1611 // before any new outgoing arguments are stored to the stack, because the
1612 // outgoing stack slots may alias the incoming argument stack slots, and
1613 // the alias isn't otherwise explicit. This is slightly more conservative
1614 // than necessary, because it means that each store effectively depends
1615 // on every argument instead of just those arguments it would clobber.
1616
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001617 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001618 InFlag = SDValue();
1619 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1620 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1621 RegsToPass[i].second, InFlag);
1622 InFlag = Chain.getValue(1);
1623 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001624 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001625 }
1626
Bill Wendling24c79f22008-09-16 21:48:12 +00001627 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1628 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1629 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001630 bool isDirect = false;
1631 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001632 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001633 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001634
1635 if (EnableARMLongCalls) {
Saleem Abdulrasool90386ad2014-06-07 20:29:27 +00001636 assert((Subtarget->isTargetWindows() ||
1637 getTargetMachine().getRelocationModel() == Reloc::Static) &&
1638 "long-calls with non-static relocation model!");
Jim Grosbach32bb3622010-04-14 22:28:31 +00001639 // Handle a global address or an external symbol. If it's not one of
1640 // those, the target's already in a register, so we don't need to do
1641 // anything extra.
1642 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001643 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001644 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001645 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001646 ARMConstantPoolValue *CPV =
1647 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1648
Jim Grosbach32bb3622010-04-14 22:28:31 +00001649 // Get the address of the callee into a register
1650 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1651 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1652 Callee = DAG.getLoad(getPointerTy(), dl,
1653 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001654 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001655 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001656 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1657 const char *Sym = S->getSymbol();
1658
1659 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001660 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001661 ARMConstantPoolValue *CPV =
1662 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1663 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001664 // Get the address of the callee into a register
1665 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1666 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1667 Callee = DAG.getLoad(getPointerTy(), dl,
1668 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001669 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001670 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001671 }
1672 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001673 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001674 isDirect = true;
Chris Lattner55452c22009-07-15 04:12:33 +00001675 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Tim Northoverd6a729b2014-01-06 14:28:05 +00001676 bool isStub = (isExt && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001677 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001678 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Chengc3c949b42007-06-19 21:05:09 +00001679 // ARM call to a local ARM function is predicable.
Evan Chengf128bdc2010-06-16 07:35:02 +00001680 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001681 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001682 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001683 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Tim Northover72360d22013-12-02 10:35:41 +00001684 Callee = DAG.getNode(ARMISD::WrapperPIC, dl, getPointerTy(),
Tim Northoverd4d294d2014-08-06 11:13:06 +00001685 DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
1686 0, ARMII::MO_NONLAZY));
1687 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
1688 MachinePointerInfo::getGOT(), false, false, true, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001689 } else if (Subtarget->isTargetCOFF()) {
1690 assert(Subtarget->isTargetWindows() &&
1691 "Windows is the only supported COFF target");
1692 unsigned TargetFlags = GV->hasDLLImportStorageClass()
1693 ? ARMII::MO_DLLIMPORT
1694 : ARMII::MO_NO_FLAG;
1695 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), /*Offset=*/0,
1696 TargetFlags);
1697 if (GV->hasDLLImportStorageClass())
1698 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
1699 DAG.getNode(ARMISD::Wrapper, dl, getPointerTy(),
1700 Callee), MachinePointerInfo::getGOT(),
1701 false, false, false, 0);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001702 } else {
1703 // On ELF targets for PIC code, direct calls should go through the PLT
1704 unsigned OpFlags = 0;
1705 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001706 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001707 OpFlags = ARMII::MO_PLT;
1708 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1709 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001710 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001711 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001712 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001713 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001714 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Cheng83f35172007-01-30 20:37:08 +00001715 // tBX takes a register source operand.
1716 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001717 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001718 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001719 ARMConstantPoolValue *CPV =
1720 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1721 ARMPCLabelIndex, 4);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001722 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001723 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen021052a2009-02-04 20:06:27 +00001724 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Chengcdbb70c2009-10-31 03:39:36 +00001725 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001726 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001727 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00001728 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson7117a912009-03-20 22:42:55 +00001729 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen021052a2009-02-04 20:06:27 +00001730 getPointerTy(), Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001731 } else {
1732 unsigned OpFlags = 0;
1733 // On ELF targets for PIC code, direct calls should go through the PLT
1734 if (Subtarget->isTargetELF() &&
1735 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1736 OpFlags = ARMII::MO_PLT;
1737 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1738 }
Evan Cheng10043e22007-01-19 07:51:42 +00001739 }
1740
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001741 // FIXME: handle tail calls differently.
1742 unsigned CallOpc;
Eric Christopherc1058df2014-07-04 01:55:26 +00001743 bool HasMinSizeAttr = MF.getFunction()->getAttributes().hasAttribute(
1744 AttributeSet::FunctionIndex, Attribute::MinSize);
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001745 if (Subtarget->isThumb()) {
1746 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001747 CallOpc = ARMISD::CALL_NOLINK;
1748 else
1749 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1750 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001751 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001752 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001753 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet8e1fe842012-11-02 21:32:17 +00001754 // Emit regular call when code size is the priority
1755 !HasMinSizeAttr)
Evan Cheng65f9d192012-02-28 18:51:51 +00001756 // "mov lr, pc; b _foo" to avoid confusing the RSP
1757 CallOpc = ARMISD::CALL_NOLINK;
1758 else
1759 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001760 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001761
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001762 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001763 Ops.push_back(Chain);
1764 Ops.push_back(Callee);
1765
1766 // Add argument registers to the end of the list so that they are known live
1767 // into the call.
1768 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1769 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1770 RegsToPass[i].second.getValueType()));
1771
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001772 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001773 if (!isTailCall) {
1774 const uint32_t *Mask;
Eric Christopherd9134482014-08-04 21:25:23 +00001775 const TargetRegisterInfo *TRI =
1776 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
Matthias Braunc22630e2013-10-04 16:52:54 +00001777 const ARMBaseRegisterInfo *ARI = static_cast<const ARMBaseRegisterInfo*>(TRI);
1778 if (isThisReturn) {
1779 // For 'this' returns, use the R0-preserving mask if applicable
1780 Mask = ARI->getThisReturnPreservedMask(CallConv);
1781 if (!Mask) {
1782 // Set isThisReturn to false if the calling convention is not one that
1783 // allows 'returned' to be modeled in this way, so LowerCallResult does
1784 // not try to pass 'this' straight through
1785 isThisReturn = false;
1786 Mask = ARI->getCallPreservedMask(CallConv);
1787 }
1788 } else
Stephen Linff7fcee2013-06-26 21:42:14 +00001789 Mask = ARI->getCallPreservedMask(CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001790
Matthias Braunc22630e2013-10-04 16:52:54 +00001791 assert(Mask && "Missing call preserved mask for calling convention");
1792 Ops.push_back(DAG.getRegisterMask(Mask));
1793 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001794
Gabor Greiff304a7a2008-08-28 21:40:38 +00001795 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001796 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001797
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001798 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001799 if (isTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001800 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Dale Johannesend679ff72010-06-03 21:09:53 +00001801
Duncan Sands739a0542008-07-02 17:40:58 +00001802 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001803 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001804 InFlag = Chain.getValue(1);
1805
Chris Lattner27539552008-10-11 22:08:30 +00001806 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001807 DAG.getIntPtrConstant(0, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001808 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001809 InFlag = Chain.getValue(1);
1810
Bob Wilsona4c22902009-04-17 19:07:39 +00001811 // Handle result values, copying them out of physregs into vregs that we
1812 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001813 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001814 InVals, isThisReturn,
1815 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001816}
1817
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001818/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001819/// on the stack. Remember the next parameter register to allocate,
1820/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001821/// this.
1822void
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001823ARMTargetLowering::HandleByVal(
1824 CCState *State, unsigned &size, unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001825 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1826 assert((State->getCallOrPrologue() == Prologue ||
1827 State->getCallOrPrologue() == Call) &&
1828 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001829
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001830 if ((ARM::R0 <= reg) && (reg <= ARM::R3)) {
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001831 if (Subtarget->isAAPCS_ABI() && Align > 4) {
1832 unsigned AlignInRegs = Align / 4;
1833 unsigned Waste = (ARM::R4 - reg) % AlignInRegs;
1834 for (unsigned i = 0; i < Waste; ++i)
1835 reg = State->AllocateReg(GPRArgRegs, 4);
1836 }
1837 if (reg != 0) {
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001838 unsigned excess = 4 * (ARM::R4 - reg);
1839
1840 // Special case when NSAA != SP and parameter size greater than size of
1841 // all remained GPR regs. In that case we can't split parameter, we must
1842 // send it to stack. We also must set NCRN to R4, so waste all
1843 // remained registers.
Oliver Stannardd55e1152014-03-05 15:25:27 +00001844 const unsigned NSAAOffset = State->getNextStackOffset();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001845 if (Subtarget->isAAPCS_ABI() && NSAAOffset != 0 && size > excess) {
1846 while (State->AllocateReg(GPRArgRegs, 4))
1847 ;
1848 return;
1849 }
1850
1851 // First register for byval parameter is the first register that wasn't
1852 // allocated before this method call, so it would be "reg".
1853 // If parameter is small enough to be saved in range [reg, r4), then
1854 // the end (first after last) register would be reg + param-size-in-regs,
1855 // else parameter would be splitted between registers and stack,
1856 // end register would be r4 in this case.
1857 unsigned ByValRegBegin = reg;
Stepan Dyatkovskiy2703bca2013-05-08 14:51:27 +00001858 unsigned ByValRegEnd = (size < excess) ? reg + size/4 : (unsigned)ARM::R4;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001859 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
1860 // Note, first register is allocated in the beginning of function already,
1861 // allocate remained amount of registers we need.
1862 for (unsigned i = reg+1; i != ByValRegEnd; ++i)
1863 State->AllocateReg(GPRArgRegs, 4);
Oliver Stannardd55e1152014-03-05 15:25:27 +00001864 // A byval parameter that is split between registers and memory needs its
1865 // size truncated here.
1866 // In the case where the entire structure fits in registers, we set the
1867 // size in memory to zero.
1868 if (size < excess)
1869 size = 0;
1870 else
1871 size -= excess;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001872 }
1873 }
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001874}
1875
Dale Johannesend679ff72010-06-03 21:09:53 +00001876/// MatchingStackOffset - Return true if the given stack call argument is
1877/// already available in the same position (relatively) of the caller's
1878/// incoming argument stack.
1879static
1880bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1881 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00001882 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00001883 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1884 int FI = INT_MAX;
1885 if (Arg.getOpcode() == ISD::CopyFromReg) {
1886 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001887 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00001888 return false;
1889 MachineInstr *Def = MRI->getVRegDef(VR);
1890 if (!Def)
1891 return false;
1892 if (!Flags.isByVal()) {
1893 if (!TII->isLoadFromStackSlot(Def, FI))
1894 return false;
1895 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00001896 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00001897 }
1898 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1899 if (Flags.isByVal())
1900 // ByVal argument is passed in as a pointer but it's now being
1901 // dereferenced. e.g.
1902 // define @foo(%struct.X* %A) {
1903 // tail call @bar(%struct.X* byval %A)
1904 // }
1905 return false;
1906 SDValue Ptr = Ld->getBasePtr();
1907 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1908 if (!FINode)
1909 return false;
1910 FI = FINode->getIndex();
1911 } else
1912 return false;
1913
1914 assert(FI != INT_MAX);
1915 if (!MFI->isFixedObjectIndex(FI))
1916 return false;
1917 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1918}
1919
1920/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1921/// for tail call optimization. Targets which want to do tail call
1922/// optimization should implement this function.
1923bool
1924ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1925 CallingConv::ID CalleeCC,
1926 bool isVarArg,
1927 bool isCalleeStructRet,
1928 bool isCallerStructRet,
1929 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001930 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00001931 const SmallVectorImpl<ISD::InputArg> &Ins,
1932 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00001933 const Function *CallerF = DAG.getMachineFunction().getFunction();
1934 CallingConv::ID CallerCC = CallerF->getCallingConv();
1935 bool CCMatch = CallerCC == CalleeCC;
1936
1937 // Look for obvious safe cases to perform tail call optimization that do not
1938 // require ABI changes. This is what gcc calls sibcall.
1939
Jim Grosbache3864cc2010-06-16 23:45:49 +00001940 // Do not sibcall optimize vararg calls unless the call site is not passing
1941 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00001942 if (isVarArg && !Outs.empty())
1943 return false;
1944
Tim Northoverd8407452013-10-01 14:33:28 +00001945 // Exception-handling functions need a special set of instructions to indicate
1946 // a return to the hardware. Tail-calling another function would probably
1947 // break this.
1948 if (CallerF->hasFnAttribute("interrupt"))
1949 return false;
1950
Dale Johannesend679ff72010-06-03 21:09:53 +00001951 // Also avoid sibcall optimization if either caller or callee uses struct
1952 // return semantics.
1953 if (isCalleeStructRet || isCallerStructRet)
1954 return false;
1955
Dale Johannesend24c66b2010-06-23 18:52:34 +00001956 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach3840c902011-07-08 20:18:11 +00001957 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1958 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1959 // support in the assembler and linker to be used. This would need to be
1960 // fixed to fully support tail calls in Thumb1.
1961 //
Dale Johannesene2289282010-07-08 01:18:23 +00001962 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1963 // LR. This means if we need to reload LR, it takes an extra instructions,
1964 // which outweighs the value of the tail call; but here we don't know yet
1965 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach535d3b42010-09-08 03:54:02 +00001966 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesene2289282010-07-08 01:18:23 +00001967 // emitEpilogue if LR is used.
Dale Johannesene2289282010-07-08 01:18:23 +00001968
1969 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1970 // but we need to make sure there are enough registers; the only valid
1971 // registers are the 4 used for parameters. We don't currently do this
1972 // case.
Evan Chengd4b08732010-11-30 23:55:39 +00001973 if (Subtarget->isThumb1Only())
1974 return false;
Dale Johannesen3ac52b32010-06-18 18:13:11 +00001975
Dale Johannesend679ff72010-06-03 21:09:53 +00001976 // If the calling conventions do not match, then we'd better make sure the
1977 // results are returned in the same way as what the caller expects.
1978 if (!CCMatch) {
1979 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopherb5217502014-08-06 18:45:26 +00001980 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1,
1981 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00001982 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1983
1984 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopherb5217502014-08-06 18:45:26 +00001985 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2,
1986 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00001987 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1988
1989 if (RVLocs1.size() != RVLocs2.size())
1990 return false;
1991 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1992 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1993 return false;
1994 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1995 return false;
1996 if (RVLocs1[i].isRegLoc()) {
1997 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1998 return false;
1999 } else {
2000 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2001 return false;
2002 }
2003 }
2004 }
2005
Manman Ren7e48b252012-10-12 23:39:43 +00002006 // If Caller's vararg or byval argument has been split between registers and
2007 // stack, do not perform tail call, since part of the argument is in caller's
2008 // local frame.
2009 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
2010 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002011 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00002012 return false;
2013
Dale Johannesend679ff72010-06-03 21:09:53 +00002014 // If the callee takes no arguments then go on to check the results of the
2015 // call.
2016 if (!Outs.empty()) {
2017 // Check if stack adjustment is needed. For now, do not do this if any
2018 // argument is passed on the stack.
2019 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002020 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
2021 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002022 CCInfo.AnalyzeCallOperands(Outs,
2023 CCAssignFnForNode(CalleeCC, false, isVarArg));
2024 if (CCInfo.getNextStackOffset()) {
2025 MachineFunction &MF = DAG.getMachineFunction();
2026
2027 // Check if the arguments are already laid out in the right way as
2028 // the caller's fixed stack objects.
2029 MachineFrameInfo *MFI = MF.getFrameInfo();
2030 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Eric Christopherd9134482014-08-04 21:25:23 +00002031 const TargetInstrInfo *TII =
2032 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002033 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2034 i != e;
2035 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002036 CCValAssign &VA = ArgLocs[i];
2037 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002038 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002039 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00002040 if (VA.getLocInfo() == CCValAssign::Indirect)
2041 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002042 if (VA.needsCustom()) {
2043 // f64 and vector types are split into multiple registers or
2044 // register/stack-slot combinations. The types will not match
2045 // the registers; give up on memory f64 refs until we figure
2046 // out what to do about this.
2047 if (!VA.isRegLoc())
2048 return false;
2049 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002050 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002051 if (RegVT == MVT::v2f64) {
2052 if (!ArgLocs[++i].isRegLoc())
2053 return false;
2054 if (!ArgLocs[++i].isRegLoc())
2055 return false;
2056 }
2057 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002058 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2059 MFI, MRI, TII))
2060 return false;
2061 }
2062 }
2063 }
2064 }
2065
2066 return true;
2067}
2068
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002069bool
2070ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2071 MachineFunction &MF, bool isVarArg,
2072 const SmallVectorImpl<ISD::OutputArg> &Outs,
2073 LLVMContext &Context) const {
2074 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002075 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002076 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2077 isVarArg));
2078}
2079
Tim Northoverd8407452013-10-01 14:33:28 +00002080static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2081 SDLoc DL, SelectionDAG &DAG) {
2082 const MachineFunction &MF = DAG.getMachineFunction();
2083 const Function *F = MF.getFunction();
2084
2085 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2086
2087 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2088 // version of the "preferred return address". These offsets affect the return
2089 // instruction if this is a return from PL1 without hypervisor extensions.
2090 // IRQ/FIQ: +4 "subs pc, lr, #4"
2091 // SWI: 0 "subs pc, lr, #0"
2092 // ABORT: +4 "subs pc, lr, #4"
2093 // UNDEF: +4/+2 "subs pc, lr, #0"
2094 // UNDEF varies depending on where the exception came from ARM or Thumb
2095 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2096
2097 int64_t LROffset;
2098 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2099 IntKind == "ABORT")
2100 LROffset = 4;
2101 else if (IntKind == "SWI" || IntKind == "UNDEF")
2102 LROffset = 0;
2103 else
2104 report_fatal_error("Unsupported interrupt attribute. If present, value "
2105 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2106
2107 RetOps.insert(RetOps.begin() + 1, DAG.getConstant(LROffset, MVT::i32, false));
2108
Craig Topper48d114b2014-04-26 18:35:24 +00002109 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002110}
2111
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002112SDValue
2113ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002114 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002115 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002116 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002117 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002118
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002119 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002120 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002121
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002122 // CCState - Info about the registers and stack slots.
Eric Christopherb5217502014-08-06 18:45:26 +00002123 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2124 *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002125
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002126 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002127 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2128 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002129
Bob Wilsona4c22902009-04-17 19:07:39 +00002130 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002131 SmallVector<SDValue, 4> RetOps;
2132 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002133 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002134
Jonathan Roelofsef84bda2014-08-05 21:32:21 +00002135 MachineFunction &MF = DAG.getMachineFunction();
2136 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2137 AFI->setReturnRegsCount(RVLocs.size());
2138
Bob Wilsona4c22902009-04-17 19:07:39 +00002139 // Copy the result values into the output registers.
2140 for (unsigned i = 0, realRVLocIdx = 0;
2141 i != RVLocs.size();
2142 ++i, ++realRVLocIdx) {
2143 CCValAssign &VA = RVLocs[i];
2144 assert(VA.isRegLoc() && "Can only return in registers!");
2145
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002146 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002147
2148 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002149 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002150 case CCValAssign::Full: break;
2151 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002152 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002153 break;
2154 }
2155
Bob Wilsona4c22902009-04-17 19:07:39 +00002156 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002157 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002158 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002159 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2160 DAG.getConstant(0, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002161 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002162 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002163
Christian Pirkerb5728192014-05-08 14:06:24 +00002164 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2165 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2166 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002167 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002168 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002169 VA = RVLocs[++i]; // skip ahead to next loc
2170 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002171 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2172 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002173 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002174 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002175 VA = RVLocs[++i]; // skip ahead to next loc
2176
2177 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002178 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2179 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002180 }
2181 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2182 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002183 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002184 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002185 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2186 fmrrd.getValue(isLittleEndian ? 0 : 1),
2187 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002188 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002189 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002190 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002191 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2192 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002193 Flag);
2194 } else
2195 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2196
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002197 // Guarantee that all emitted copies are
2198 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002199 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002200 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002201 }
2202
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002203 // Update chain and glue.
2204 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002205 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002206 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002207
Tim Northoverd8407452013-10-01 14:33:28 +00002208 // CPUs which aren't M-class use a special sequence to return from
2209 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2210 // though we use "subs pc, lr, #N").
2211 //
2212 // M-class CPUs actually use a normal return sequence with a special
2213 // (hardware-provided) value in LR, so the normal code path works.
2214 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2215 !Subtarget->isMClass()) {
2216 if (Subtarget->isThumb1Only())
2217 report_fatal_error("interrupt attribute is not supported in Thumb1");
2218 return LowerInterruptReturn(RetOps, dl, DAG);
2219 }
2220
Craig Topper48d114b2014-04-26 18:35:24 +00002221 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002222}
2223
Evan Chengf8bad082012-04-10 01:51:00 +00002224bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002225 if (N->getNumValues() != 1)
2226 return false;
2227 if (!N->hasNUsesOfValue(1, 0))
2228 return false;
2229
Evan Chengf8bad082012-04-10 01:51:00 +00002230 SDValue TCChain = Chain;
2231 SDNode *Copy = *N->use_begin();
2232 if (Copy->getOpcode() == ISD::CopyToReg) {
2233 // If the copy has a glue operand, we conservatively assume it isn't safe to
2234 // perform a tail call.
2235 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2236 return false;
2237 TCChain = Copy->getOperand(0);
2238 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2239 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002240 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002241 SmallPtrSet<SDNode*, 2> Copies;
2242 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002243 UI != UE; ++UI) {
2244 if (UI->getOpcode() != ISD::CopyToReg)
2245 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002246 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002247 }
Evan Chengf8bad082012-04-10 01:51:00 +00002248 if (Copies.size() > 2)
2249 return false;
2250
2251 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2252 UI != UE; ++UI) {
2253 SDValue UseChain = UI->getOperand(0);
2254 if (Copies.count(UseChain.getNode()))
2255 // Second CopyToReg
2256 Copy = *UI;
2257 else
2258 // First CopyToReg
2259 TCChain = UseChain;
2260 }
2261 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002262 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002263 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002264 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002265 Copy = *Copy->use_begin();
2266 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002267 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002268 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002269 } else {
2270 return false;
2271 }
2272
Evan Cheng419ea282010-12-01 22:59:46 +00002273 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002274 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2275 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002276 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2277 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002278 return false;
2279 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002280 }
2281
Evan Chengf8bad082012-04-10 01:51:00 +00002282 if (!HasRet)
2283 return false;
2284
2285 Chain = TCChain;
2286 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002287}
2288
Evan Cheng0663f232011-03-21 01:19:09 +00002289bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002290 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002291 return false;
2292
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00002293 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng0663f232011-03-21 01:19:09 +00002294 return false;
2295
2296 return !Subtarget->isThumb1Only();
2297}
2298
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002299// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2300// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2301// one of the above mentioned nodes. It has to be wrapped because otherwise
2302// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2303// be used to form addressing mode. These wrapped nodes will be selected
2304// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002305static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002306 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002307 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002308 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002309 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002310 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002311 if (CP->isMachineConstantPoolEntry())
2312 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2313 CP->getAlignment());
2314 else
2315 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2316 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002317 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002318}
2319
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002320unsigned ARMTargetLowering::getJumpTableEncoding() const {
2321 return MachineJumpTableInfo::EK_Inline;
2322}
2323
Dan Gohman21cea8a2010-04-17 15:26:15 +00002324SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2325 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002326 MachineFunction &MF = DAG.getMachineFunction();
2327 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2328 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002329 SDLoc DL(Op);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002330 EVT PtrVT = getPointerTy();
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002331 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002332 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2333 SDValue CPAddr;
2334 if (RelocM == Reloc::Static) {
2335 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2336 } else {
2337 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002338 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002339 ARMConstantPoolValue *CPV =
2340 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2341 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002342 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2343 }
2344 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2345 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002346 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002347 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002348 if (RelocM == Reloc::Static)
2349 return Result;
Evan Cheng408aa562009-11-06 22:24:13 +00002350 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002351 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002352}
2353
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002354// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002355SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002356ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002357 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002358 SDLoc dl(GA);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002359 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002360 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002361 MachineFunction &MF = DAG.getMachineFunction();
2362 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002363 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002364 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002365 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2366 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002367 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002368 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002369 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattner7727d052010-09-21 06:44:06 +00002370 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002371 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002372 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002373
Evan Cheng408aa562009-11-06 22:24:13 +00002374 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002375 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002376
2377 // call __tls_get_addr.
2378 ArgListTy Args;
2379 ArgListEntry Entry;
2380 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002381 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002382 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002383
Dale Johannesen555a3752009-01-30 23:10:59 +00002384 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002385 TargetLowering::CallLoweringInfo CLI(DAG);
2386 CLI.setDebugLoc(dl).setChain(Chain)
2387 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002388 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args),
2389 0);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002390
Justin Holewinskiaa583972012-05-25 16:35:28 +00002391 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002392 return CallResult.first;
2393}
2394
2395// Lower ISD::GlobalTLSAddress using the "initial exec" or
2396// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002397SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002398ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002399 SelectionDAG &DAG,
2400 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002401 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002402 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002403 SDValue Offset;
2404 SDValue Chain = DAG.getEntryNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002405 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002406 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002407 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002408
Hans Wennborgaea41202012-05-04 09:40:39 +00002409 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002410 MachineFunction &MF = DAG.getMachineFunction();
2411 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002412 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002413 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002414 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2415 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002416 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2417 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2418 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002419 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002420 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002421 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002422 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002423 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002424 Chain = Offset.getValue(1);
2425
Evan Cheng408aa562009-11-06 22:24:13 +00002426 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002427 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002428
Evan Chengcdbb70c2009-10-31 03:39:36 +00002429 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002430 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002431 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002432 } else {
2433 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002434 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002435 ARMConstantPoolValue *CPV =
2436 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002437 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002438 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002439 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002440 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002441 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002442 }
2443
2444 // The address of the thread local variable is the add of the thread
2445 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002446 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002447}
2448
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002449SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002450ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002451 // TODO: implement the "local dynamic" model
2452 assert(Subtarget->isTargetELF() &&
2453 "TLS not implemented for non-ELF targets");
2454 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgaea41202012-05-04 09:40:39 +00002455
2456 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2457
2458 switch (model) {
2459 case TLSModel::GeneralDynamic:
2460 case TLSModel::LocalDynamic:
2461 return LowerToTLSGeneralDynamicModel(GA, DAG);
2462 case TLSModel::InitialExec:
2463 case TLSModel::LocalExec:
2464 return LowerToTLSExecModels(GA, DAG, model);
2465 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002466 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002467}
2468
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002469SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002470 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002471 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002472 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002473 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002474 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Rafael Espindola6de96a12009-01-15 20:18:42 +00002475 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002476 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002477 ARMConstantPoolConstant::Create(GV,
2478 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002479 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002480 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson7117a912009-03-20 22:42:55 +00002481 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002482 CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002483 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002484 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002485 SDValue Chain = Result.getValue(1);
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002486 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00002487 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002488 if (!UseGOTOFF)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002489 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002490 MachinePointerInfo::getGOT(),
2491 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002492 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002493 }
2494
2495 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002496 // pair. This is always cheaper.
Eric Christopherc1058df2014-07-04 01:55:26 +00002497 if (Subtarget->useMovt(DAG.getMachineFunction())) {
Evan Cheng68aec142011-01-19 02:16:49 +00002498 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002499 // FIXME: Once remat is capable of dealing with instructions with register
2500 // operands, expand this into two nodes.
2501 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2502 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002503 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002504 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2505 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2506 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2507 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002508 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002509 }
2510}
2511
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002512SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002513 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002514 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002515 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002516 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002517 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002518
Eric Christopherc1058df2014-07-04 01:55:26 +00002519 if (Subtarget->useMovt(DAG.getMachineFunction()))
Evan Cheng68aec142011-01-19 02:16:49 +00002520 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002521
Tim Northover72360d22013-12-02 10:35:41 +00002522 // FIXME: Once remat is capable of dealing with instructions with register
2523 // operands, expand this into multiple nodes
2524 unsigned Wrapper =
2525 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002526
Tim Northover72360d22013-12-02 10:35:41 +00002527 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2528 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002529
Evan Cheng1b389522009-09-03 07:04:02 +00002530 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002531 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2532 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002533 return Result;
2534}
2535
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002536SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2537 SelectionDAG &DAG) const {
2538 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
Eric Christopherc1058df2014-07-04 01:55:26 +00002539 assert(Subtarget->useMovt(DAG.getMachineFunction()) &&
2540 "Windows on ARM expects to use movw/movt");
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002541
2542 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002543 const ARMII::TOF TargetFlags =
2544 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG);
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002545 EVT PtrVT = getPointerTy();
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002546 SDValue Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002547 SDLoc DL(Op);
2548
2549 ++NumMovwMovt;
2550
2551 // FIXME: Once remat is capable of dealing with instructions with register
2552 // operands, expand this into two nodes.
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002553 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2554 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0,
2555 TargetFlags));
2556 if (GV->hasDLLImportStorageClass())
2557 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
2558 MachinePointerInfo::getGOT(), false, false, false, 0);
2559 return Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002560}
2561
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002562SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002563 SelectionDAG &DAG) const {
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002564 assert(Subtarget->isTargetELF() &&
2565 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Cheng408aa562009-11-06 22:24:13 +00002566 MachineFunction &MF = DAG.getMachineFunction();
2567 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002568 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002569 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002570 SDLoc dl(Op);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002571 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingc214cb02011-10-01 08:58:29 +00002572 ARMConstantPoolValue *CPV =
2573 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2574 ARMPCLabelIndex, PCAdj);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002575 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002576 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002577 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002578 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002579 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00002580 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002581 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002582}
2583
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002584SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002585ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002586 SDLoc dl(Op);
Jim Grosbachfaa3abb2010-05-27 23:49:24 +00002587 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002588 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2589 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002590 Op.getOperand(1), Val);
2591}
2592
2593SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002594ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002595 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002596 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2597 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2598}
2599
2600SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002601ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002602 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002603 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002604 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002605 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002606 default: return SDValue(); // Don't custom lower most intrinsics.
Jim Grosbach07393ba2014-06-16 21:55:30 +00002607 case Intrinsic::arm_rbit: {
2608 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
2609 "RBIT intrinsic must have i32 type!");
2610 return DAG.getNode(ARMISD::RBIT, dl, MVT::i32, Op.getOperand(0));
2611 }
Bob Wilson17f88782009-08-04 00:25:01 +00002612 case Intrinsic::arm_thread_pointer: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002613 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson17f88782009-08-04 00:25:01 +00002614 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2615 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002616 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002617 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002618 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002619 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002620 EVT PtrVT = getPointerTy();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002621 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2622 SDValue CPAddr;
2623 unsigned PCAdj = (RelocM != Reloc::PIC_)
2624 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002625 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002626 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2627 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002628 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002629 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002630 SDValue Result =
Evan Chengcdbb70c2009-10-31 03:39:36 +00002631 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002632 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002633 false, false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002634
2635 if (RelocM == Reloc::PIC_) {
Evan Cheng408aa562009-11-06 22:24:13 +00002636 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002637 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2638 }
2639 return Result;
2640 }
Evan Cheng18381b42011-03-29 23:06:19 +00002641 case Intrinsic::arm_neon_vmulls:
2642 case Intrinsic::arm_neon_vmullu: {
2643 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2644 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002645 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002646 Op.getOperand(1), Op.getOperand(2));
2647 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002648 }
2649}
2650
Eli Friedman30a49e92011-08-03 21:06:02 +00002651static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2652 const ARMSubtarget *Subtarget) {
2653 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002654 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00002655 if (!Subtarget->hasDataBarrier()) {
2656 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2657 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2658 // here.
2659 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00002660 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00002661 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00002662 DAG.getConstant(0, MVT::i32));
2663 }
2664
Tim Northover36b24172013-07-03 09:20:36 +00002665 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
2666 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
2667 unsigned Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00002668 if (Subtarget->isMClass()) {
2669 // Only a full system barrier exists in the M-class architectures.
2670 Domain = ARM_MB::SY;
2671 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00002672 // Swift happens to implement ISHST barriers in a way that's compatible with
2673 // Release semantics but weaker than ISH so we'd be fools not to use
2674 // it. Beware: other processors probably don't!
2675 Domain = ARM_MB::ISHST;
2676 }
2677
Joey Gouly926d3f52013-09-05 15:35:24 +00002678 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
2679 DAG.getConstant(Intrinsic::arm_dmb, MVT::i32),
Tim Northover36b24172013-07-03 09:20:36 +00002680 DAG.getConstant(Domain, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002681}
2682
Evan Cheng8740ee32010-11-03 06:34:55 +00002683static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2684 const ARMSubtarget *Subtarget) {
2685 // ARM pre v5TE and Thumb1 does not have preload instructions.
2686 if (!(Subtarget->isThumb2() ||
2687 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2688 // Just preserve the chain.
2689 return Op.getOperand(0);
2690
Andrew Trickef9de2a2013-05-25 02:42:55 +00002691 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00002692 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2693 if (!isRead &&
2694 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2695 // ARMv7 with MP extension has PLDW.
2696 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00002697
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002698 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2699 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00002700 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00002701 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002702 isData = ~isData & 1;
2703 }
Evan Cheng8740ee32010-11-03 06:34:55 +00002704
2705 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng21acf9f2010-11-04 05:19:35 +00002706 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2707 DAG.getConstant(isData, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00002708}
2709
Dan Gohman31ae5862010-04-17 14:41:14 +00002710static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2711 MachineFunction &MF = DAG.getMachineFunction();
2712 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2713
Evan Cheng10043e22007-01-19 07:51:42 +00002714 // vastart just stores the address of the VarArgsFrameIndex slot into the
2715 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002716 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002717 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00002718 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002719 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00002720 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2721 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002722}
2723
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002724SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00002725ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2726 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002727 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00002728 MachineFunction &MF = DAG.getMachineFunction();
2729 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2730
Craig Topper760b1342012-02-22 05:59:10 +00002731 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00002732 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002733 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002734 else
Craig Topperc7242e02012-04-20 07:30:17 +00002735 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002736
2737 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002738 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002739 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002740
2741 SDValue ArgValue2;
2742 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002743 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00002744 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00002745
2746 // Create load node to retrieve arguments from the stack.
2747 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Chengcdbb70c2009-10-31 03:39:36 +00002748 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002749 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002750 false, false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00002751 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00002752 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002753 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002754 }
Christian Pirkerb5728192014-05-08 14:06:24 +00002755 if (!Subtarget->isLittle())
2756 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002757 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00002758}
2759
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002760void
2761ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002762 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002763 unsigned ArgSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002764 unsigned &ArgRegsSize,
2765 unsigned &ArgRegsSaveSize)
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002766 const {
2767 unsigned NumGPRs;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002768 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2769 unsigned RBegin, REnd;
2770 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2771 NumGPRs = REnd - RBegin;
2772 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002773 unsigned int firstUnalloced;
2774 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2775 sizeof(GPRArgRegs) /
2776 sizeof(GPRArgRegs[0]));
2777 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2778 }
2779
Eric Christopherd9134482014-08-04 21:25:23 +00002780 unsigned Align = MF.getTarget()
2781 .getSubtargetImpl()
2782 ->getFrameLowering()
2783 ->getStackAlignment();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002784 ArgRegsSize = NumGPRs * 4;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002785
2786 // If parameter is split between stack and GPRs...
Mark Seabornbe266aa2014-02-16 18:59:48 +00002787 if (NumGPRs && Align > 4 &&
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002788 (ArgRegsSize < ArgSize ||
2789 InRegsParamRecordIdx >= CCInfo.getInRegsParamsCount())) {
Mark Seabornbe266aa2014-02-16 18:59:48 +00002790 // Add padding for part of param recovered from GPRs. For example,
2791 // if Align == 8, its last byte must be at address K*8 - 1.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002792 // We need to do it, since remained (stack) part of parameter has
2793 // stack alignment, and we need to "attach" "GPRs head" without gaps
2794 // to it:
2795 // Stack:
2796 // |---- 8 bytes block ----| |---- 8 bytes block ----| |---- 8 bytes...
2797 // [ [padding] [GPRs head] ] [ Tail passed via stack ....
2798 //
2799 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2800 unsigned Padding =
Mark Seabornbe266aa2014-02-16 18:59:48 +00002801 OffsetToAlignment(ArgRegsSize + AFI->getArgRegsSaveSize(), Align);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002802 ArgRegsSaveSize = ArgRegsSize + Padding;
2803 } else
2804 // We don't need to extend regs save size for byval parameters if they
2805 // are passed via GPRs only.
2806 ArgRegsSaveSize = ArgRegsSize;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002807}
2808
2809// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00002810// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002811// byval). Either way, we allocate stack slots adjacent to the data
2812// provided by our caller, and store the unallocated registers there.
2813// If this is a variadic function, the va_list pointer will begin with
2814// these values; otherwise, this reassembles a (byval) structure that
2815// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002816// Return: The frame index registers were stored into.
2817int
2818ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002819 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002820 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002821 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002822 unsigned OffsetFromOrigArg,
2823 unsigned ArgOffset,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002824 unsigned ArgSize,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002825 bool ForceMutable,
2826 unsigned ByValStoreOffset,
2827 unsigned TotalArgRegsSaveSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002828
2829 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00002830 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002831 // Setup first unallocated register as first byval register;
2832 // eat all remained registers
2833 // (these two actions are performed by HandleByVal method).
2834 // Then, here, we initialize stack frame with
2835 // "store-reg" instructions.
2836 // Case #2. Var-args function, that doesn't contain byval parameters.
2837 // The same: eat all remained unallocated registers,
2838 // initialize stack frame.
2839
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002840 MachineFunction &MF = DAG.getMachineFunction();
2841 MachineFrameInfo *MFI = MF.getFrameInfo();
2842 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002843 unsigned firstRegToSaveIndex, lastRegToSaveIndex;
2844 unsigned RBegin, REnd;
2845 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2846 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2847 firstRegToSaveIndex = RBegin - ARM::R0;
2848 lastRegToSaveIndex = REnd - ARM::R0;
2849 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002850 firstRegToSaveIndex = CCInfo.getFirstUnallocated
Craig Topper58713212013-07-15 04:27:47 +00002851 (GPRArgRegs, array_lengthof(GPRArgRegs));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002852 lastRegToSaveIndex = 4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002853 }
2854
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002855 unsigned ArgRegsSize, ArgRegsSaveSize;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002856 computeRegArea(CCInfo, MF, InRegsParamRecordIdx, ArgSize,
2857 ArgRegsSize, ArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002858
2859 // Store any by-val regs to their spots on the stack so that they may be
2860 // loaded by deferencing the result of formal parameter pointer or va_next.
2861 // Note: once stack area for byval/varargs registers
2862 // was initialized, it can't be initialized again.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002863 if (ArgRegsSaveSize) {
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002864 unsigned Padding = ArgRegsSaveSize - ArgRegsSize;
2865
2866 if (Padding) {
2867 assert(AFI->getStoredByValParamsPadding() == 0 &&
2868 "The only parameter may be padded.");
2869 AFI->setStoredByValParamsPadding(Padding);
2870 }
2871
Oliver Stannardd55e1152014-03-05 15:25:27 +00002872 int FrameIndex = MFI->CreateFixedObject(ArgRegsSaveSize,
2873 Padding +
2874 ByValStoreOffset -
2875 (int64_t)TotalArgRegsSaveSize,
2876 false);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002877 SDValue FIN = DAG.getFrameIndex(FrameIndex, getPointerTy());
Oliver Stannardd55e1152014-03-05 15:25:27 +00002878 if (Padding) {
2879 MFI->CreateFixedObject(Padding,
2880 ArgOffset + ByValStoreOffset -
2881 (int64_t)ArgRegsSaveSize,
2882 false);
2883 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002884
2885 SmallVector<SDValue, 4> MemOps;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002886 for (unsigned i = 0; firstRegToSaveIndex < lastRegToSaveIndex;
2887 ++firstRegToSaveIndex, ++i) {
Craig Topper760b1342012-02-22 05:59:10 +00002888 const TargetRegisterClass *RC;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002889 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002890 RC = &ARM::tGPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002891 else
Craig Topperc7242e02012-04-20 07:30:17 +00002892 RC = &ARM::GPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002893
2894 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2895 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2896 SDValue Store =
2897 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002898 MachinePointerInfo(OrigArg, OffsetFromOrigArg + 4*i),
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002899 false, false, 0);
2900 MemOps.push_back(Store);
2901 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2902 DAG.getConstant(4, getPointerTy()));
2903 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002904
2905 AFI->setArgRegsSaveSize(ArgRegsSaveSize + AFI->getArgRegsSaveSize());
2906
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002907 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002908 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002909 return FrameIndex;
Oliver Stannardd55e1152014-03-05 15:25:27 +00002910 } else {
2911 if (ArgSize == 0) {
2912 // We cannot allocate a zero-byte object for the first variadic argument,
2913 // so just make up a size.
2914 ArgSize = 4;
2915 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002916 // This will point to the next argument passed via stack.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002917 return MFI->CreateFixedObject(
Oliver Stannardd55e1152014-03-05 15:25:27 +00002918 ArgSize, ArgOffset, !ForceMutable);
2919 }
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002920}
2921
2922// Setup stack frame, the va_list pointer will start from.
2923void
2924ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002925 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002926 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002927 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002928 bool ForceMutable) const {
2929 MachineFunction &MF = DAG.getMachineFunction();
2930 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2931
2932 // Try to store any remaining integer argument regs
2933 // to their spots on the stack so that they may be loaded by deferencing
2934 // the result of va_next.
2935 // If there is no regs to be stored, just point address after last
2936 // argument passed via stack.
2937 int FrameIndex =
Craig Topper062a2ba2014-04-25 05:30:21 +00002938 StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
2939 CCInfo.getInRegsParamsCount(), 0, ArgOffset, 0, ForceMutable,
2940 0, TotalArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002941
2942 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002943}
2944
Bob Wilson2e076c42009-06-22 23:27:02 +00002945SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002946ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002947 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002948 const SmallVectorImpl<ISD::InputArg>
2949 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002950 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002951 SmallVectorImpl<SDValue> &InVals)
2952 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00002953 MachineFunction &MF = DAG.getMachineFunction();
2954 MachineFrameInfo *MFI = MF.getFrameInfo();
2955
Bob Wilsona4c22902009-04-17 19:07:39 +00002956 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2957
2958 // Assign locations to all of the incoming arguments.
2959 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002960 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
2961 *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002962 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002963 CCAssignFnForNode(CallConv, /* Return*/ false,
2964 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00002965
Bob Wilsona4c22902009-04-17 19:07:39 +00002966 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002967 int lastInsIndex = -1;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002968 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002969 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
2970 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002971
2972 // Initially ArgRegsSaveSize is zero.
2973 // Then we increase this value each time we meet byval parameter.
2974 // We also increase this value in case of varargs function.
2975 AFI->setArgRegsSaveSize(0);
2976
Oliver Stannardd55e1152014-03-05 15:25:27 +00002977 unsigned ByValStoreOffset = 0;
2978 unsigned TotalArgRegsSaveSize = 0;
2979 unsigned ArgRegsSaveSizeMaxAlign = 4;
2980
2981 // Calculate the amount of stack space that we need to allocate to store
2982 // byval and variadic arguments that are passed in registers.
2983 // We need to know this before we allocate the first byval or variadic
2984 // argument, as they will be allocated a stack slot below the CFA (Canonical
2985 // Frame Address, the stack pointer at entry to the function).
2986 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2987 CCValAssign &VA = ArgLocs[i];
2988 if (VA.isMemLoc()) {
2989 int index = VA.getValNo();
2990 if (index != lastInsIndex) {
2991 ISD::ArgFlagsTy Flags = Ins[index].Flags;
2992 if (Flags.isByVal()) {
2993 unsigned ExtraArgRegsSize;
2994 unsigned ExtraArgRegsSaveSize;
2995 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsProceed(),
2996 Flags.getByValSize(),
2997 ExtraArgRegsSize, ExtraArgRegsSaveSize);
2998
2999 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
3000 if (Flags.getByValAlign() > ArgRegsSaveSizeMaxAlign)
3001 ArgRegsSaveSizeMaxAlign = Flags.getByValAlign();
3002 CCInfo.nextInRegsParam();
3003 }
3004 lastInsIndex = index;
3005 }
3006 }
3007 }
3008 CCInfo.rewindByValRegsInfo();
3009 lastInsIndex = -1;
3010 if (isVarArg) {
3011 unsigned ExtraArgRegsSize;
3012 unsigned ExtraArgRegsSaveSize;
3013 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsCount(), 0,
3014 ExtraArgRegsSize, ExtraArgRegsSaveSize);
3015 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
3016 }
3017 // If the arg regs save area contains N-byte aligned values, the
3018 // bottom of it must be at least N-byte aligned.
3019 TotalArgRegsSaveSize = RoundUpToAlignment(TotalArgRegsSaveSize, ArgRegsSaveSizeMaxAlign);
3020 TotalArgRegsSaveSize = std::min(TotalArgRegsSaveSize, 16U);
3021
Bob Wilsona4c22902009-04-17 19:07:39 +00003022 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3023 CCValAssign &VA = ArgLocs[i];
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00003024 std::advance(CurOrigArg, Ins[VA.getValNo()].OrigArgIndex - CurArgIdx);
3025 CurArgIdx = Ins[VA.getValNo()].OrigArgIndex;
Bob Wilsonea09d4a2009-04-17 20:35:10 +00003026 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00003027 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003028 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00003029
Bob Wilsona4c22902009-04-17 19:07:39 +00003030 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003031 // f64 and vector types are split up into multiple registers or
3032 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00003033 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003034 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003035 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00003036 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00003037 SDValue ArgValue2;
3038 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00003039 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson699bdf72010-04-13 22:03:22 +00003040 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3041 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003042 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003043 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00003044 } else {
3045 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3046 Chain, DAG, dl);
3047 }
Owen Anderson9f944592009-08-11 20:47:22 +00003048 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3049 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00003050 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson9f944592009-08-11 20:47:22 +00003051 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00003052 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
3053 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003054 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00003055
Bob Wilson2e076c42009-06-22 23:27:02 +00003056 } else {
Craig Topper760b1342012-02-22 05:59:10 +00003057 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003058
Owen Anderson9f944592009-08-11 20:47:22 +00003059 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003060 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003061 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003062 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003063 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003064 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003065 else if (RegVT == MVT::i32)
Craig Topperc7242e02012-04-20 07:30:17 +00003066 RC = AFI->isThumb1OnlyFunction() ?
3067 (const TargetRegisterClass*)&ARM::tGPRRegClass :
3068 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003069 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003070 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003071
3072 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003073 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003074 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003075 }
3076
3077 // If this is an 8 or 16-bit value, it is really passed promoted
3078 // to 32 bits. Insert an assert[sz]ext to capture this, then
3079 // truncate to the right size.
3080 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003081 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003082 case CCValAssign::Full: break;
3083 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003084 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003085 break;
3086 case CCValAssign::SExt:
3087 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3088 DAG.getValueType(VA.getValVT()));
3089 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3090 break;
3091 case CCValAssign::ZExt:
3092 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3093 DAG.getValueType(VA.getValVT()));
3094 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3095 break;
3096 }
3097
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003098 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003099
3100 } else { // VA.isRegLoc()
3101
3102 // sanity check
3103 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003104 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003105
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003106 int index = ArgLocs[i].getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003107
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003108 // Some Ins[] entries become multiple ArgLoc[] entries.
3109 // Process them only once.
3110 if (index != lastInsIndex)
3111 {
3112 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003113 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003114 // This can be changed with more analysis.
3115 // In case of tail call optimization mark all arguments mutable.
3116 // Since they could be overwritten by lowering of arguments in case of
3117 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003118 if (Flags.isByVal()) {
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003119 unsigned CurByValIndex = CCInfo.getInRegsParamsProceed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003120
3121 ByValStoreOffset = RoundUpToAlignment(ByValStoreOffset, Flags.getByValAlign());
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003122 int FrameIndex = StoreByValRegs(
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003123 CCInfo, DAG, dl, Chain, CurOrigArg,
3124 CurByValIndex,
3125 Ins[VA.getValNo()].PartOffset,
3126 VA.getLocMemOffset(),
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003127 Flags.getByValSize(),
Oliver Stannardd55e1152014-03-05 15:25:27 +00003128 true /*force mutable frames*/,
3129 ByValStoreOffset,
3130 TotalArgRegsSaveSize);
3131 ByValStoreOffset += Flags.getByValSize();
3132 ByValStoreOffset = std::min(ByValStoreOffset, 16U);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003133 InVals.push_back(DAG.getFrameIndex(FrameIndex, getPointerTy()));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003134 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003135 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003136 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003137 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003138 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003139
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003140 // Create load nodes to retrieve arguments from the stack.
3141 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3142 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
3143 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003144 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003145 }
3146 lastInsIndex = index;
3147 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003148 }
3149 }
3150
3151 // varargs
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003152 if (isVarArg)
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003153 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003154 CCInfo.getNextStackOffset(),
3155 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003156
Oliver Stannardb14c6252014-04-02 16:10:33 +00003157 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3158
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003159 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003160}
3161
3162/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003163static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003164 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003165 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003166 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003167 // Maybe this has already been legalized into the constant pool?
3168 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003169 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003170 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003171 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003172 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003173 }
3174 }
3175 return false;
3176}
3177
Evan Cheng10043e22007-01-19 07:51:42 +00003178/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3179/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003180SDValue
3181ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003182 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003183 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003184 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003185 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003186 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003187 // Constant does not fit, try adjusting it by one?
3188 switch (CC) {
3189 default: break;
3190 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003191 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003192 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003193 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003194 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003195 }
3196 break;
3197 case ISD::SETULT:
3198 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003199 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003200 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003201 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003202 }
3203 break;
3204 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003205 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003206 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003207 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003208 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003209 }
3210 break;
3211 case ISD::SETULE:
3212 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003213 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003214 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003215 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003216 }
3217 break;
3218 }
3219 }
3220 }
3221
3222 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003223 ARMISD::NodeType CompareType;
3224 switch (CondCode) {
3225 default:
3226 CompareType = ARMISD::CMP;
3227 break;
3228 case ARMCC::EQ:
3229 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003230 // Uses only Z Flag
3231 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003232 break;
3233 }
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003234 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003235 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003236}
3237
3238/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003239SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003240ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003241 SDLoc dl) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003242 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003243 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003244 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003245 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003246 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3247 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003248}
3249
Bob Wilson45acbd02011-03-08 01:17:20 +00003250/// duplicateCmp - Glue values can have only one use, so this function
3251/// duplicates a comparison node.
3252SDValue
3253ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3254 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003255 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003256 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3257 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3258
3259 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3260 Cmp = Cmp.getOperand(0);
3261 Opc = Cmp.getOpcode();
3262 if (Opc == ARMISD::CMPFP)
3263 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3264 else {
3265 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3266 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3267 }
3268 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3269}
3270
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003271std::pair<SDValue, SDValue>
3272ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3273 SDValue &ARMcc) const {
3274 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3275
3276 SDValue Value, OverflowCmp;
3277 SDValue LHS = Op.getOperand(0);
3278 SDValue RHS = Op.getOperand(1);
3279
3280
3281 // FIXME: We are currently always generating CMPs because we don't support
3282 // generating CMN through the backend. This is not as good as the natural
3283 // CMP case because it causes a register dependency and cannot be folded
3284 // later.
3285
3286 switch (Op.getOpcode()) {
3287 default:
3288 llvm_unreachable("Unknown overflow instruction!");
3289 case ISD::SADDO:
3290 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32);
3291 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS);
3292 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS);
3293 break;
3294 case ISD::UADDO:
3295 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32);
3296 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS);
3297 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS);
3298 break;
3299 case ISD::SSUBO:
3300 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32);
3301 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS);
3302 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS);
3303 break;
3304 case ISD::USUBO:
3305 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32);
3306 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS);
3307 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS);
3308 break;
3309 } // switch (...)
3310
3311 return std::make_pair(Value, OverflowCmp);
3312}
3313
3314
3315SDValue
3316ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3317 // Let legalize expand this if it isn't a legal type yet.
3318 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3319 return SDValue();
3320
3321 SDValue Value, OverflowCmp;
3322 SDValue ARMcc;
3323 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3324 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3325 // We use 0 and 1 as false and true values.
3326 SDValue TVal = DAG.getConstant(1, MVT::i32);
3327 SDValue FVal = DAG.getConstant(0, MVT::i32);
3328 EVT VT = Op.getValueType();
3329
3330 SDValue Overflow = DAG.getNode(ARMISD::CMOV, SDLoc(Op), VT, TVal, FVal,
3331 ARMcc, CCR, OverflowCmp);
3332
3333 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
3334 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), VTs, Value, Overflow);
3335}
3336
3337
Bill Wendling6a981312010-08-11 08:43:16 +00003338SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3339 SDValue Cond = Op.getOperand(0);
3340 SDValue SelectTrue = Op.getOperand(1);
3341 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003342 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003343 unsigned Opc = Cond.getOpcode();
3344
3345 if (Cond.getResNo() == 1 &&
3346 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3347 Opc == ISD::USUBO)) {
3348 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3349 return SDValue();
3350
3351 SDValue Value, OverflowCmp;
3352 SDValue ARMcc;
3353 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3354 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3355 EVT VT = Op.getValueType();
3356
3357 return DAG.getNode(ARMISD::CMOV, SDLoc(Op), VT, SelectTrue, SelectFalse,
3358 ARMcc, CCR, OverflowCmp);
3359
3360 }
Bill Wendling6a981312010-08-11 08:43:16 +00003361
3362 // Convert:
3363 //
3364 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3365 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3366 //
3367 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3368 const ConstantSDNode *CMOVTrue =
3369 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3370 const ConstantSDNode *CMOVFalse =
3371 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3372
3373 if (CMOVTrue && CMOVFalse) {
3374 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3375 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3376
3377 SDValue True;
3378 SDValue False;
3379 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3380 True = SelectTrue;
3381 False = SelectFalse;
3382 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3383 True = SelectFalse;
3384 False = SelectTrue;
3385 }
3386
3387 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003388 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003389 SDValue ARMcc = Cond.getOperand(2);
3390 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003391 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003392 assert(True.getValueType() == VT);
3393 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendling6a981312010-08-11 08:43:16 +00003394 }
3395 }
3396 }
3397
Dan Gohmand4a77c42012-02-24 00:09:36 +00003398 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3399 // undefined bits before doing a full-word comparison with zero.
3400 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
3401 DAG.getConstant(1, Cond.getValueType()));
3402
Bill Wendling6a981312010-08-11 08:43:16 +00003403 return DAG.getSelectCC(dl, Cond,
3404 DAG.getConstant(0, Cond.getValueType()),
3405 SelectTrue, SelectFalse, ISD::SETNE);
3406}
3407
Joey Gouly881eab52013-08-22 15:29:11 +00003408static ISD::CondCode getInverseCCForVSEL(ISD::CondCode CC) {
3409 if (CC == ISD::SETNE)
3410 return ISD::SETEQ;
Weiming Zhao63871d22013-12-18 22:25:17 +00003411 return ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003412}
3413
3414static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3415 bool &swpCmpOps, bool &swpVselOps) {
3416 // Start by selecting the GE condition code for opcodes that return true for
3417 // 'equality'
3418 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3419 CC == ISD::SETULE)
3420 CondCode = ARMCC::GE;
3421
3422 // and GT for opcodes that return false for 'equality'.
3423 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3424 CC == ISD::SETULT)
3425 CondCode = ARMCC::GT;
3426
3427 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3428 // to swap the compare operands.
3429 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3430 CC == ISD::SETULT)
3431 swpCmpOps = true;
3432
3433 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3434 // If we have an unordered opcode, we need to swap the operands to the VSEL
3435 // instruction (effectively negating the condition).
3436 //
3437 // This also has the effect of swapping which one of 'less' or 'greater'
3438 // returns true, so we also swap the compare operands. It also switches
3439 // whether we return true for 'equality', so we compensate by picking the
3440 // opposite condition code to our original choice.
3441 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3442 CC == ISD::SETUGT) {
3443 swpCmpOps = !swpCmpOps;
3444 swpVselOps = !swpVselOps;
3445 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3446 }
3447
3448 // 'ordered' is 'anything but unordered', so use the VS condition code and
3449 // swap the VSEL operands.
3450 if (CC == ISD::SETO) {
3451 CondCode = ARMCC::VS;
3452 swpVselOps = true;
3453 }
3454
3455 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3456 // code and swap the VSEL operands.
3457 if (CC == ISD::SETUNE) {
3458 CondCode = ARMCC::EQ;
3459 swpVselOps = true;
3460 }
3461}
3462
Dan Gohman21cea8a2010-04-17 15:26:15 +00003463SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003464 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003465 SDValue LHS = Op.getOperand(0);
3466 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003467 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003468 SDValue TrueVal = Op.getOperand(2);
3469 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003470 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003471
Owen Anderson9f944592009-08-11 20:47:22 +00003472 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003473 // Try to generate VSEL on ARMv8.
3474 // The VSEL instruction can't use all the usual ARM condition
3475 // codes: it only has two bits to select the condition code, so it's
3476 // constrained to use only GE, GT, VS and EQ.
3477 //
3478 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3479 // swap the operands of the previous compare instruction (effectively
3480 // inverting the compare condition, swapping 'less' and 'greater') and
3481 // sometimes need to swap the operands to the VSEL (which inverts the
3482 // condition in the sense of firing whenever the previous condition didn't)
Joey Goulyccd04892013-09-13 13:46:57 +00003483 if (getSubtarget()->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
Joey Gouly881eab52013-08-22 15:29:11 +00003484 TrueVal.getValueType() == MVT::f64)) {
3485 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3486 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3487 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
3488 CC = getInverseCCForVSEL(CC);
3489 std::swap(TrueVal, FalseVal);
3490 }
3491 }
3492
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003493 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003494 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003495 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Joey Gouly881eab52013-08-22 15:29:11 +00003496 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3497 Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003498 }
3499
3500 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003501 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003502
Joey Gouly881eab52013-08-22 15:29:11 +00003503 // Try to generate VSEL on ARMv8.
Joey Goulyccd04892013-09-13 13:46:57 +00003504 if (getSubtarget()->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
Joey Gouly881eab52013-08-22 15:29:11 +00003505 TrueVal.getValueType() == MVT::f64)) {
Joey Goulye3dd6842013-08-23 12:01:13 +00003506 // We can select VMAXNM/VMINNM from a compare followed by a select with the
3507 // same operands, as follows:
3508 // c = fcmp [ogt, olt, ugt, ult] a, b
3509 // select c, a, b
3510 // We only do this in unsafe-fp-math, because signed zeros and NaNs are
3511 // handled differently than the original code sequence.
3512 if (getTargetMachine().Options.UnsafeFPMath && LHS == TrueVal &&
3513 RHS == FalseVal) {
3514 if (CC == ISD::SETOGT || CC == ISD::SETUGT)
3515 return DAG.getNode(ARMISD::VMAXNM, dl, VT, TrueVal, FalseVal);
3516 if (CC == ISD::SETOLT || CC == ISD::SETULT)
3517 return DAG.getNode(ARMISD::VMINNM, dl, VT, TrueVal, FalseVal);
3518 }
3519
Joey Gouly881eab52013-08-22 15:29:11 +00003520 bool swpCmpOps = false;
3521 bool swpVselOps = false;
3522 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3523
3524 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3525 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3526 if (swpCmpOps)
3527 std::swap(LHS, RHS);
3528 if (swpVselOps)
3529 std::swap(TrueVal, FalseVal);
3530 }
3531 }
3532
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003533 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3534 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003535 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00003536 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003537 ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003538 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003539 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003540 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003541 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson7117a912009-03-20 22:42:55 +00003542 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003543 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Cheng10043e22007-01-19 07:51:42 +00003544 }
3545 return Result;
3546}
3547
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003548/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3549/// to morph to an integer compare sequence.
3550static bool canChangeToInt(SDValue Op, bool &SeenZero,
3551 const ARMSubtarget *Subtarget) {
3552 SDNode *N = Op.getNode();
3553 if (!N->hasOneUse())
3554 // Otherwise it requires moving the value from fp to integer registers.
3555 return false;
3556 if (!N->getNumValues())
3557 return false;
3558 EVT VT = Op.getValueType();
3559 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3560 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3561 // vmrs are very slow, e.g. cortex-a8.
3562 return false;
3563
3564 if (isFloatingPointZero(Op)) {
3565 SeenZero = true;
3566 return true;
3567 }
3568 return ISD::isNormalLoad(N);
3569}
3570
3571static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3572 if (isFloatingPointZero(Op))
3573 return DAG.getConstant(0, MVT::i32);
3574
3575 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003576 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003577 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003578 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003579 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003580
3581 llvm_unreachable("Unknown VFP cmp argument!");
3582}
3583
3584static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3585 SDValue &RetVal1, SDValue &RetVal2) {
3586 if (isFloatingPointZero(Op)) {
3587 RetVal1 = DAG.getConstant(0, MVT::i32);
3588 RetVal2 = DAG.getConstant(0, MVT::i32);
3589 return;
3590 }
3591
3592 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3593 SDValue Ptr = Ld->getBasePtr();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003594 RetVal1 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003595 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003596 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003597 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003598 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003599
3600 EVT PtrType = Ptr.getValueType();
3601 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003602 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003603 PtrType, Ptr, DAG.getConstant(4, PtrType));
Andrew Trickef9de2a2013-05-25 02:42:55 +00003604 RetVal2 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003605 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003606 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003607 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003608 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003609 return;
3610 }
3611
3612 llvm_unreachable("Unknown VFP cmp argument!");
3613}
3614
3615/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3616/// f32 and even f64 comparisons to integer ones.
3617SDValue
3618ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3619 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003620 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003621 SDValue LHS = Op.getOperand(2);
3622 SDValue RHS = Op.getOperand(3);
3623 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003624 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003625
Evan Chengd12af5d2012-03-01 23:27:13 +00003626 bool LHSSeenZero = false;
3627 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3628 bool RHSSeenZero = false;
3629 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3630 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003631 // If unsafe fp math optimization is enabled and there are no other uses of
3632 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003633 // to an integer comparison.
3634 if (CC == ISD::SETOEQ)
3635 CC = ISD::SETEQ;
3636 else if (CC == ISD::SETUNE)
3637 CC = ISD::SETNE;
3638
Evan Chengd12af5d2012-03-01 23:27:13 +00003639 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003640 SDValue ARMcc;
3641 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003642 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3643 bitcastf32Toi32(LHS, DAG), Mask);
3644 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3645 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003646 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3647 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3648 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3649 Chain, Dest, ARMcc, CCR, Cmp);
3650 }
3651
3652 SDValue LHS1, LHS2;
3653 SDValue RHS1, RHS2;
3654 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3655 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003656 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3657 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003658 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3659 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003660 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003661 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003662 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003663 }
3664
3665 return SDValue();
3666}
3667
3668SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3669 SDValue Chain = Op.getOperand(0);
3670 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3671 SDValue LHS = Op.getOperand(2);
3672 SDValue RHS = Op.getOperand(3);
3673 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003674 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003675
Owen Anderson9f944592009-08-11 20:47:22 +00003676 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003677 SDValue ARMcc;
3678 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003679 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003680 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003681 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003682 }
3683
Owen Anderson9f944592009-08-11 20:47:22 +00003684 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003685
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003686 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003687 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3688 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3689 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3690 if (Result.getNode())
3691 return Result;
3692 }
3693
Evan Cheng10043e22007-01-19 07:51:42 +00003694 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003695 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003696
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003697 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3698 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003699 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003700 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003701 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003702 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003703 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003704 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3705 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003706 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003707 }
3708 return Res;
3709}
3710
Dan Gohman21cea8a2010-04-17 15:26:15 +00003711SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003712 SDValue Chain = Op.getOperand(0);
3713 SDValue Table = Op.getOperand(1);
3714 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003715 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003716
Owen Anderson53aa7a92009-08-10 22:56:29 +00003717 EVT PTy = getPointerTy();
Evan Cheng10043e22007-01-19 07:51:42 +00003718 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3719 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3f17aee2009-07-14 18:44:34 +00003720 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003721 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson9f944592009-08-11 20:47:22 +00003722 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chengc8bed032009-07-28 20:53:24 +00003723 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3724 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003725 if (Subtarget->isThumb2()) {
3726 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3727 // which does another jump to the destination. This also makes it easier
3728 // to translate it to TBB / TBH later.
3729 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00003730 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Chengc6d70ae2009-07-29 02:18:14 +00003731 Addr, Op.getOperand(2), JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003732 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00003733 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003734 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattner7727d052010-09-21 06:44:06 +00003735 MachinePointerInfo::getJumpTable(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003736 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003737 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00003738 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson9f944592009-08-11 20:47:22 +00003739 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003740 } else {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003741 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003742 MachinePointerInfo::getJumpTable(),
3743 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003744 Chain = Addr.getValue(1);
Owen Anderson9f944592009-08-11 20:47:22 +00003745 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003746 }
Evan Cheng10043e22007-01-19 07:51:42 +00003747}
3748
Eli Friedman2d4055b2011-11-09 23:36:02 +00003749static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00003750 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003751 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003752
James Molloy547d4c02012-02-20 09:24:05 +00003753 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3754 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3755 return Op;
3756 return DAG.UnrollVectorOp(Op.getNode());
3757 }
3758
3759 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3760 "Invalid type for custom lowering!");
3761 if (VT != MVT::v4i16)
3762 return DAG.UnrollVectorOp(Op.getNode());
3763
3764 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3765 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003766}
3767
Bob Wilsone4191e72010-03-19 22:51:32 +00003768static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman2d4055b2011-11-09 23:36:02 +00003769 EVT VT = Op.getValueType();
3770 if (VT.isVector())
3771 return LowerVectorFP_TO_INT(Op, DAG);
3772
Andrew Trickef9de2a2013-05-25 02:42:55 +00003773 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003774 unsigned Opc;
3775
3776 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003777 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003778 case ISD::FP_TO_SINT:
3779 Opc = ARMISD::FTOSI;
3780 break;
3781 case ISD::FP_TO_UINT:
3782 Opc = ARMISD::FTOUI;
3783 break;
3784 }
3785 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peck527da1b2010-11-23 03:31:01 +00003786 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003787}
3788
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003789static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3790 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003791 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003792
Eli Friedman2d4055b2011-11-09 23:36:02 +00003793 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3794 if (VT.getVectorElementType() == MVT::f32)
3795 return Op;
3796 return DAG.UnrollVectorOp(Op.getNode());
3797 }
3798
Duncan Sandsa41634e2011-08-12 14:54:45 +00003799 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3800 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003801 if (VT != MVT::v4f32)
3802 return DAG.UnrollVectorOp(Op.getNode());
3803
3804 unsigned CastOpc;
3805 unsigned Opc;
3806 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003807 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003808 case ISD::SINT_TO_FP:
3809 CastOpc = ISD::SIGN_EXTEND;
3810 Opc = ISD::SINT_TO_FP;
3811 break;
3812 case ISD::UINT_TO_FP:
3813 CastOpc = ISD::ZERO_EXTEND;
3814 Opc = ISD::UINT_TO_FP;
3815 break;
3816 }
3817
3818 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3819 return DAG.getNode(Opc, dl, VT, Op);
3820}
3821
Bob Wilsone4191e72010-03-19 22:51:32 +00003822static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3823 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003824 if (VT.isVector())
3825 return LowerVectorINT_TO_FP(Op, DAG);
3826
Andrew Trickef9de2a2013-05-25 02:42:55 +00003827 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003828 unsigned Opc;
3829
3830 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003831 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003832 case ISD::SINT_TO_FP:
3833 Opc = ARMISD::SITOF;
3834 break;
3835 case ISD::UINT_TO_FP:
3836 Opc = ARMISD::UITOF;
3837 break;
3838 }
3839
Wesley Peck527da1b2010-11-23 03:31:01 +00003840 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilsone4191e72010-03-19 22:51:32 +00003841 return DAG.getNode(Opc, dl, VT, Op);
3842}
3843
Evan Cheng25f93642010-07-08 02:08:50 +00003844SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00003845 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003846 SDValue Tmp0 = Op.getOperand(0);
3847 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003848 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003849 EVT VT = Op.getValueType();
3850 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00003851 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3852 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3853 bool UseNEON = !InGPR && Subtarget->hasNEON();
3854
3855 if (UseNEON) {
3856 // Use VBSL to copy the sign bit.
3857 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3858 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3859 DAG.getTargetConstant(EncodedVal, MVT::i32));
3860 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3861 if (VT == MVT::f64)
3862 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3863 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3864 DAG.getConstant(32, MVT::i32));
3865 else /*if (VT == MVT::f32)*/
3866 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3867 if (SrcVT == MVT::f32) {
3868 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3869 if (VT == MVT::f64)
3870 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3871 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3872 DAG.getConstant(32, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00003873 } else if (VT == MVT::f32)
3874 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3875 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3876 DAG.getConstant(32, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00003877 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3878 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3879
3880 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3881 MVT::i32);
3882 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3883 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3884 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00003885
Evan Chengd6b641e2011-02-23 02:24:55 +00003886 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3887 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3888 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00003889 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00003890 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3891 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3892 DAG.getConstant(0, MVT::i32));
3893 } else {
3894 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3895 }
3896
3897 return Res;
3898 }
Evan Cheng2da1c952011-02-11 02:28:55 +00003899
3900 // Bitcast operand 1 to i32.
3901 if (SrcVT == MVT::f64)
3902 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00003903 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00003904 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3905
Evan Chengd6b641e2011-02-23 02:24:55 +00003906 // Or in the signbit with integer operations.
3907 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3908 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3909 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3910 if (VT == MVT::f32) {
3911 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3912 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3913 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3914 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00003915 }
3916
Evan Chengd6b641e2011-02-23 02:24:55 +00003917 // f64: Or the high part with signbit and then combine two parts.
3918 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00003919 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00003920 SDValue Lo = Tmp0.getValue(0);
3921 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3922 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3923 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00003924}
3925
Evan Cheng168ced92010-05-22 01:47:14 +00003926SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3927 MachineFunction &MF = DAG.getMachineFunction();
3928 MachineFrameInfo *MFI = MF.getFrameInfo();
3929 MFI->setReturnAddressIsTaken(true);
3930
Bill Wendling908bf812014-01-06 00:43:20 +00003931 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00003932 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00003933
Evan Cheng168ced92010-05-22 01:47:14 +00003934 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003935 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00003936 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3937 if (Depth) {
3938 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3939 SDValue Offset = DAG.getConstant(4, MVT::i32);
3940 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3941 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003942 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00003943 }
3944
3945 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00003946 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00003947 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3948}
3949
Dan Gohman21cea8a2010-04-17 15:26:15 +00003950SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00003951 const ARMBaseRegisterInfo &ARI =
3952 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
3953 MachineFunction &MF = DAG.getMachineFunction();
3954 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003955 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00003956
Owen Anderson53aa7a92009-08-10 22:56:29 +00003957 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003958 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003959 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00003960 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003961 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3962 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00003963 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3964 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003965 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003966 return FrameAddr;
3967}
3968
Renato Golinc7aea402014-05-06 16:51:25 +00003969// FIXME? Maybe this could be a TableGen attribute on some registers and
3970// this table could be generated automatically from RegInfo.
Hal Finkelf0e086a2014-05-11 19:29:07 +00003971unsigned ARMTargetLowering::getRegisterByName(const char* RegName,
3972 EVT VT) const {
Renato Golinc7aea402014-05-06 16:51:25 +00003973 unsigned Reg = StringSwitch<unsigned>(RegName)
3974 .Case("sp", ARM::SP)
3975 .Default(0);
3976 if (Reg)
3977 return Reg;
3978 report_fatal_error("Invalid register name global variable");
3979}
3980
Wesley Peck527da1b2010-11-23 03:31:01 +00003981/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00003982/// expand a bit convert where either the source or destination type is i64 to
3983/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3984/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3985/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00003986static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00003987 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003988 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003989 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00003990
Bob Wilson59b70ea2010-04-17 05:30:19 +00003991 // This function is only supposed to be called for i64 types, either as the
3992 // source or destination of the bit convert.
3993 EVT SrcVT = Op.getValueType();
3994 EVT DstVT = N->getValueType(0);
3995 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00003996 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00003997
Bob Wilson59b70ea2010-04-17 05:30:19 +00003998 // Turn i64->f64 into VMOVDRR.
3999 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson9f944592009-08-11 20:47:22 +00004000 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
4001 DAG.getConstant(0, MVT::i32));
4002 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
4003 DAG.getConstant(1, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00004004 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00004005 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00004006 }
Bob Wilson7117a912009-03-20 22:42:55 +00004007
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00004008 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00004009 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00004010 SDValue Cvt;
Christian Pirker6692e7c2014-05-14 16:59:44 +00004011 if (TLI.isBigEndian() && SrcVT.isVector() &&
4012 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00004013 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4014 DAG.getVTList(MVT::i32, MVT::i32),
4015 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
4016 else
4017 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4018 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00004019 // Merge the pieces into a single i64 value.
4020 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
4021 }
Bob Wilson7117a912009-03-20 22:42:55 +00004022
Bob Wilson59b70ea2010-04-17 05:30:19 +00004023 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00004024}
4025
Bob Wilson2e076c42009-06-22 23:27:02 +00004026/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00004027/// Zero vectors are used to represent vector negation and in those cases
4028/// will be implemented with the NEON VNEG instruction. However, VNEG does
4029/// not support i64 elements, so sometimes the zero vectors will need to be
4030/// explicitly constructed. Regardless, use a canonical VMOV to create the
4031/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004032static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004033 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00004034 // The canonical modified immediate encoding of a zero vector is....0!
4035 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
4036 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
4037 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00004038 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00004039}
4040
Jim Grosbach624fcb22009-10-31 21:00:56 +00004041/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
4042/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004043SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
4044 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00004045 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4046 EVT VT = Op.getValueType();
4047 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004048 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004049 SDValue ShOpLo = Op.getOperand(0);
4050 SDValue ShOpHi = Op.getOperand(1);
4051 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004052 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004053 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00004054
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004055 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
4056
Jim Grosbach624fcb22009-10-31 21:00:56 +00004057 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
4058 DAG.getConstant(VTBits, MVT::i32), ShAmt);
4059 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
4060 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
4061 DAG.getConstant(VTBits, MVT::i32));
4062 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
4063 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004064 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004065
4066 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4067 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004068 ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004069 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004070 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004071 CCR, Cmp);
4072
4073 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004074 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004075}
4076
Jim Grosbach5d994042009-10-31 19:38:01 +00004077/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4078/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004079SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4080 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004081 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4082 EVT VT = Op.getValueType();
4083 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004084 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004085 SDValue ShOpLo = Op.getOperand(0);
4086 SDValue ShOpHi = Op.getOperand(1);
4087 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004088 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004089
4090 assert(Op.getOpcode() == ISD::SHL_PARTS);
4091 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
4092 DAG.getConstant(VTBits, MVT::i32), ShAmt);
4093 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4094 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
4095 DAG.getConstant(VTBits, MVT::i32));
4096 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4097 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4098
4099 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4100 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4101 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004102 ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004103 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004104 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004105 CCR, Cmp);
4106
4107 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004108 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004109}
4110
Jim Grosbach535d3b42010-09-08 03:54:02 +00004111SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004112 SelectionDAG &DAG) const {
4113 // The rounding mode is in bits 23:22 of the FPSCR.
4114 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4115 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4116 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004117 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004118 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
4119 DAG.getConstant(Intrinsic::arm_get_fpscr,
4120 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004121 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemanb69b1822010-08-03 21:31:55 +00004122 DAG.getConstant(1U << 22, MVT::i32));
4123 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
4124 DAG.getConstant(22, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004125 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemanb69b1822010-08-03 21:31:55 +00004126 DAG.getConstant(3, MVT::i32));
4127}
4128
Jim Grosbach8546ec92010-01-18 19:58:49 +00004129static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4130 const ARMSubtarget *ST) {
4131 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004132 SDLoc dl(N);
Jim Grosbach8546ec92010-01-18 19:58:49 +00004133
4134 if (!ST->hasV6T2Ops())
4135 return SDValue();
4136
4137 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
4138 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4139}
4140
Evan Chengb4eae132012-12-04 22:41:50 +00004141/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4142/// for each 16-bit element from operand, repeated. The basic idea is to
4143/// leverage vcnt to get the 8-bit counts, gather and add the results.
4144///
4145/// Trace for v4i16:
4146/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4147/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4148/// 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 +00004149/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004150/// [b0 b1 b2 b3 b4 b5 b6 b7]
4151/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4152/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4153/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4154static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4155 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004156 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004157
4158 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4159 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4160 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4161 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4162 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4163 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4164}
4165
4166/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4167/// bit-count for each 16-bit element from the operand. We need slightly
4168/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4169/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004170///
Evan Chengb4eae132012-12-04 22:41:50 +00004171/// Trace for v4i16:
4172/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4173/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4174/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4175/// v4i16:Extracted = [k0 k1 k2 k3 ]
4176static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4177 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004178 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004179
4180 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4181 if (VT.is64BitVector()) {
4182 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4183 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
4184 DAG.getIntPtrConstant(0));
4185 } else {
4186 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
4187 BitCounts, DAG.getIntPtrConstant(0));
4188 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4189 }
4190}
4191
4192/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4193/// bit-count for each 32-bit element from the operand. The idea here is
4194/// to split the vector into 16-bit elements, leverage the 16-bit count
4195/// routine, and then combine the results.
4196///
4197/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4198/// input = [v0 v1 ] (vi: 32-bit elements)
4199/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4200/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004201/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004202/// [k0 k1 k2 k3 ]
4203/// N1 =+[k1 k0 k3 k2 ]
4204/// [k0 k2 k1 k3 ]
4205/// N2 =+[k1 k3 k0 k2 ]
4206/// [k0 k2 k1 k3 ]
4207/// Extended =+[k1 k3 k0 k2 ]
4208/// [k0 k2 ]
4209/// Extracted=+[k1 k3 ]
4210///
4211static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4212 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004213 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004214
4215 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4216
4217 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4218 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4219 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4220 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4221 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4222
4223 if (VT.is64BitVector()) {
4224 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4225 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
4226 DAG.getIntPtrConstant(0));
4227 } else {
4228 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
4229 DAG.getIntPtrConstant(0));
4230 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4231 }
4232}
4233
4234static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4235 const ARMSubtarget *ST) {
4236 EVT VT = N->getValueType(0);
4237
4238 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004239 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4240 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004241 "Unexpected type for custom ctpop lowering");
4242
4243 if (VT.getVectorElementType() == MVT::i32)
4244 return lowerCTPOP32BitElements(N, DAG);
4245 else
4246 return lowerCTPOP16BitElements(N, DAG);
4247}
4248
Bob Wilson2e076c42009-06-22 23:27:02 +00004249static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4250 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004251 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004252 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004253
Bob Wilson7d471332010-11-18 21:16:28 +00004254 if (!VT.isVector())
4255 return SDValue();
4256
Bob Wilson2e076c42009-06-22 23:27:02 +00004257 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004258 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004259
Bob Wilson7d471332010-11-18 21:16:28 +00004260 // Left shifts translate directly to the vshiftu intrinsic.
4261 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004262 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilson7d471332010-11-18 21:16:28 +00004263 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
4264 N->getOperand(0), N->getOperand(1));
4265
4266 assert((N->getOpcode() == ISD::SRA ||
4267 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4268
4269 // NEON uses the same intrinsics for both left and right shifts. For
4270 // right shifts, the shift amounts are negative, so negate the vector of
4271 // shift amounts.
4272 EVT ShiftVT = N->getOperand(1).getValueType();
4273 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4274 getZeroVector(ShiftVT, DAG, dl),
4275 N->getOperand(1));
4276 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4277 Intrinsic::arm_neon_vshifts :
4278 Intrinsic::arm_neon_vshiftu);
4279 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4280 DAG.getConstant(vshiftInt, MVT::i32),
4281 N->getOperand(0), NegatedCount);
4282}
4283
4284static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4285 const ARMSubtarget *ST) {
4286 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004287 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004288
Eli Friedman682d8c12009-08-22 03:13:10 +00004289 // We can get here for a node like i32 = ISD::SHL i32, i64
4290 if (VT != MVT::i64)
4291 return SDValue();
4292
4293 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004294 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004295
Chris Lattnerf81d5882007-11-24 07:07:01 +00004296 // We only lower SRA, SRL of 1 here, all others use generic lowering.
4297 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmaneffb8942008-09-12 16:56:44 +00004298 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands6ed40142008-12-01 11:39:25 +00004299 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004300
Chris Lattnerf81d5882007-11-24 07:07:01 +00004301 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004302 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004303
Chris Lattnerf81d5882007-11-24 07:07:01 +00004304 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004305 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004306 DAG.getConstant(0, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004307 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004308 DAG.getConstant(1, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004309
Chris Lattnerf81d5882007-11-24 07:07:01 +00004310 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4311 // captures the result into a carry flag.
4312 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004313 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004314
Chris Lattnerf81d5882007-11-24 07:07:01 +00004315 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004316 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004317
Chris Lattnerf81d5882007-11-24 07:07:01 +00004318 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004319 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004320}
4321
Bob Wilson2e076c42009-06-22 23:27:02 +00004322static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4323 SDValue TmpOp0, TmpOp1;
4324 bool Invert = false;
4325 bool Swap = false;
4326 unsigned Opc = 0;
4327
4328 SDValue Op0 = Op.getOperand(0);
4329 SDValue Op1 = Op.getOperand(1);
4330 SDValue CC = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004331 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004332 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004333 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004334
4335 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
4336 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004337 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004338 case ISD::SETUNE:
4339 case ISD::SETNE: Invert = true; // Fallthrough
4340 case ISD::SETOEQ:
4341 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4342 case ISD::SETOLT:
4343 case ISD::SETLT: Swap = true; // Fallthrough
4344 case ISD::SETOGT:
4345 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4346 case ISD::SETOLE:
4347 case ISD::SETLE: Swap = true; // Fallthrough
4348 case ISD::SETOGE:
4349 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4350 case ISD::SETUGE: Swap = true; // Fallthrough
4351 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4352 case ISD::SETUGT: Swap = true; // Fallthrough
4353 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4354 case ISD::SETUEQ: Invert = true; // Fallthrough
4355 case ISD::SETONE:
4356 // Expand this to (OLT | OGT).
4357 TmpOp0 = Op0;
4358 TmpOp1 = Op1;
4359 Opc = ISD::OR;
4360 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
4361 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
4362 break;
4363 case ISD::SETUO: Invert = true; // Fallthrough
4364 case ISD::SETO:
4365 // Expand this to (OLT | OGE).
4366 TmpOp0 = Op0;
4367 TmpOp1 = Op1;
4368 Opc = ISD::OR;
4369 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
4370 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
4371 break;
4372 }
4373 } else {
4374 // Integer comparisons.
4375 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004376 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004377 case ISD::SETNE: Invert = true;
4378 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4379 case ISD::SETLT: Swap = true;
4380 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4381 case ISD::SETLE: Swap = true;
4382 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4383 case ISD::SETULT: Swap = true;
4384 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4385 case ISD::SETULE: Swap = true;
4386 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4387 }
4388
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004389 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004390 if (Opc == ARMISD::VCEQ) {
4391
4392 SDValue AndOp;
4393 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4394 AndOp = Op0;
4395 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4396 AndOp = Op1;
4397
4398 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004399 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004400 AndOp = AndOp.getOperand(0);
4401
4402 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4403 Opc = ARMISD::VTST;
Wesley Peck527da1b2010-11-23 03:31:01 +00004404 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
4405 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004406 Invert = !Invert;
4407 }
4408 }
4409 }
4410
4411 if (Swap)
4412 std::swap(Op0, Op1);
4413
Owen Andersonc7baee32010-11-08 23:21:22 +00004414 // If one of the operands is a constant vector zero, attempt to fold the
4415 // comparison to a specialized compare-against-zero form.
4416 SDValue SingleOp;
4417 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4418 SingleOp = Op0;
4419 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4420 if (Opc == ARMISD::VCGE)
4421 Opc = ARMISD::VCLEZ;
4422 else if (Opc == ARMISD::VCGT)
4423 Opc = ARMISD::VCLTZ;
4424 SingleOp = Op1;
4425 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004426
Owen Andersonc7baee32010-11-08 23:21:22 +00004427 SDValue Result;
4428 if (SingleOp.getNode()) {
4429 switch (Opc) {
4430 case ARMISD::VCEQ:
4431 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
4432 case ARMISD::VCGE:
4433 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
4434 case ARMISD::VCLEZ:
4435 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
4436 case ARMISD::VCGT:
4437 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
4438 case ARMISD::VCLTZ:
4439 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
4440 default:
4441 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
4442 }
4443 } else {
4444 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
4445 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004446
4447 if (Invert)
4448 Result = DAG.getNOT(dl, Result, VT);
4449
4450 return Result;
4451}
4452
Bob Wilson5b2b5042010-06-14 22:19:57 +00004453/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4454/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004455/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004456static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4457 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Andersona4076922010-11-05 21:57:54 +00004458 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004459 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004460
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004461 // SplatBitSize is set to the smallest size that splats the vector, so a
4462 // zero vector will always have SplatBitSize == 8. However, NEON modified
4463 // immediate instructions others than VMOV do not support the 8-bit encoding
4464 // of a zero vector, and the default encoding of zero is supposed to be the
4465 // 32-bit version.
4466 if (SplatBits == 0)
4467 SplatBitSize = 32;
4468
Bob Wilson2e076c42009-06-22 23:27:02 +00004469 switch (SplatBitSize) {
4470 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004471 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004472 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004473 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004474 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004475 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004476 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004477 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004478 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004479
4480 case 16:
4481 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004482 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004483 if ((SplatBits & ~0xff) == 0) {
4484 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004485 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004486 Imm = SplatBits;
4487 break;
4488 }
4489 if ((SplatBits & ~0xff00) == 0) {
4490 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004491 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004492 Imm = SplatBits >> 8;
4493 break;
4494 }
4495 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004496
4497 case 32:
4498 // NEON's 32-bit VMOV supports splat values where:
4499 // * only one byte is nonzero, or
4500 // * the least significant byte is 0xff and the second byte is nonzero, or
4501 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004502 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004503 if ((SplatBits & ~0xff) == 0) {
4504 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004505 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004506 Imm = SplatBits;
4507 break;
4508 }
4509 if ((SplatBits & ~0xff00) == 0) {
4510 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004511 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004512 Imm = SplatBits >> 8;
4513 break;
4514 }
4515 if ((SplatBits & ~0xff0000) == 0) {
4516 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004517 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004518 Imm = SplatBits >> 16;
4519 break;
4520 }
4521 if ((SplatBits & ~0xff000000) == 0) {
4522 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004523 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004524 Imm = SplatBits >> 24;
4525 break;
4526 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004527
Owen Andersona4076922010-11-05 21:57:54 +00004528 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4529 if (type == OtherModImm) return SDValue();
4530
Bob Wilson2e076c42009-06-22 23:27:02 +00004531 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004532 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4533 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004534 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004535 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004536 break;
4537 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004538
4539 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004540 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4541 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004542 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004543 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004544 break;
4545 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004546
4547 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4548 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4549 // VMOV.I32. A (very) minor optimization would be to replicate the value
4550 // and fall through here to test for a valid 64-bit splat. But, then the
4551 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004552 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004553
4554 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004555 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004556 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004557 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00004558 uint64_t BitMask = 0xff;
4559 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004560 unsigned ImmMask = 1;
4561 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00004562 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00004563 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004564 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00004565 Imm |= ImmMask;
4566 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004567 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004568 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004569 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004570 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00004571 }
Christian Pirker6f81e752014-06-23 18:05:53 +00004572
4573 if (DAG.getTargetLoweringInfo().isBigEndian())
4574 // swap higher and lower 32 bit word
4575 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4);
4576
Bob Wilson6eae5202010-06-11 21:34:50 +00004577 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004578 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00004579 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00004580 break;
4581 }
4582
Bob Wilson6eae5202010-06-11 21:34:50 +00004583 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00004584 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00004585 }
4586
Bob Wilsona3f19012010-07-13 21:16:48 +00004587 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
4588 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00004589}
4590
Lang Hames591cdaf2012-03-29 21:56:11 +00004591SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4592 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004593 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00004594 return SDValue();
4595
Tim Northoverf79c3a52013-08-20 08:57:11 +00004596 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00004597 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004598
4599 // Try splatting with a VMOV.f32...
4600 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00004601 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
4602
Lang Hames591cdaf2012-03-29 21:56:11 +00004603 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004604 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
4605 // We have code in place to select a valid ConstantFP already, no need to
4606 // do any mangling.
4607 return Op;
4608 }
4609
4610 // It's a float and we are trying to use NEON operations where
4611 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004612 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004613 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
4614 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4615 NewVal);
4616 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
4617 DAG.getConstant(0, MVT::i32));
4618 }
4619
Tim Northoverf79c3a52013-08-20 08:57:11 +00004620 // The rest of our options are NEON only, make sure that's allowed before
4621 // proceeding..
4622 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
4623 return SDValue();
4624
Lang Hames591cdaf2012-03-29 21:56:11 +00004625 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00004626 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
4627
4628 // It wouldn't really be worth bothering for doubles except for one very
4629 // important value, which does happen to match: 0.0. So make sure we don't do
4630 // anything stupid.
4631 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
4632 return SDValue();
4633
4634 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
4635 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4636 false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004637 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004638 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004639 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4640 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004641 if (IsDouble)
4642 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4643
4644 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004645 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4646 VecConstant);
4647 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4648 DAG.getConstant(0, MVT::i32));
4649 }
4650
4651 // Finally, try a VMVN.i32
Tim Northoverf79c3a52013-08-20 08:57:11 +00004652 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4653 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004654 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004655 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004656 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004657
4658 if (IsDouble)
4659 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4660
4661 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004662 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4663 VecConstant);
4664 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4665 DAG.getConstant(0, MVT::i32));
4666 }
4667
4668 return SDValue();
4669}
4670
Quentin Colombet8e1fe842012-11-02 21:32:17 +00004671// check if an VEXT instruction can handle the shuffle mask when the
4672// vector sources of the shuffle are the same.
4673static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4674 unsigned NumElts = VT.getVectorNumElements();
4675
4676 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4677 if (M[0] < 0)
4678 return false;
4679
4680 Imm = M[0];
4681
4682 // If this is a VEXT shuffle, the immediate value is the index of the first
4683 // element. The other shuffle indices must be the successive elements after
4684 // the first one.
4685 unsigned ExpectedElt = Imm;
4686 for (unsigned i = 1; i < NumElts; ++i) {
4687 // Increment the expected index. If it wraps around, just follow it
4688 // back to index zero and keep going.
4689 ++ExpectedElt;
4690 if (ExpectedElt == NumElts)
4691 ExpectedElt = 0;
4692
4693 if (M[i] < 0) continue; // ignore UNDEF indices
4694 if (ExpectedElt != static_cast<unsigned>(M[i]))
4695 return false;
4696 }
4697
4698 return true;
4699}
4700
Lang Hames591cdaf2012-03-29 21:56:11 +00004701
Benjamin Kramer339ced42012-01-15 13:16:05 +00004702static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004703 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004704 unsigned NumElts = VT.getVectorNumElements();
4705 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00004706
4707 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4708 if (M[0] < 0)
4709 return false;
4710
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004711 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00004712
4713 // If this is a VEXT shuffle, the immediate value is the index of the first
4714 // element. The other shuffle indices must be the successive elements after
4715 // the first one.
4716 unsigned ExpectedElt = Imm;
4717 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004718 // Increment the expected index. If it wraps around, it may still be
4719 // a VEXT but the source vectors must be swapped.
4720 ExpectedElt += 1;
4721 if (ExpectedElt == NumElts * 2) {
4722 ExpectedElt = 0;
4723 ReverseVEXT = true;
4724 }
4725
Bob Wilson411dfad2010-08-17 05:54:34 +00004726 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004727 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00004728 return false;
4729 }
4730
4731 // Adjust the index value if the source operands will be swapped.
4732 if (ReverseVEXT)
4733 Imm -= NumElts;
4734
Bob Wilson32cd8552009-08-19 17:03:43 +00004735 return true;
4736}
4737
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004738/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4739/// instruction with the specified blocksize. (The order of the elements
4740/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00004741static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004742 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4743 "Only possible block sizes for VREV are: 16, 32, 64");
4744
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004745 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00004746 if (EltSz == 64)
4747 return false;
4748
4749 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004750 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00004751 // If the first shuffle index is UNDEF, be optimistic.
4752 if (M[0] < 0)
4753 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004754
4755 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4756 return false;
4757
4758 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004759 if (M[i] < 0) continue; // ignore UNDEF indices
4760 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004761 return false;
4762 }
4763
4764 return true;
4765}
4766
Benjamin Kramer339ced42012-01-15 13:16:05 +00004767static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00004768 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
4769 // range, then 0 is placed into the resulting vector. So pretty much any mask
4770 // of 8 elements can work here.
4771 return VT == MVT::v8i8 && M.size() == 8;
4772}
4773
Benjamin Kramer339ced42012-01-15 13:16:05 +00004774static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004775 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4776 if (EltSz == 64)
4777 return false;
4778
Bob Wilsona7062312009-08-21 20:54:19 +00004779 unsigned NumElts = VT.getVectorNumElements();
4780 WhichResult = (M[0] == 0 ? 0 : 1);
4781 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004782 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4783 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsona7062312009-08-21 20:54:19 +00004784 return false;
4785 }
4786 return true;
4787}
4788
Bob Wilson0bbd3072009-12-03 06:40:55 +00004789/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
4790/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4791/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004792static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004793 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4794 if (EltSz == 64)
4795 return false;
4796
4797 unsigned NumElts = VT.getVectorNumElements();
4798 WhichResult = (M[0] == 0 ? 0 : 1);
4799 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004800 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4801 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004802 return false;
4803 }
4804 return true;
4805}
4806
Benjamin Kramer339ced42012-01-15 13:16:05 +00004807static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004808 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4809 if (EltSz == 64)
4810 return false;
4811
Bob Wilsona7062312009-08-21 20:54:19 +00004812 unsigned NumElts = VT.getVectorNumElements();
4813 WhichResult = (M[0] == 0 ? 0 : 1);
4814 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004815 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsona7062312009-08-21 20:54:19 +00004816 if ((unsigned) M[i] != 2 * i + WhichResult)
4817 return false;
4818 }
4819
4820 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004821 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004822 return false;
4823
4824 return true;
4825}
4826
Bob Wilson0bbd3072009-12-03 06:40:55 +00004827/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4828/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4829/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00004830static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004831 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4832 if (EltSz == 64)
4833 return false;
4834
4835 unsigned Half = VT.getVectorNumElements() / 2;
4836 WhichResult = (M[0] == 0 ? 0 : 1);
4837 for (unsigned j = 0; j != 2; ++j) {
4838 unsigned Idx = WhichResult;
4839 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004840 int MIdx = M[i + j * Half];
4841 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson0bbd3072009-12-03 06:40:55 +00004842 return false;
4843 Idx += 2;
4844 }
4845 }
4846
4847 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4848 if (VT.is64BitVector() && EltSz == 32)
4849 return false;
4850
4851 return true;
4852}
4853
Benjamin Kramer339ced42012-01-15 13:16:05 +00004854static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004855 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4856 if (EltSz == 64)
4857 return false;
4858
Bob Wilsona7062312009-08-21 20:54:19 +00004859 unsigned NumElts = VT.getVectorNumElements();
4860 WhichResult = (M[0] == 0 ? 0 : 1);
4861 unsigned Idx = WhichResult * NumElts / 2;
4862 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004863 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4864 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsona7062312009-08-21 20:54:19 +00004865 return false;
4866 Idx += 1;
4867 }
4868
4869 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004870 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004871 return false;
4872
4873 return true;
4874}
4875
Bob Wilson0bbd3072009-12-03 06:40:55 +00004876/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4877/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4878/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004879static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004880 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4881 if (EltSz == 64)
4882 return false;
4883
4884 unsigned NumElts = VT.getVectorNumElements();
4885 WhichResult = (M[0] == 0 ? 0 : 1);
4886 unsigned Idx = WhichResult * NumElts / 2;
4887 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004888 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4889 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004890 return false;
4891 Idx += 1;
4892 }
4893
4894 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4895 if (VT.is64BitVector() && EltSz == 32)
4896 return false;
4897
4898 return true;
4899}
4900
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00004901/// \return true if this is a reverse operation on an vector.
4902static bool isReverseMask(ArrayRef<int> M, EVT VT) {
4903 unsigned NumElts = VT.getVectorNumElements();
4904 // Make sure the mask has the right size.
4905 if (NumElts != M.size())
4906 return false;
4907
4908 // Look for <15, ..., 3, -1, 1, 0>.
4909 for (unsigned i = 0; i != NumElts; ++i)
4910 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
4911 return false;
4912
4913 return true;
4914}
4915
Dale Johannesen2bff5052010-07-29 20:10:08 +00004916// If N is an integer constant that can be moved into a register in one
4917// instruction, return an SDValue of such a constant (will become a MOV
4918// instruction). Otherwise return null.
4919static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004920 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00004921 uint64_t Val;
4922 if (!isa<ConstantSDNode>(N))
4923 return SDValue();
4924 Val = cast<ConstantSDNode>(N)->getZExtValue();
4925
4926 if (ST->isThumb1Only()) {
4927 if (Val <= 255 || ~Val <= 255)
4928 return DAG.getConstant(Val, MVT::i32);
4929 } else {
4930 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4931 return DAG.getConstant(Val, MVT::i32);
4932 }
4933 return SDValue();
4934}
4935
Bob Wilson2e076c42009-06-22 23:27:02 +00004936// If this is a case we can't handle, return null and let the default
4937// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00004938SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4939 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00004940 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004941 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004942 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004943
4944 APInt SplatBits, SplatUndef;
4945 unsigned SplatBitSize;
4946 bool HasAnyUndefs;
4947 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00004948 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00004949 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00004950 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00004951 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00004952 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00004953 DAG, VmovVT, VT.is128BitVector(),
4954 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00004955 if (Val.getNode()) {
4956 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00004957 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00004958 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00004959
4960 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00004961 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004962 Val = isNEONModifiedImm(NegatedImm,
4963 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00004964 DAG, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00004965 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00004966 if (Val.getNode()) {
4967 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00004968 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00004969 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00004970
4971 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00004972 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00004973 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00004974 if (ImmVal != -1) {
4975 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4976 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4977 }
4978 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00004979 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00004980 }
4981
Bob Wilson91fdf682010-05-22 00:23:12 +00004982 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00004983 //
4984 // As an optimisation, even if more than one value is used it may be more
4985 // profitable to splat with one value then change some lanes.
4986 //
4987 // Heuristically we decide to do this if the vector has a "dominant" value,
4988 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00004989 unsigned NumElts = VT.getVectorNumElements();
4990 bool isOnlyLowElement = true;
4991 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00004992 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00004993 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00004994
4995 // Map of the number of times a particular SDValue appears in the
4996 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00004997 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00004998 SDValue Value;
4999 for (unsigned i = 0; i < NumElts; ++i) {
5000 SDValue V = Op.getOperand(i);
5001 if (V.getOpcode() == ISD::UNDEF)
5002 continue;
5003 if (i > 0)
5004 isOnlyLowElement = false;
5005 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
5006 isConstant = false;
5007
James Molloy49bdbce2012-09-06 09:55:02 +00005008 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00005009 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00005010
James Molloy49bdbce2012-09-06 09:55:02 +00005011 // Is this value dominant? (takes up more than half of the lanes)
5012 if (++Count > (NumElts / 2)) {
5013 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00005014 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00005015 }
Bob Wilson91fdf682010-05-22 00:23:12 +00005016 }
James Molloy49bdbce2012-09-06 09:55:02 +00005017 if (ValueCounts.size() != 1)
5018 usesOnlyOneValue = false;
5019 if (!Value.getNode() && ValueCounts.size() > 0)
5020 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00005021
James Molloy49bdbce2012-09-06 09:55:02 +00005022 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00005023 return DAG.getUNDEF(VT);
5024
Quentin Colombet0f2fe742013-07-23 22:34:47 +00005025 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
5026 // Keep going if we are hitting this case.
5027 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00005028 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
5029
Dale Johannesen2bff5052010-07-29 20:10:08 +00005030 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5031
Dale Johannesen710a2d92010-10-19 20:00:17 +00005032 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
5033 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00005034 if (hasDominantValue && EltSize <= 32) {
5035 if (!isConstant) {
5036 SDValue N;
5037
5038 // If we are VDUPing a value that comes directly from a vector, that will
5039 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005040 // just use VDUPLANE. We can only do this if the lane being extracted
5041 // is at a constant index, as the VDUP from lane instructions only have
5042 // constant-index forms.
5043 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5044 isa<ConstantSDNode>(Value->getOperand(1))) {
Silviu Barangab1409702012-10-15 09:41:32 +00005045 // We need to create a new undef vector to use for the VDUPLANE if the
5046 // size of the vector from which we get the value is different than the
5047 // size of the vector that we need to create. We will insert the element
5048 // such that the register coalescer will remove unnecessary copies.
5049 if (VT != Value->getOperand(0).getValueType()) {
5050 ConstantSDNode *constIndex;
5051 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
5052 assert(constIndex && "The index is not a constant!");
5053 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
5054 VT.getVectorNumElements();
5055 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
5056 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
5057 Value, DAG.getConstant(index, MVT::i32)),
5058 DAG.getConstant(index, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005059 } else
Silviu Barangab1409702012-10-15 09:41:32 +00005060 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00005061 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005062 } else
James Molloy49bdbce2012-09-06 09:55:02 +00005063 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
5064
5065 if (!usesOnlyOneValue) {
5066 // The dominant value was splatted as 'N', but we now have to insert
5067 // all differing elements.
5068 for (unsigned I = 0; I < NumElts; ++I) {
5069 if (Op.getOperand(I) == Value)
5070 continue;
5071 SmallVector<SDValue, 3> Ops;
5072 Ops.push_back(N);
5073 Ops.push_back(Op.getOperand(I));
5074 Ops.push_back(DAG.getConstant(I, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00005075 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00005076 }
5077 }
5078 return N;
5079 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00005080 if (VT.getVectorElementType().isFloatingPoint()) {
5081 SmallVector<SDValue, 8> Ops;
5082 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005083 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005084 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005085 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00005086 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005087 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5088 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005089 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005090 }
James Molloy49bdbce2012-09-06 09:55:02 +00005091 if (usesOnlyOneValue) {
5092 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5093 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005094 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005095 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005096 }
5097
5098 // If all elements are constants and the case above didn't get hit, fall back
5099 // to the default expansion, which will generate a load from the constant
5100 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005101 if (isConstant)
5102 return SDValue();
5103
Bob Wilson6f2b8962011-01-07 21:37:30 +00005104 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5105 if (NumElts >= 4) {
5106 SDValue shuffle = ReconstructShuffle(Op, DAG);
5107 if (shuffle != SDValue())
5108 return shuffle;
5109 }
5110
Bob Wilson91fdf682010-05-22 00:23:12 +00005111 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005112 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5113 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005114 if (EltSize >= 32) {
5115 // Do the expansion with floating-point types, since that is what the VFP
5116 // registers are defined to use, and since i64 is not legal.
5117 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5118 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005119 SmallVector<SDValue, 8> Ops;
5120 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005121 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005122 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005123 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005124 }
5125
Jim Grosbach24e102a2013-07-08 18:18:52 +00005126 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5127 // know the default expansion would otherwise fall back on something even
5128 // worse. For a vector with one or two non-undef values, that's
5129 // scalar_to_vector for the elements followed by a shuffle (provided the
5130 // shuffle is valid for the target) and materialization element by element
5131 // on the stack followed by a load for everything else.
5132 if (!isConstant && !usesOnlyOneValue) {
5133 SDValue Vec = DAG.getUNDEF(VT);
5134 for (unsigned i = 0 ; i < NumElts; ++i) {
5135 SDValue V = Op.getOperand(i);
5136 if (V.getOpcode() == ISD::UNDEF)
5137 continue;
5138 SDValue LaneIdx = DAG.getConstant(i, MVT::i32);
5139 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5140 }
5141 return Vec;
5142 }
5143
Bob Wilson2e076c42009-06-22 23:27:02 +00005144 return SDValue();
5145}
5146
Bob Wilson6f2b8962011-01-07 21:37:30 +00005147// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005148// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005149SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5150 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005151 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005152 EVT VT = Op.getValueType();
5153 unsigned NumElts = VT.getVectorNumElements();
5154
5155 SmallVector<SDValue, 2> SourceVecs;
5156 SmallVector<unsigned, 2> MinElts;
5157 SmallVector<unsigned, 2> MaxElts;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005158
Bob Wilson6f2b8962011-01-07 21:37:30 +00005159 for (unsigned i = 0; i < NumElts; ++i) {
5160 SDValue V = Op.getOperand(i);
5161 if (V.getOpcode() == ISD::UNDEF)
5162 continue;
5163 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5164 // A shuffle can only come from building a vector from various
5165 // elements of other vectors.
5166 return SDValue();
Eli Friedman74d1da52011-10-14 23:58:49 +00005167 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
5168 VT.getVectorElementType()) {
5169 // This code doesn't know how to handle shuffles where the vector
5170 // element types do not match (this happens because type legalization
5171 // promotes the return type of EXTRACT_VECTOR_ELT).
5172 // FIXME: It might be appropriate to extend this code to handle
5173 // mismatched types.
5174 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005175 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005176
Bob Wilson6f2b8962011-01-07 21:37:30 +00005177 // Record this extraction against the appropriate vector if possible...
5178 SDValue SourceVec = V.getOperand(0);
Jim Grosbach6df755c2012-07-25 17:02:47 +00005179 // If the element number isn't a constant, we can't effectively
5180 // analyze what's going on.
5181 if (!isa<ConstantSDNode>(V.getOperand(1)))
5182 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005183 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5184 bool FoundSource = false;
5185 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
5186 if (SourceVecs[j] == SourceVec) {
5187 if (MinElts[j] > EltNo)
5188 MinElts[j] = EltNo;
5189 if (MaxElts[j] < EltNo)
5190 MaxElts[j] = EltNo;
5191 FoundSource = true;
5192 break;
5193 }
5194 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005195
Bob Wilson6f2b8962011-01-07 21:37:30 +00005196 // Or record a new source if not...
5197 if (!FoundSource) {
5198 SourceVecs.push_back(SourceVec);
5199 MinElts.push_back(EltNo);
5200 MaxElts.push_back(EltNo);
5201 }
5202 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005203
Bob Wilson6f2b8962011-01-07 21:37:30 +00005204 // Currently only do something sane when at most two source vectors
5205 // involved.
5206 if (SourceVecs.size() > 2)
5207 return SDValue();
5208
5209 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
5210 int VEXTOffsets[2] = {0, 0};
Andrew Trick5eb0a302011-01-19 02:26:13 +00005211
Bob Wilson6f2b8962011-01-07 21:37:30 +00005212 // This loop extracts the usage patterns of the source vectors
5213 // and prepares appropriate SDValues for a shuffle if possible.
5214 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
5215 if (SourceVecs[i].getValueType() == VT) {
5216 // No VEXT necessary
5217 ShuffleSrcs[i] = SourceVecs[i];
5218 VEXTOffsets[i] = 0;
5219 continue;
5220 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
5221 // It probably isn't worth padding out a smaller vector just to
5222 // break it down again in a shuffle.
5223 return SDValue();
5224 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005225
Bob Wilson6f2b8962011-01-07 21:37:30 +00005226 // Since only 64-bit and 128-bit vectors are legal on ARM and
5227 // we've eliminated the other cases...
Bob Wilson3fa9c062011-01-07 23:40:46 +00005228 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
5229 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick5eb0a302011-01-19 02:26:13 +00005230
Bob Wilson6f2b8962011-01-07 21:37:30 +00005231 if (MaxElts[i] - MinElts[i] >= NumElts) {
5232 // Span too large for a VEXT to cope
5233 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005234 }
5235
Bob Wilson6f2b8962011-01-07 21:37:30 +00005236 if (MinElts[i] >= NumElts) {
5237 // The extraction can just take the second half
5238 VEXTOffsets[i] = NumElts;
Eric Christopher2af95512011-01-14 23:50:53 +00005239 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5240 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005241 DAG.getIntPtrConstant(NumElts));
5242 } else if (MaxElts[i] < NumElts) {
5243 // The extraction can just take the first half
5244 VEXTOffsets[i] = 0;
Eric Christopher2af95512011-01-14 23:50:53 +00005245 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5246 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005247 DAG.getIntPtrConstant(0));
5248 } else {
5249 // An actual VEXT is needed
5250 VEXTOffsets[i] = MinElts[i];
Eric Christopher2af95512011-01-14 23:50:53 +00005251 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5252 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005253 DAG.getIntPtrConstant(0));
Eric Christopher2af95512011-01-14 23:50:53 +00005254 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5255 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005256 DAG.getIntPtrConstant(NumElts));
5257 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
5258 DAG.getConstant(VEXTOffsets[i], MVT::i32));
5259 }
5260 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005261
Bob Wilson6f2b8962011-01-07 21:37:30 +00005262 SmallVector<int, 8> Mask;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005263
Bob Wilson6f2b8962011-01-07 21:37:30 +00005264 for (unsigned i = 0; i < NumElts; ++i) {
5265 SDValue Entry = Op.getOperand(i);
5266 if (Entry.getOpcode() == ISD::UNDEF) {
5267 Mask.push_back(-1);
5268 continue;
5269 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005270
Bob Wilson6f2b8962011-01-07 21:37:30 +00005271 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher2af95512011-01-14 23:50:53 +00005272 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
5273 .getOperand(1))->getSExtValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005274 if (ExtractVec == SourceVecs[0]) {
5275 Mask.push_back(ExtractElt - VEXTOffsets[0]);
5276 } else {
5277 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
5278 }
5279 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005280
Bob Wilson6f2b8962011-01-07 21:37:30 +00005281 // Final check before we try to produce nonsense...
5282 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher2af95512011-01-14 23:50:53 +00005283 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
5284 &Mask[0]);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005285
Bob Wilson6f2b8962011-01-07 21:37:30 +00005286 return SDValue();
5287}
5288
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005289/// isShuffleMaskLegal - Targets can use this to indicate that they only
5290/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5291/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5292/// are assumed to be legal.
5293bool
5294ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5295 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005296 if (VT.getVectorNumElements() == 4 &&
5297 (VT.is128BitVector() || VT.is64BitVector())) {
5298 unsigned PFIndexes[4];
5299 for (unsigned i = 0; i != 4; ++i) {
5300 if (M[i] < 0)
5301 PFIndexes[i] = 8;
5302 else
5303 PFIndexes[i] = M[i];
5304 }
5305
5306 // Compute the index in the perfect shuffle table.
5307 unsigned PFTableIndex =
5308 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5309 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5310 unsigned Cost = (PFEntry >> 30);
5311
5312 if (Cost <= 4)
5313 return true;
5314 }
5315
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005316 bool ReverseVEXT;
Bob Wilsona7062312009-08-21 20:54:19 +00005317 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005318
Bob Wilson846bd792010-06-07 23:53:38 +00005319 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5320 return (EltSize >= 32 ||
5321 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005322 isVREVMask(M, VT, 64) ||
5323 isVREVMask(M, VT, 32) ||
5324 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005325 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005326 isVTBLMask(M, VT) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005327 isVTRNMask(M, VT, WhichResult) ||
5328 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson0bbd3072009-12-03 06:40:55 +00005329 isVZIPMask(M, VT, WhichResult) ||
5330 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
5331 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005332 isVZIP_v_undef_Mask(M, VT, WhichResult) ||
5333 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005334}
5335
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005336/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5337/// the specified operations to build the shuffle.
5338static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5339 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005340 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005341 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5342 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5343 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5344
5345 enum {
5346 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5347 OP_VREV,
5348 OP_VDUP0,
5349 OP_VDUP1,
5350 OP_VDUP2,
5351 OP_VDUP3,
5352 OP_VEXT1,
5353 OP_VEXT2,
5354 OP_VEXT3,
5355 OP_VUZPL, // VUZP, left result
5356 OP_VUZPR, // VUZP, right result
5357 OP_VZIPL, // VZIP, left result
5358 OP_VZIPR, // VZIP, right result
5359 OP_VTRNL, // VTRN, left result
5360 OP_VTRNR // VTRN, right result
5361 };
5362
5363 if (OpNum == OP_COPY) {
5364 if (LHSID == (1*9+2)*9+3) return LHS;
5365 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5366 return RHS;
5367 }
5368
5369 SDValue OpLHS, OpRHS;
5370 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5371 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5372 EVT VT = OpLHS.getValueType();
5373
5374 switch (OpNum) {
5375 default: llvm_unreachable("Unknown shuffle opcode!");
5376 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00005377 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00005378 if (VT.getVectorElementType() == MVT::i32 ||
5379 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00005380 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
5381 // vrev <4 x i16> -> VREV32
5382 if (VT.getVectorElementType() == MVT::i16)
5383 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
5384 // vrev <4 x i8> -> VREV16
5385 assert(VT.getVectorElementType() == MVT::i8);
5386 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005387 case OP_VDUP0:
5388 case OP_VDUP1:
5389 case OP_VDUP2:
5390 case OP_VDUP3:
5391 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005392 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005393 case OP_VEXT1:
5394 case OP_VEXT2:
5395 case OP_VEXT3:
5396 return DAG.getNode(ARMISD::VEXT, dl, VT,
5397 OpLHS, OpRHS,
5398 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
5399 case OP_VUZPL:
5400 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005401 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005402 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
5403 case OP_VZIPL:
5404 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005405 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005406 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
5407 case OP_VTRNL:
5408 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005409 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5410 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005411 }
5412}
5413
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005414static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005415 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005416 SelectionDAG &DAG) {
5417 // Check to see if we can use the VTBL instruction.
5418 SDValue V1 = Op.getOperand(0);
5419 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005420 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005421
5422 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00005423 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005424 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
5425 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
5426
5427 if (V2.getNode()->getOpcode() == ISD::UNDEF)
5428 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Craig Topper48d114b2014-04-26 18:35:24 +00005429 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00005430
Owen Anderson77aa2662011-04-05 21:48:57 +00005431 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Craig Topper48d114b2014-04-26 18:35:24 +00005432 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005433}
5434
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005435static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
5436 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005437 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005438 SDValue OpLHS = Op.getOperand(0);
5439 EVT VT = OpLHS.getValueType();
5440
5441 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
5442 "Expect an v8i16/v16i8 type");
5443 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
5444 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
5445 // extract the first 8 bytes into the top double word and the last 8 bytes
5446 // into the bottom double word. The v8i16 case is similar.
5447 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
5448 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
5449 DAG.getConstant(ExtractNum, MVT::i32));
5450}
5451
Bob Wilson2e076c42009-06-22 23:27:02 +00005452static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005453 SDValue V1 = Op.getOperand(0);
5454 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005455 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00005456 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005457 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00005458
Bob Wilsonc6800b52009-08-13 02:13:04 +00005459 // Convert shuffles that are directly supported on NEON to target-specific
5460 // DAG nodes, instead of keeping them as shuffles and matching them again
5461 // during code selection. This is more efficient and avoids the possibility
5462 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00005463 // FIXME: floating-point vectors should be canonicalized to integer vectors
5464 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005465 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005466
Bob Wilson846bd792010-06-07 23:53:38 +00005467 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5468 if (EltSize <= 32) {
5469 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
5470 int Lane = SVN->getSplatIndex();
5471 // If this is undef splat, generate it via "just" vdup, if possible.
5472 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00005473
Dan Gohman198b7ff2011-11-03 21:49:52 +00005474 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00005475 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5476 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5477 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005478 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
5479 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
5480 // reaches it).
5481 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
5482 !isa<ConstantSDNode>(V1.getOperand(0))) {
5483 bool IsScalarToVector = true;
5484 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
5485 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
5486 IsScalarToVector = false;
5487 break;
5488 }
5489 if (IsScalarToVector)
5490 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5491 }
Bob Wilson846bd792010-06-07 23:53:38 +00005492 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
5493 DAG.getConstant(Lane, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00005494 }
Bob Wilson846bd792010-06-07 23:53:38 +00005495
5496 bool ReverseVEXT;
5497 unsigned Imm;
5498 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
5499 if (ReverseVEXT)
5500 std::swap(V1, V2);
5501 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
5502 DAG.getConstant(Imm, MVT::i32));
5503 }
5504
5505 if (isVREVMask(ShuffleMask, VT, 64))
5506 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
5507 if (isVREVMask(ShuffleMask, VT, 32))
5508 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
5509 if (isVREVMask(ShuffleMask, VT, 16))
5510 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
5511
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005512 if (V2->getOpcode() == ISD::UNDEF &&
5513 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
5514 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
5515 DAG.getConstant(Imm, MVT::i32));
5516 }
5517
Bob Wilson846bd792010-06-07 23:53:38 +00005518 // Check for Neon shuffles that modify both input vectors in place.
5519 // If both results are used, i.e., if there are two shuffles with the same
5520 // source operands and with masks corresponding to both results of one of
5521 // these operations, DAG memoization will ensure that a single node is
5522 // used for both shuffles.
5523 unsigned WhichResult;
5524 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5525 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5526 V1, V2).getValue(WhichResult);
5527 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5528 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5529 V1, V2).getValue(WhichResult);
5530 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5531 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5532 V1, V2).getValue(WhichResult);
5533
5534 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5535 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5536 V1, V1).getValue(WhichResult);
5537 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5538 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5539 V1, V1).getValue(WhichResult);
5540 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5541 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5542 V1, V1).getValue(WhichResult);
Bob Wilsoncce31f62009-08-14 05:08:32 +00005543 }
Bob Wilson32cd8552009-08-19 17:03:43 +00005544
Bob Wilsona7062312009-08-21 20:54:19 +00005545 // If the shuffle is not directly supported and it has 4 elements, use
5546 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00005547 unsigned NumElts = VT.getVectorNumElements();
5548 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005549 unsigned PFIndexes[4];
5550 for (unsigned i = 0; i != 4; ++i) {
5551 if (ShuffleMask[i] < 0)
5552 PFIndexes[i] = 8;
5553 else
5554 PFIndexes[i] = ShuffleMask[i];
5555 }
5556
5557 // Compute the index in the perfect shuffle table.
5558 unsigned PFTableIndex =
5559 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005560 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5561 unsigned Cost = (PFEntry >> 30);
5562
5563 if (Cost <= 4)
5564 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
5565 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00005566
Bob Wilsond8a9a042010-06-04 00:04:02 +00005567 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00005568 if (EltSize >= 32) {
5569 // Do the expansion with floating-point types, since that is what the VFP
5570 // registers are defined to use, and since i64 is not legal.
5571 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5572 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00005573 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
5574 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005575 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00005576 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00005577 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00005578 Ops.push_back(DAG.getUNDEF(EltVT));
5579 else
5580 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
5581 ShuffleMask[i] < (int)NumElts ? V1 : V2,
5582 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
5583 MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00005584 }
Craig Topper48d114b2014-04-26 18:35:24 +00005585 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005586 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00005587 }
5588
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005589 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
5590 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
5591
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005592 if (VT == MVT::v8i8) {
5593 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
5594 if (NewOp.getNode())
5595 return NewOp;
5596 }
5597
Bob Wilson6f34e272009-08-14 05:16:33 +00005598 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005599}
5600
Eli Friedmana5e244c2011-10-24 23:08:52 +00005601static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
5602 // INSERT_VECTOR_ELT is legal only for immediate indexes.
5603 SDValue Lane = Op.getOperand(2);
5604 if (!isa<ConstantSDNode>(Lane))
5605 return SDValue();
5606
5607 return Op;
5608}
5609
Bob Wilson2e076c42009-06-22 23:27:02 +00005610static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00005611 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00005612 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00005613 if (!isa<ConstantSDNode>(Lane))
5614 return SDValue();
5615
5616 SDValue Vec = Op.getOperand(0);
5617 if (Op.getValueType() == MVT::i32 &&
5618 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005619 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00005620 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
5621 }
5622
5623 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00005624}
5625
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005626static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5627 // The only time a CONCAT_VECTORS operation can have legal types is when
5628 // two 64-bit vectors are concatenated to a 128-bit vector.
5629 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
5630 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005631 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005632 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005633 SDValue Op0 = Op.getOperand(0);
5634 SDValue Op1 = Op.getOperand(1);
5635 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005636 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005637 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005638 DAG.getIntPtrConstant(0));
5639 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005640 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005641 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005642 DAG.getIntPtrConstant(1));
Wesley Peck527da1b2010-11-23 03:31:01 +00005643 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005644}
5645
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005646/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
5647/// element has been zero/sign-extended, depending on the isSigned parameter,
5648/// from an integer type half its size.
5649static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
5650 bool isSigned) {
5651 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
5652 EVT VT = N->getValueType(0);
5653 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
5654 SDNode *BVN = N->getOperand(0).getNode();
5655 if (BVN->getValueType(0) != MVT::v4i32 ||
5656 BVN->getOpcode() != ISD::BUILD_VECTOR)
5657 return false;
5658 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5659 unsigned HiElt = 1 - LoElt;
5660 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
5661 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
5662 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
5663 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
5664 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
5665 return false;
5666 if (isSigned) {
5667 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
5668 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
5669 return true;
5670 } else {
5671 if (Hi0->isNullValue() && Hi1->isNullValue())
5672 return true;
5673 }
5674 return false;
5675 }
5676
5677 if (N->getOpcode() != ISD::BUILD_VECTOR)
5678 return false;
5679
5680 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5681 SDNode *Elt = N->getOperand(i).getNode();
5682 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
5683 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5684 unsigned HalfSize = EltSize / 2;
5685 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005686 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005687 return false;
5688 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005689 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005690 return false;
5691 }
5692 continue;
5693 }
5694 return false;
5695 }
5696
5697 return true;
5698}
5699
5700/// isSignExtended - Check if a node is a vector value that is sign-extended
5701/// or a constant BUILD_VECTOR with sign-extended elements.
5702static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
5703 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
5704 return true;
5705 if (isExtendedBUILD_VECTOR(N, DAG, true))
5706 return true;
5707 return false;
5708}
5709
5710/// isZeroExtended - Check if a node is a vector value that is zero-extended
5711/// or a constant BUILD_VECTOR with zero-extended elements.
5712static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
5713 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
5714 return true;
5715 if (isExtendedBUILD_VECTOR(N, DAG, false))
5716 return true;
5717 return false;
5718}
5719
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005720static EVT getExtensionTo64Bits(const EVT &OrigVT) {
5721 if (OrigVT.getSizeInBits() >= 64)
5722 return OrigVT;
5723
5724 assert(OrigVT.isSimple() && "Expecting a simple value type");
5725
5726 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
5727 switch (OrigSimpleTy) {
5728 default: llvm_unreachable("Unexpected Vector Type");
5729 case MVT::v2i8:
5730 case MVT::v2i16:
5731 return MVT::v2i32;
5732 case MVT::v4i8:
5733 return MVT::v4i16;
5734 }
5735}
5736
Sebastian Popa204f722012-11-30 19:08:04 +00005737/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
5738/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
5739/// We insert the required extension here to get the vector to fill a D register.
5740static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
5741 const EVT &OrigTy,
5742 const EVT &ExtTy,
5743 unsigned ExtOpcode) {
5744 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
5745 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
5746 // 64-bits we need to insert a new extension so that it will be 64-bits.
5747 assert(ExtTy.is128BitVector() && "Unexpected extension size");
5748 if (OrigTy.getSizeInBits() >= 64)
5749 return N;
5750
5751 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005752 EVT NewVT = getExtensionTo64Bits(OrigTy);
5753
Andrew Trickef9de2a2013-05-25 02:42:55 +00005754 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00005755}
5756
5757/// SkipLoadExtensionForVMULL - return a load of the original vector size that
5758/// does not do any sign/zero extension. If the original vector is less
5759/// than 64 bits, an appropriate extension will be added after the load to
5760/// reach a total size of 64 bits. We have to add the extension separately
5761/// because ARM does not have a sign/zero extending load for vectors.
5762static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005763 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
5764
5765 // The load already has the right type.
5766 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00005767 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00005768 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
5769 LD->isNonTemporal(), LD->isInvariant(),
5770 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005771
5772 // We need to create a zextload/sextload. We cannot just create a load
5773 // followed by a zext/zext node because LowerMUL is also run during normal
5774 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005775 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005776 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00005777 LD->getMemoryVT(), LD->isVolatile(), LD->isInvariant(),
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005778 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00005779}
5780
5781/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
5782/// extending load, or BUILD_VECTOR with extended elements, return the
5783/// unextended value. The unextended vector should be 64 bits so that it can
5784/// be used as an operand to a VMULL instruction. If the original vector size
5785/// before extension is less than 64 bits we add a an extension to resize
5786/// the vector to 64 bits.
5787static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00005788 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00005789 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
5790 N->getOperand(0)->getValueType(0),
5791 N->getValueType(0),
5792 N->getOpcode());
5793
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005794 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00005795 return SkipLoadExtensionForVMULL(LD, DAG);
5796
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005797 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
5798 // have been legalized as a BITCAST from v4i32.
5799 if (N->getOpcode() == ISD::BITCAST) {
5800 SDNode *BVN = N->getOperand(0).getNode();
5801 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
5802 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
5803 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00005804 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005805 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
5806 }
5807 // Construct a new BUILD_VECTOR with elements truncated to half the size.
5808 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
5809 EVT VT = N->getValueType(0);
5810 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
5811 unsigned NumElts = VT.getVectorNumElements();
5812 MVT TruncVT = MVT::getIntegerVT(EltSize);
5813 SmallVector<SDValue, 8> Ops;
5814 for (unsigned i = 0; i != NumElts; ++i) {
5815 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
5816 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00005817 // Element types smaller than 32 bits are not legal, so use i32 elements.
5818 // The values are implicitly truncated so sext vs. zext doesn't matter.
5819 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005820 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005821 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Craig Topper48d114b2014-04-26 18:35:24 +00005822 MVT::getVectorVT(TruncVT, NumElts), Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00005823}
5824
Evan Chenge2086e72011-03-29 01:56:09 +00005825static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
5826 unsigned Opcode = N->getOpcode();
5827 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5828 SDNode *N0 = N->getOperand(0).getNode();
5829 SDNode *N1 = N->getOperand(1).getNode();
5830 return N0->hasOneUse() && N1->hasOneUse() &&
5831 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
5832 }
5833 return false;
5834}
5835
5836static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
5837 unsigned Opcode = N->getOpcode();
5838 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5839 SDNode *N0 = N->getOperand(0).getNode();
5840 SDNode *N1 = N->getOperand(1).getNode();
5841 return N0->hasOneUse() && N1->hasOneUse() &&
5842 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
5843 }
5844 return false;
5845}
5846
Bob Wilson38ab35a2010-09-01 23:50:19 +00005847static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
5848 // Multiplications are only custom-lowered for 128-bit vectors so that
5849 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
5850 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00005851 assert(VT.is128BitVector() && VT.isInteger() &&
5852 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00005853 SDNode *N0 = Op.getOperand(0).getNode();
5854 SDNode *N1 = Op.getOperand(1).getNode();
5855 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00005856 bool isMLA = false;
5857 bool isN0SExt = isSignExtended(N0, DAG);
5858 bool isN1SExt = isSignExtended(N1, DAG);
5859 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00005860 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00005861 else {
5862 bool isN0ZExt = isZeroExtended(N0, DAG);
5863 bool isN1ZExt = isZeroExtended(N1, DAG);
5864 if (isN0ZExt && isN1ZExt)
5865 NewOpc = ARMISD::VMULLu;
5866 else if (isN1SExt || isN1ZExt) {
5867 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
5868 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
5869 if (isN1SExt && isAddSubSExt(N0, DAG)) {
5870 NewOpc = ARMISD::VMULLs;
5871 isMLA = true;
5872 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
5873 NewOpc = ARMISD::VMULLu;
5874 isMLA = true;
5875 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
5876 std::swap(N0, N1);
5877 NewOpc = ARMISD::VMULLu;
5878 isMLA = true;
5879 }
5880 }
5881
5882 if (!NewOpc) {
5883 if (VT == MVT::v2i64)
5884 // Fall through to expand this. It is not legal.
5885 return SDValue();
5886 else
5887 // Other vector multiplications are legal.
5888 return Op;
5889 }
5890 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00005891
5892 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005893 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00005894 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00005895 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005896 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00005897 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005898 assert(Op0.getValueType().is64BitVector() &&
5899 Op1.getValueType().is64BitVector() &&
5900 "unexpected types for extended operands to VMULL");
5901 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
5902 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00005903
Evan Chenge2086e72011-03-29 01:56:09 +00005904 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
5905 // isel lowering to take advantage of no-stall back to back vmul + vmla.
5906 // vmull q0, d4, d6
5907 // vmlal q0, d5, d6
5908 // is faster than
5909 // vaddl q0, d4, d5
5910 // vmovl q1, d6
5911 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00005912 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
5913 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005914 EVT Op1VT = Op1.getValueType();
5915 return DAG.getNode(N0->getOpcode(), DL, VT,
5916 DAG.getNode(NewOpc, DL, VT,
5917 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
5918 DAG.getNode(NewOpc, DL, VT,
5919 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00005920}
5921
Owen Anderson77aa2662011-04-05 21:48:57 +00005922static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005923LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00005924 // Convert to float
5925 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
5926 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
5927 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
5928 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
5929 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
5930 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
5931 // Get reciprocal estimate.
5932 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00005933 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005934 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
5935 // Because char has a smaller range than uchar, we can actually get away
5936 // without any newton steps. This requires that we use a weird bias
5937 // of 0xb000, however (again, this has been exhaustively tested).
5938 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
5939 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
5940 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
5941 Y = DAG.getConstant(0xb000, MVT::i32);
5942 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
5943 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
5944 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
5945 // Convert back to short.
5946 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
5947 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
5948 return X;
5949}
5950
Owen Anderson77aa2662011-04-05 21:48:57 +00005951static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005952LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00005953 SDValue N2;
5954 // Convert to float.
5955 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
5956 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
5957 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
5958 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
5959 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
5960 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005961
Nate Begemanfa62d502011-02-11 20:53:29 +00005962 // Use reciprocal estimate and one refinement step.
5963 // float4 recip = vrecpeq_f32(yf);
5964 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00005965 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005966 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005967 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005968 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
5969 N1, N2);
5970 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5971 // Because short has a smaller range than ushort, we can actually get away
5972 // with only a single newton step. This requires that we use a weird bias
5973 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005974 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00005975 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5976 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005977 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00005978 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5979 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5980 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5981 // Convert back to integer and return.
5982 // return vmovn_s32(vcvt_s32_f32(result));
5983 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5984 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5985 return N0;
5986}
5987
5988static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
5989 EVT VT = Op.getValueType();
5990 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5991 "unexpected type for custom-lowering ISD::SDIV");
5992
Andrew Trickef9de2a2013-05-25 02:42:55 +00005993 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00005994 SDValue N0 = Op.getOperand(0);
5995 SDValue N1 = Op.getOperand(1);
5996 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00005997
Nate Begemanfa62d502011-02-11 20:53:29 +00005998 if (VT == MVT::v8i8) {
5999 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
6000 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006001
Nate Begemanfa62d502011-02-11 20:53:29 +00006002 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6003 DAG.getIntPtrConstant(4));
6004 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00006005 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00006006 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6007 DAG.getIntPtrConstant(0));
6008 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
6009 DAG.getIntPtrConstant(0));
6010
6011 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
6012 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
6013
6014 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6015 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006016
Nate Begemanfa62d502011-02-11 20:53:29 +00006017 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
6018 return N0;
6019 }
6020 return LowerSDIV_v4i16(N0, N1, dl, DAG);
6021}
6022
6023static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
6024 EVT VT = Op.getValueType();
6025 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6026 "unexpected type for custom-lowering ISD::UDIV");
6027
Andrew Trickef9de2a2013-05-25 02:42:55 +00006028 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006029 SDValue N0 = Op.getOperand(0);
6030 SDValue N1 = Op.getOperand(1);
6031 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006032
Nate Begemanfa62d502011-02-11 20:53:29 +00006033 if (VT == MVT::v8i8) {
6034 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
6035 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006036
Nate Begemanfa62d502011-02-11 20:53:29 +00006037 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6038 DAG.getIntPtrConstant(4));
6039 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00006040 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00006041 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6042 DAG.getIntPtrConstant(0));
6043 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
6044 DAG.getIntPtrConstant(0));
Owen Anderson77aa2662011-04-05 21:48:57 +00006045
Nate Begemanfa62d502011-02-11 20:53:29 +00006046 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
6047 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00006048
Nate Begemanfa62d502011-02-11 20:53:29 +00006049 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6050 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006051
6052 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begemanfa62d502011-02-11 20:53:29 +00006053 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
6054 N0);
6055 return N0;
6056 }
Owen Anderson77aa2662011-04-05 21:48:57 +00006057
Nate Begemanfa62d502011-02-11 20:53:29 +00006058 // v4i16 sdiv ... Convert to float.
6059 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
6060 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
6061 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
6062 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
6063 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006064 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00006065
6066 // Use reciprocal estimate and two refinement steps.
6067 // float4 recip = vrecpeq_f32(yf);
6068 // recip *= vrecpsq_f32(yf, recip);
6069 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006070 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006071 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006072 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006073 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006074 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006075 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00006076 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006077 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006078 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006079 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6080 // Simply multiplying by the reciprocal estimate can leave us a few ulps
6081 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6082 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006083 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006084 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6085 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
6086 N1 = DAG.getConstant(2, MVT::i32);
6087 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6088 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6089 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6090 // Convert back to integer and return.
6091 // return vmovn_u32(vcvt_s32_f32(result));
6092 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6093 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6094 return N0;
6095}
6096
Evan Chenge8916542011-08-30 01:34:54 +00006097static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6098 EVT VT = Op.getNode()->getValueType(0);
6099 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6100
6101 unsigned Opc;
6102 bool ExtraOp = false;
6103 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006104 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006105 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6106 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6107 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6108 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6109 }
6110
6111 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006112 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006113 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006114 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006115 Op.getOperand(1), Op.getOperand(2));
6116}
6117
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006118SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6119 assert(Subtarget->isTargetDarwin());
6120
6121 // For iOS, we want to call an alternative entry point: __sincos_stret,
6122 // return values are passed via sret.
6123 SDLoc dl(Op);
6124 SDValue Arg = Op.getOperand(0);
6125 EVT ArgVT = Arg.getValueType();
6126 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
6127
6128 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
6129 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6130
6131 // Pair of floats / doubles used to pass the result.
6132 StructType *RetTy = StructType::get(ArgTy, ArgTy, NULL);
6133
6134 // Create stack object for sret.
6135 const uint64_t ByteSize = TLI.getDataLayout()->getTypeAllocSize(RetTy);
6136 const unsigned StackAlign = TLI.getDataLayout()->getPrefTypeAlignment(RetTy);
6137 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
6138 SDValue SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
6139
6140 ArgListTy Args;
6141 ArgListEntry Entry;
6142
6143 Entry.Node = SRet;
6144 Entry.Ty = RetTy->getPointerTo();
6145 Entry.isSExt = false;
6146 Entry.isZExt = false;
6147 Entry.isSRet = true;
6148 Args.push_back(Entry);
6149
6150 Entry.Node = Arg;
6151 Entry.Ty = ArgTy;
6152 Entry.isSExt = false;
6153 Entry.isZExt = false;
6154 Args.push_back(Entry);
6155
6156 const char *LibcallName = (ArgVT == MVT::f64)
6157 ? "__sincos_stret" : "__sincosf_stret";
6158 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
6159
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006160 TargetLowering::CallLoweringInfo CLI(DAG);
6161 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
6162 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), Callee,
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00006163 std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006164 .setDiscardResult();
6165
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006166 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6167
6168 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6169 MachinePointerInfo(), false, false, false, 0);
6170
6171 // Address of cos field.
6172 SDValue Add = DAG.getNode(ISD::ADD, dl, getPointerTy(), SRet,
6173 DAG.getIntPtrConstant(ArgVT.getStoreSize()));
6174 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6175 MachinePointerInfo(), false, false, false, 0);
6176
6177 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6178 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6179 LoadSin.getValue(0), LoadCos.getValue(0));
6180}
6181
Eli Friedman10f9ce22011-09-15 22:26:18 +00006182static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00006183 // Monotonic load/store is legal for all targets
6184 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
6185 return Op;
6186
Alp Tokercb402912014-01-24 17:20:08 +00006187 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00006188 // dmb or equivalent available.
6189 return SDValue();
6190}
6191
Tim Northoverbc933082013-05-23 19:11:20 +00006192static void ReplaceREADCYCLECOUNTER(SDNode *N,
6193 SmallVectorImpl<SDValue> &Results,
6194 SelectionDAG &DAG,
6195 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006196 SDLoc DL(N);
Tim Northoverbc933082013-05-23 19:11:20 +00006197 SDValue Cycles32, OutChain;
6198
6199 if (Subtarget->hasPerfMon()) {
6200 // Under Power Management extensions, the cycle-count is:
6201 // mrc p15, #0, <Rt>, c9, c13, #0
6202 SDValue Ops[] = { N->getOperand(0), // Chain
6203 DAG.getConstant(Intrinsic::arm_mrc, MVT::i32),
6204 DAG.getConstant(15, MVT::i32),
6205 DAG.getConstant(0, MVT::i32),
6206 DAG.getConstant(9, MVT::i32),
6207 DAG.getConstant(13, MVT::i32),
6208 DAG.getConstant(0, MVT::i32)
6209 };
6210
6211 Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00006212 DAG.getVTList(MVT::i32, MVT::Other), Ops);
Tim Northoverbc933082013-05-23 19:11:20 +00006213 OutChain = Cycles32.getValue(1);
6214 } else {
6215 // Intrinsic is defined to return 0 on unsupported platforms. Technically
6216 // there are older ARM CPUs that have implementation-specific ways of
6217 // obtaining this information (FIXME!).
6218 Cycles32 = DAG.getConstant(0, MVT::i32);
6219 OutChain = DAG.getEntryNode();
6220 }
6221
6222
6223 SDValue Cycles64 = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64,
6224 Cycles32, DAG.getConstant(0, MVT::i32));
6225 Results.push_back(Cycles64);
6226 Results.push_back(OutChain);
6227}
6228
Dan Gohman21cea8a2010-04-17 15:26:15 +00006229SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006230 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006231 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Cheng10043e22007-01-19 07:51:42 +00006232 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006233 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006234 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00006235 switch (Subtarget->getTargetTriple().getObjectFormat()) {
6236 default: llvm_unreachable("unknown object format");
6237 case Triple::COFF:
6238 return LowerGlobalAddressWindows(Op, DAG);
6239 case Triple::ELF:
6240 return LowerGlobalAddressELF(Op, DAG);
6241 case Triple::MachO:
6242 return LowerGlobalAddressDarwin(Op, DAG);
6243 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006244 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006245 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006246 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6247 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006248 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006249 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006250 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006251 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006252 case ISD::SINT_TO_FP:
6253 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6254 case ISD::FP_TO_SINT:
6255 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006256 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006257 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006258 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006259 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006260 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006261 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006262 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6263 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006264 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006265 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006266 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006267 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng15b80e42009-11-12 07:13:11 +00006268 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00006269 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00006270 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach8546ec92010-01-18 19:58:49 +00006271 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00006272 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00006273 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00006274 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006275 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00006276 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00006277 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006278 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006279 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00006280 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006281 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begemanfa62d502011-02-11 20:53:29 +00006282 case ISD::SDIV: return LowerSDIV(Op, DAG);
6283 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00006284 case ISD::ADDC:
6285 case ISD::ADDE:
6286 case ISD::SUBC:
6287 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00006288 case ISD::SADDO:
6289 case ISD::UADDO:
6290 case ISD::SSUBO:
6291 case ISD::USUBO:
6292 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00006293 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00006294 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006295 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00006296 case ISD::SDIVREM:
6297 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00006298 case ISD::DYNAMIC_STACKALLOC:
6299 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
6300 return LowerDYNAMIC_STACKALLOC(Op, DAG);
6301 llvm_unreachable("Don't know how to custom lower this!");
Evan Cheng10043e22007-01-19 07:51:42 +00006302 }
Evan Cheng10043e22007-01-19 07:51:42 +00006303}
6304
Duncan Sands6ed40142008-12-01 11:39:25 +00006305/// ReplaceNodeResults - Replace the results of node with an illegal result
6306/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00006307void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
6308 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006309 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00006310 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006311 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006312 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00006313 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peck527da1b2010-11-23 03:31:01 +00006314 case ISD::BITCAST:
6315 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006316 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006317 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00006318 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00006319 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006320 break;
Tim Northoverbc933082013-05-23 19:11:20 +00006321 case ISD::READCYCLECOUNTER:
6322 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
6323 return;
Duncan Sands6ed40142008-12-01 11:39:25 +00006324 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00006325 if (Res.getNode())
6326 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00006327}
Chris Lattnerf81d5882007-11-24 07:07:01 +00006328
Evan Cheng10043e22007-01-19 07:51:42 +00006329//===----------------------------------------------------------------------===//
6330// ARM Scheduler Hooks
6331//===----------------------------------------------------------------------===//
6332
Bill Wendling030b58e2011-10-06 22:18:16 +00006333/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6334/// registers the function context.
6335void ARMTargetLowering::
6336SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6337 MachineBasicBlock *DispatchBB, int FI) const {
Eric Christopherd9134482014-08-04 21:25:23 +00006338 const TargetInstrInfo *TII =
6339 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Bill Wendling374ee192011-10-03 21:25:38 +00006340 DebugLoc dl = MI->getDebugLoc();
6341 MachineFunction *MF = MBB->getParent();
6342 MachineRegisterInfo *MRI = &MF->getRegInfo();
6343 MachineConstantPool *MCP = MF->getConstantPool();
6344 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6345 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00006346
Bill Wendling374ee192011-10-03 21:25:38 +00006347 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006348 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00006349
Bill Wendling374ee192011-10-03 21:25:38 +00006350 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006351 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00006352 ARMConstantPoolValue *CPV =
6353 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6354 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6355
Craig Topperc7242e02012-04-20 07:30:17 +00006356 const TargetRegisterClass *TRC = isThumb ?
6357 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6358 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00006359
Bill Wendling030b58e2011-10-06 22:18:16 +00006360 // Grab constant pool and fixed stack memory operands.
6361 MachineMemOperand *CPMMO =
6362 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6363 MachineMemOperand::MOLoad, 4, 4);
6364
6365 MachineMemOperand *FIMMOSt =
6366 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6367 MachineMemOperand::MOStore, 4, 4);
6368
6369 // Load the address of the dispatch MBB into the jump buffer.
6370 if (isThumb2) {
6371 // Incoming value: jbuf
6372 // ldr.n r5, LCPI1_1
6373 // orr r5, r5, #1
6374 // add r5, pc
6375 // str r5, [$jbuf, #+4] ; &jbuf[1]
6376 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6377 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6378 .addConstantPoolIndex(CPI)
6379 .addMemOperand(CPMMO));
6380 // Set the low bit because of thumb mode.
6381 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6382 AddDefaultCC(
6383 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6384 .addReg(NewVReg1, RegState::Kill)
6385 .addImm(0x01)));
6386 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6387 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6388 .addReg(NewVReg2, RegState::Kill)
6389 .addImm(PCLabelId);
6390 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6391 .addReg(NewVReg3, RegState::Kill)
6392 .addFrameIndex(FI)
6393 .addImm(36) // &jbuf[1] :: pc
6394 .addMemOperand(FIMMOSt));
6395 } else if (isThumb) {
6396 // Incoming value: jbuf
6397 // ldr.n r1, LCPI1_4
6398 // add r1, pc
6399 // mov r2, #1
6400 // orrs r1, r2
6401 // add r2, $jbuf, #+4 ; &jbuf[1]
6402 // str r1, [r2]
6403 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6404 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6405 .addConstantPoolIndex(CPI)
6406 .addMemOperand(CPMMO));
6407 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6408 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6409 .addReg(NewVReg1, RegState::Kill)
6410 .addImm(PCLabelId);
6411 // Set the low bit because of thumb mode.
6412 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6413 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6414 .addReg(ARM::CPSR, RegState::Define)
6415 .addImm(1));
6416 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6417 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6418 .addReg(ARM::CPSR, RegState::Define)
6419 .addReg(NewVReg2, RegState::Kill)
6420 .addReg(NewVReg3, RegState::Kill));
6421 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6422 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
6423 .addFrameIndex(FI)
6424 .addImm(36)); // &jbuf[1] :: pc
6425 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6426 .addReg(NewVReg4, RegState::Kill)
6427 .addReg(NewVReg5, RegState::Kill)
6428 .addImm(0)
6429 .addMemOperand(FIMMOSt));
6430 } else {
6431 // Incoming value: jbuf
6432 // ldr r1, LCPI1_1
6433 // add r1, pc, r1
6434 // str r1, [$jbuf, #+4] ; &jbuf[1]
6435 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6436 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6437 .addConstantPoolIndex(CPI)
6438 .addImm(0)
6439 .addMemOperand(CPMMO));
6440 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6441 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6442 .addReg(NewVReg1, RegState::Kill)
6443 .addImm(PCLabelId));
6444 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6445 .addReg(NewVReg2, RegState::Kill)
6446 .addFrameIndex(FI)
6447 .addImm(36) // &jbuf[1] :: pc
6448 .addMemOperand(FIMMOSt));
6449 }
6450}
6451
6452MachineBasicBlock *ARMTargetLowering::
6453EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
Eric Christopherd9134482014-08-04 21:25:23 +00006454 const TargetInstrInfo *TII =
6455 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00006456 DebugLoc dl = MI->getDebugLoc();
6457 MachineFunction *MF = MBB->getParent();
6458 MachineRegisterInfo *MRI = &MF->getRegInfo();
6459 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6460 MachineFrameInfo *MFI = MF->getFrameInfo();
6461 int FI = MFI->getFunctionContextIndex();
6462
Craig Topperc7242e02012-04-20 07:30:17 +00006463 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
6464 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen691ae332012-05-20 06:38:47 +00006465 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00006466
Bill Wendling362c1b02011-10-06 21:29:56 +00006467 // Get a mapping of the call site numbers to all of the landing pads they're
6468 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00006469 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6470 unsigned MaxCSNum = 0;
6471 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00006472 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6473 ++BB) {
Bill Wendling202803e2011-10-05 00:02:33 +00006474 if (!BB->isLandingPad()) continue;
6475
6476 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6477 // pad.
6478 for (MachineBasicBlock::iterator
6479 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6480 if (!II->isEHLabel()) continue;
6481
6482 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006483 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00006484
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006485 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6486 for (SmallVectorImpl<unsigned>::iterator
6487 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6488 CSI != CSE; ++CSI) {
6489 CallSiteNumToLPad[*CSI].push_back(BB);
6490 MaxCSNum = std::max(MaxCSNum, *CSI);
6491 }
Bill Wendling202803e2011-10-05 00:02:33 +00006492 break;
6493 }
6494 }
6495
6496 // Get an ordered list of the machine basic blocks for the jump table.
6497 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling883ec972011-10-07 23:18:02 +00006498 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00006499 LPadList.reserve(CallSiteNumToLPad.size());
6500 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6501 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6502 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006503 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00006504 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00006505 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6506 }
Bill Wendling202803e2011-10-05 00:02:33 +00006507 }
6508
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006509 assert(!LPadList.empty() &&
6510 "No landing pad destinations for the dispatch jump table!");
6511
Bill Wendling362c1b02011-10-06 21:29:56 +00006512 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00006513 MachineJumpTableInfo *JTI =
6514 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6515 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
6516 unsigned UId = AFI->createJumpTableUId();
Chad Rosier96603432013-03-01 18:30:38 +00006517 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00006518
Bill Wendling362c1b02011-10-06 21:29:56 +00006519 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00006520
6521 // Shove the dispatch's address into the return slot in the function context.
6522 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6523 DispatchBB->setIsLandingPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00006524
Bill Wendling324be982011-10-05 00:39:32 +00006525 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006526 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00006527 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006528 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00006529 else
6530 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
6531
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006532 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00006533 DispatchBB->addSuccessor(TrapBB);
6534
6535 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6536 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00006537
Bill Wendling510fbcd2011-10-17 21:32:56 +00006538 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00006539 MF->insert(MF->end(), DispatchBB);
6540 MF->insert(MF->end(), DispContBB);
6541 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00006542
Bill Wendling030b58e2011-10-06 22:18:16 +00006543 // Insert code into the entry block that creates and registers the function
6544 // context.
6545 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6546
Bill Wendling030b58e2011-10-06 22:18:16 +00006547 MachineMemOperand *FIMMOLd =
Bill Wendling362c1b02011-10-06 21:29:56 +00006548 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendlingb3d46782011-10-06 23:37:36 +00006549 MachineMemOperand::MOLoad |
6550 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00006551
Chad Rosier1ec8e402012-11-06 23:05:24 +00006552 MachineInstrBuilder MIB;
6553 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6554
6555 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6556 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6557
6558 // Add a register mask with no preserved registers. This results in all
6559 // registers being marked as clobbered.
6560 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00006561
Bill Wendling85833f72011-10-18 22:49:07 +00006562 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00006563 if (Subtarget->isThumb2()) {
6564 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6565 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6566 .addFrameIndex(FI)
6567 .addImm(4)
6568 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006569
Bill Wendling85833f72011-10-18 22:49:07 +00006570 if (NumLPads < 256) {
6571 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6572 .addReg(NewVReg1)
6573 .addImm(LPadList.size()));
6574 } else {
6575 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6576 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006577 .addImm(NumLPads & 0xFFFF));
6578
6579 unsigned VReg2 = VReg1;
6580 if ((NumLPads & 0xFFFF0000) != 0) {
6581 VReg2 = MRI->createVirtualRegister(TRC);
6582 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6583 .addReg(VReg1)
6584 .addImm(NumLPads >> 16));
6585 }
6586
Bill Wendling85833f72011-10-18 22:49:07 +00006587 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6588 .addReg(NewVReg1)
6589 .addReg(VReg2));
6590 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006591
Bill Wendling5626c662011-10-06 22:53:00 +00006592 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6593 .addMBB(TrapBB)
6594 .addImm(ARMCC::HI)
6595 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00006596
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006597 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6598 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006599 .addJumpTableIndex(MJTI)
6600 .addImm(UId));
Bill Wendling202803e2011-10-05 00:02:33 +00006601
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006602 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006603 AddDefaultCC(
6604 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006605 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6606 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00006607 .addReg(NewVReg1)
6608 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6609
6610 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006611 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00006612 .addReg(NewVReg1)
Bill Wendling5626c662011-10-06 22:53:00 +00006613 .addJumpTableIndex(MJTI)
6614 .addImm(UId);
6615 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00006616 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6617 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6618 .addFrameIndex(FI)
6619 .addImm(1)
6620 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00006621
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006622 if (NumLPads < 256) {
6623 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6624 .addReg(NewVReg1)
6625 .addImm(NumLPads));
6626 } else {
6627 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00006628 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6629 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6630
6631 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006632 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006633 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006634 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006635 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006636
6637 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6638 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6639 .addReg(VReg1, RegState::Define)
6640 .addConstantPoolIndex(Idx));
6641 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6642 .addReg(NewVReg1)
6643 .addReg(VReg1));
6644 }
6645
Bill Wendlingb3d46782011-10-06 23:37:36 +00006646 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6647 .addMBB(TrapBB)
6648 .addImm(ARMCC::HI)
6649 .addReg(ARM::CPSR);
6650
6651 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6652 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6653 .addReg(ARM::CPSR, RegState::Define)
6654 .addReg(NewVReg1)
6655 .addImm(2));
6656
6657 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00006658 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendlingb3d46782011-10-06 23:37:36 +00006659 .addJumpTableIndex(MJTI)
6660 .addImm(UId));
6661
6662 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6663 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6664 .addReg(ARM::CPSR, RegState::Define)
6665 .addReg(NewVReg2, RegState::Kill)
6666 .addReg(NewVReg3));
6667
6668 MachineMemOperand *JTMMOLd =
6669 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6670 MachineMemOperand::MOLoad, 4, 4);
6671
6672 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6673 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6674 .addReg(NewVReg4, RegState::Kill)
6675 .addImm(0)
6676 .addMemOperand(JTMMOLd));
6677
Chad Rosier96603432013-03-01 18:30:38 +00006678 unsigned NewVReg6 = NewVReg5;
6679 if (RelocM == Reloc::PIC_) {
6680 NewVReg6 = MRI->createVirtualRegister(TRC);
6681 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6682 .addReg(ARM::CPSR, RegState::Define)
6683 .addReg(NewVReg5, RegState::Kill)
6684 .addReg(NewVReg3));
6685 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00006686
6687 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6688 .addReg(NewVReg6, RegState::Kill)
6689 .addJumpTableIndex(MJTI)
6690 .addImm(UId);
Bill Wendling5626c662011-10-06 22:53:00 +00006691 } else {
6692 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6693 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6694 .addFrameIndex(FI)
6695 .addImm(4)
6696 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00006697
Bill Wendling4969dcd2011-10-18 22:52:20 +00006698 if (NumLPads < 256) {
6699 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6700 .addReg(NewVReg1)
6701 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00006702 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00006703 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6704 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006705 .addImm(NumLPads & 0xFFFF));
6706
6707 unsigned VReg2 = VReg1;
6708 if ((NumLPads & 0xFFFF0000) != 0) {
6709 VReg2 = MRI->createVirtualRegister(TRC);
6710 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6711 .addReg(VReg1)
6712 .addImm(NumLPads >> 16));
6713 }
6714
Bill Wendling4969dcd2011-10-18 22:52:20 +00006715 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6716 .addReg(NewVReg1)
6717 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00006718 } else {
6719 MachineConstantPool *ConstantPool = MF->getConstantPool();
6720 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6721 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6722
6723 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006724 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006725 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006726 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006727 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6728
6729 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6730 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6731 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00006732 .addConstantPoolIndex(Idx)
6733 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00006734 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6735 .addReg(NewVReg1)
6736 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00006737 }
6738
Bill Wendling5626c662011-10-06 22:53:00 +00006739 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6740 .addMBB(TrapBB)
6741 .addImm(ARMCC::HI)
6742 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00006743
Bill Wendling973c8172011-10-18 22:11:18 +00006744 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006745 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00006746 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006747 .addReg(NewVReg1)
6748 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00006749 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6750 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006751 .addJumpTableIndex(MJTI)
6752 .addImm(UId));
6753
6754 MachineMemOperand *JTMMOLd =
6755 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6756 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00006757 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006758 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00006759 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6760 .addReg(NewVReg3, RegState::Kill)
6761 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006762 .addImm(0)
6763 .addMemOperand(JTMMOLd));
6764
Chad Rosier96603432013-03-01 18:30:38 +00006765 if (RelocM == Reloc::PIC_) {
6766 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
6767 .addReg(NewVReg5, RegState::Kill)
6768 .addReg(NewVReg4)
6769 .addJumpTableIndex(MJTI)
6770 .addImm(UId);
6771 } else {
6772 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
6773 .addReg(NewVReg5, RegState::Kill)
6774 .addJumpTableIndex(MJTI)
6775 .addImm(UId);
6776 }
Bill Wendling5626c662011-10-06 22:53:00 +00006777 }
Bill Wendling202803e2011-10-05 00:02:33 +00006778
Bill Wendling324be982011-10-05 00:39:32 +00006779 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006780 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00006781 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006782 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6783 MachineBasicBlock *CurMBB = *I;
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006784 if (SeenMBBs.insert(CurMBB))
Bill Wendling883ec972011-10-07 23:18:02 +00006785 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006786 }
6787
Bill Wendling26d27802011-10-17 05:25:09 +00006788 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00006789 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00006790 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling883ec972011-10-07 23:18:02 +00006791 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6792 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6793 MachineBasicBlock *BB = *I;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006794
6795 // Remove the landing pad successor from the invoke block and replace it
6796 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00006797 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6798 BB->succ_end());
6799 while (!Successors.empty()) {
6800 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling883ec972011-10-07 23:18:02 +00006801 if (SMBB->isLandingPad()) {
6802 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00006803 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006804 }
6805 }
6806
6807 BB->addSuccessor(DispatchBB);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006808
6809 // Find the invoke call and mark all of the callee-saved registers as
6810 // 'implicit defined' so that they're spilled. This prevents code from
6811 // moving instructions to before the EH block, where they will never be
6812 // executed.
6813 for (MachineBasicBlock::reverse_iterator
6814 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00006815 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006816
6817 DenseMap<unsigned, bool> DefRegs;
6818 for (MachineInstr::mop_iterator
6819 OI = II->operands_begin(), OE = II->operands_end();
6820 OI != OE; ++OI) {
6821 if (!OI->isReg()) continue;
6822 DefRegs[OI->getReg()] = true;
6823 }
6824
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00006825 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006826
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006827 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00006828 unsigned Reg = SavedRegs[i];
6829 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00006830 !ARM::tGPRRegClass.contains(Reg) &&
6831 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006832 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006833 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006834 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006835 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006836 continue;
6837 if (!DefRegs[Reg])
6838 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006839 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006840
6841 break;
6842 }
Bill Wendling883ec972011-10-07 23:18:02 +00006843 }
Bill Wendling324be982011-10-05 00:39:32 +00006844
Bill Wendling617075f2011-10-18 18:30:49 +00006845 // Mark all former landing pads as non-landing pads. The dispatch is the only
6846 // landing pad now.
6847 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6848 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6849 (*I)->setIsLandingPad(false);
6850
Bill Wendling324be982011-10-05 00:39:32 +00006851 // The instruction is gone now.
6852 MI->eraseFromParent();
6853
Bill Wendling374ee192011-10-03 21:25:38 +00006854 return MBB;
6855}
6856
Evan Cheng0cc4ad92010-07-13 19:27:42 +00006857static
6858MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6859 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6860 E = MBB->succ_end(); I != E; ++I)
6861 if (*I != Succ)
6862 return *I;
6863 llvm_unreachable("Expecting a BB with two successors!");
6864}
6865
Manman Renb504f492013-10-29 22:27:32 +00006866/// Return the load opcode for a given load size. If load size >= 8,
6867/// neon opcode will be returned.
6868static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
6869 if (LdSize >= 8)
6870 return LdSize == 16 ? ARM::VLD1q32wb_fixed
6871 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
6872 if (IsThumb1)
6873 return LdSize == 4 ? ARM::tLDRi
6874 : LdSize == 2 ? ARM::tLDRHi
6875 : LdSize == 1 ? ARM::tLDRBi : 0;
6876 if (IsThumb2)
6877 return LdSize == 4 ? ARM::t2LDR_POST
6878 : LdSize == 2 ? ARM::t2LDRH_POST
6879 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
6880 return LdSize == 4 ? ARM::LDR_POST_IMM
6881 : LdSize == 2 ? ARM::LDRH_POST
6882 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
6883}
6884
6885/// Return the store opcode for a given store size. If store size >= 8,
6886/// neon opcode will be returned.
6887static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
6888 if (StSize >= 8)
6889 return StSize == 16 ? ARM::VST1q32wb_fixed
6890 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
6891 if (IsThumb1)
6892 return StSize == 4 ? ARM::tSTRi
6893 : StSize == 2 ? ARM::tSTRHi
6894 : StSize == 1 ? ARM::tSTRBi : 0;
6895 if (IsThumb2)
6896 return StSize == 4 ? ARM::t2STR_POST
6897 : StSize == 2 ? ARM::t2STRH_POST
6898 : StSize == 1 ? ARM::t2STRB_POST : 0;
6899 return StSize == 4 ? ARM::STR_POST_IMM
6900 : StSize == 2 ? ARM::STRH_POST
6901 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
6902}
6903
6904/// Emit a post-increment load operation with given size. The instructions
6905/// will be added to BB at Pos.
6906static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
6907 const TargetInstrInfo *TII, DebugLoc dl,
6908 unsigned LdSize, unsigned Data, unsigned AddrIn,
6909 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
6910 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
6911 assert(LdOpc != 0 && "Should have a load opcode");
6912 if (LdSize >= 8) {
6913 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6914 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6915 .addImm(0));
6916 } else if (IsThumb1) {
6917 // load + update AddrIn
6918 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6919 .addReg(AddrIn).addImm(0));
6920 MachineInstrBuilder MIB =
6921 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
6922 MIB = AddDefaultT1CC(MIB);
6923 MIB.addReg(AddrIn).addImm(LdSize);
6924 AddDefaultPred(MIB);
6925 } else if (IsThumb2) {
6926 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6927 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6928 .addImm(LdSize));
6929 } else { // arm
6930 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6931 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6932 .addReg(0).addImm(LdSize));
6933 }
6934}
6935
6936/// Emit a post-increment store operation with given size. The instructions
6937/// will be added to BB at Pos.
6938static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
6939 const TargetInstrInfo *TII, DebugLoc dl,
6940 unsigned StSize, unsigned Data, unsigned AddrIn,
6941 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
6942 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
6943 assert(StOpc != 0 && "Should have a store opcode");
6944 if (StSize >= 8) {
6945 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6946 .addReg(AddrIn).addImm(0).addReg(Data));
6947 } else if (IsThumb1) {
6948 // store + update AddrIn
6949 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
6950 .addReg(AddrIn).addImm(0));
6951 MachineInstrBuilder MIB =
6952 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
6953 MIB = AddDefaultT1CC(MIB);
6954 MIB.addReg(AddrIn).addImm(StSize);
6955 AddDefaultPred(MIB);
6956 } else if (IsThumb2) {
6957 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6958 .addReg(Data).addReg(AddrIn).addImm(StSize));
6959 } else { // arm
6960 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6961 .addReg(Data).addReg(AddrIn).addReg(0)
6962 .addImm(StSize));
6963 }
6964}
6965
David Peixottoc32e24a2013-10-17 19:49:22 +00006966MachineBasicBlock *
6967ARMTargetLowering::EmitStructByval(MachineInstr *MI,
6968 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00006969 // This pseudo instruction has 3 operands: dst, src, size
6970 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6971 // Otherwise, we will generate unrolled scalar copies.
Eric Christopherd9134482014-08-04 21:25:23 +00006972 const TargetInstrInfo *TII =
6973 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Manman Rene8735522012-06-01 19:33:18 +00006974 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6975 MachineFunction::iterator It = BB;
6976 ++It;
6977
6978 unsigned dest = MI->getOperand(0).getReg();
6979 unsigned src = MI->getOperand(1).getReg();
6980 unsigned SizeVal = MI->getOperand(2).getImm();
6981 unsigned Align = MI->getOperand(3).getImm();
6982 DebugLoc dl = MI->getDebugLoc();
6983
Manman Rene8735522012-06-01 19:33:18 +00006984 MachineFunction *MF = BB->getParent();
6985 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00006986 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00006987 const TargetRegisterClass *TRC = nullptr;
6988 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00006989
6990 bool IsThumb1 = Subtarget->isThumb1Only();
6991 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00006992
6993 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00006994 UnitSize = 1;
6995 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00006996 UnitSize = 2;
6997 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00006998 // Check whether we can use NEON instructions.
Bill Wendling698e84f2012-12-30 10:32:01 +00006999 if (!MF->getFunction()->getAttributes().
7000 hasAttribute(AttributeSet::FunctionIndex,
7001 Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00007002 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00007003 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00007004 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00007005 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00007006 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00007007 }
7008 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00007009 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00007010 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00007011 }
Manman Ren6e1fd462012-06-18 22:23:48 +00007012
David Peixottob0653e532013-10-24 16:39:36 +00007013 // Select the correct opcode and register class for unit size load/store
7014 bool IsNeon = UnitSize >= 8;
7015 TRC = (IsThumb1 || IsThumb2) ? (const TargetRegisterClass *)&ARM::tGPRRegClass
7016 : (const TargetRegisterClass *)&ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00007017 if (IsNeon)
David Peixottob0653e532013-10-24 16:39:36 +00007018 VecTRC = UnitSize == 16
7019 ? (const TargetRegisterClass *)&ARM::DPairRegClass
7020 : UnitSize == 8
7021 ? (const TargetRegisterClass *)&ARM::DPRRegClass
Craig Topper062a2ba2014-04-25 05:30:21 +00007022 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007023
Manman Rene8735522012-06-01 19:33:18 +00007024 unsigned BytesLeft = SizeVal % UnitSize;
7025 unsigned LoopSize = SizeVal - BytesLeft;
7026
7027 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
7028 // Use LDR and STR to copy.
7029 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
7030 // [destOut] = STR_POST(scratch, destIn, UnitSize)
7031 unsigned srcIn = src;
7032 unsigned destIn = dest;
7033 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00007034 unsigned srcOut = MRI.createVirtualRegister(TRC);
7035 unsigned destOut = MRI.createVirtualRegister(TRC);
7036 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007037 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
7038 IsThumb1, IsThumb2);
7039 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
7040 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007041 srcIn = srcOut;
7042 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007043 }
7044
7045 // Handle the leftover bytes with LDRB and STRB.
7046 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
7047 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00007048 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007049 unsigned srcOut = MRI.createVirtualRegister(TRC);
7050 unsigned destOut = MRI.createVirtualRegister(TRC);
7051 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007052 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
7053 IsThumb1, IsThumb2);
7054 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
7055 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007056 srcIn = srcOut;
7057 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007058 }
7059 MI->eraseFromParent(); // The instruction is gone now.
7060 return BB;
7061 }
7062
7063 // Expand the pseudo op to a loop.
7064 // thisMBB:
7065 // ...
7066 // movw varEnd, # --> with thumb2
7067 // movt varEnd, #
7068 // ldrcp varEnd, idx --> without thumb2
7069 // fallthrough --> loopMBB
7070 // loopMBB:
7071 // PHI varPhi, varEnd, varLoop
7072 // PHI srcPhi, src, srcLoop
7073 // PHI destPhi, dst, destLoop
7074 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7075 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
7076 // subs varLoop, varPhi, #UnitSize
7077 // bne loopMBB
7078 // fallthrough --> exitMBB
7079 // exitMBB:
7080 // epilogue to handle left-over bytes
7081 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7082 // [destOut] = STRB_POST(scratch, destLoop, 1)
7083 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7084 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7085 MF->insert(It, loopMBB);
7086 MF->insert(It, exitMBB);
7087
7088 // Transfer the remainder of BB and its successor edges to exitMBB.
7089 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007090 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007091 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7092
7093 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007094 unsigned varEnd = MRI.createVirtualRegister(TRC);
7095 if (IsThumb2) {
7096 unsigned Vtmp = varEnd;
7097 if ((LoopSize & 0xFFFF0000) != 0)
7098 Vtmp = MRI.createVirtualRegister(TRC);
7099 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), Vtmp)
7100 .addImm(LoopSize & 0xFFFF));
7101
7102 if ((LoopSize & 0xFFFF0000) != 0)
7103 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
7104 .addReg(Vtmp).addImm(LoopSize >> 16));
7105 } else {
7106 MachineConstantPool *ConstantPool = MF->getConstantPool();
7107 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7108 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7109
7110 // MachineConstantPool wants an explicit alignment.
7111 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
7112 if (Align == 0)
7113 Align = getDataLayout()->getTypeAllocSize(C->getType());
7114 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7115
7116 if (IsThumb1)
7117 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7118 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7119 else
7120 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7121 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7122 }
Manman Rene8735522012-06-01 19:33:18 +00007123 BB->addSuccessor(loopMBB);
7124
7125 // Generate the loop body:
7126 // varPhi = PHI(varLoop, varEnd)
7127 // srcPhi = PHI(srcLoop, src)
7128 // destPhi = PHI(destLoop, dst)
7129 MachineBasicBlock *entryBB = BB;
7130 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007131 unsigned varLoop = MRI.createVirtualRegister(TRC);
7132 unsigned varPhi = MRI.createVirtualRegister(TRC);
7133 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7134 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7135 unsigned destLoop = MRI.createVirtualRegister(TRC);
7136 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007137
7138 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7139 .addReg(varLoop).addMBB(loopMBB)
7140 .addReg(varEnd).addMBB(entryBB);
7141 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7142 .addReg(srcLoop).addMBB(loopMBB)
7143 .addReg(src).addMBB(entryBB);
7144 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7145 .addReg(destLoop).addMBB(loopMBB)
7146 .addReg(dest).addMBB(entryBB);
7147
7148 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7149 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007150 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007151 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7152 IsThumb1, IsThumb2);
7153 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7154 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007155
7156 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007157 if (IsThumb1) {
7158 MachineInstrBuilder MIB =
7159 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7160 MIB = AddDefaultT1CC(MIB);
7161 MIB.addReg(varPhi).addImm(UnitSize);
7162 AddDefaultPred(MIB);
7163 } else {
7164 MachineInstrBuilder MIB =
7165 BuildMI(*BB, BB->end(), dl,
7166 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7167 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7168 MIB->getOperand(5).setReg(ARM::CPSR);
7169 MIB->getOperand(5).setIsDef(true);
7170 }
7171 BuildMI(*BB, BB->end(), dl,
7172 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7173 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007174
7175 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7176 BB->addSuccessor(loopMBB);
7177 BB->addSuccessor(exitMBB);
7178
7179 // Add epilogue to handle BytesLeft.
7180 BB = exitMBB;
7181 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007182
7183 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7184 // [destOut] = STRB_POST(scratch, destLoop, 1)
7185 unsigned srcIn = srcLoop;
7186 unsigned destIn = destLoop;
7187 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007188 unsigned srcOut = MRI.createVirtualRegister(TRC);
7189 unsigned destOut = MRI.createVirtualRegister(TRC);
7190 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007191 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7192 IsThumb1, IsThumb2);
7193 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7194 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007195 srcIn = srcOut;
7196 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007197 }
7198
7199 MI->eraseFromParent(); // The instruction is gone now.
7200 return BB;
7201}
7202
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00007203MachineBasicBlock *
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007204ARMTargetLowering::EmitLowered__chkstk(MachineInstr *MI,
7205 MachineBasicBlock *MBB) const {
7206 const TargetMachine &TM = getTargetMachine();
Eric Christopherd9134482014-08-04 21:25:23 +00007207 const TargetInstrInfo &TII = *TM.getSubtargetImpl()->getInstrInfo();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007208 DebugLoc DL = MI->getDebugLoc();
7209
7210 assert(Subtarget->isTargetWindows() &&
7211 "__chkstk is only supported on Windows");
7212 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
7213
7214 // __chkstk takes the number of words to allocate on the stack in R4, and
7215 // returns the stack adjustment in number of bytes in R4. This will not
7216 // clober any other registers (other than the obvious lr).
7217 //
7218 // Although, technically, IP should be considered a register which may be
7219 // clobbered, the call itself will not touch it. Windows on ARM is a pure
7220 // thumb-2 environment, so there is no interworking required. As a result, we
7221 // do not expect a veneer to be emitted by the linker, clobbering IP.
7222 //
Alp Toker1d099d92014-06-19 19:41:26 +00007223 // Each module receives its own copy of __chkstk, so no import thunk is
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007224 // required, again, ensuring that IP is not clobbered.
7225 //
7226 // Finally, although some linkers may theoretically provide a trampoline for
7227 // out of range calls (which is quite common due to a 32M range limitation of
7228 // branches for Thumb), we can generate the long-call version via
7229 // -mcmodel=large, alleviating the need for the trampoline which may clobber
7230 // IP.
7231
7232 switch (TM.getCodeModel()) {
7233 case CodeModel::Small:
7234 case CodeModel::Medium:
7235 case CodeModel::Default:
7236 case CodeModel::Kernel:
7237 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
7238 .addImm((unsigned)ARMCC::AL).addReg(0)
7239 .addExternalSymbol("__chkstk")
7240 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7241 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7242 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7243 break;
7244 case CodeModel::Large:
7245 case CodeModel::JITDefault: {
7246 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
7247 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
7248
7249 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
7250 .addExternalSymbol("__chkstk");
7251 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
7252 .addImm((unsigned)ARMCC::AL).addReg(0)
7253 .addReg(Reg, RegState::Kill)
7254 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7255 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7256 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7257 break;
7258 }
7259 }
7260
7261 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr),
7262 ARM::SP)
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +00007263 .addReg(ARM::SP).addReg(ARM::R4)));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007264
7265 MI->eraseFromParent();
7266 return MBB;
7267}
7268
7269MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00007270ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00007271 MachineBasicBlock *BB) const {
Eric Christopherd9134482014-08-04 21:25:23 +00007272 const TargetInstrInfo *TII =
7273 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00007274 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00007275 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00007276 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00007277 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00007278 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00007279 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00007280 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00007281 // The Thumb2 pre-indexed stores have the same MI operands, they just
7282 // define them differently in the .td files from the isel patterns, so
7283 // they need pseudos.
7284 case ARM::t2STR_preidx:
7285 MI->setDesc(TII->get(ARM::t2STR_PRE));
7286 return BB;
7287 case ARM::t2STRB_preidx:
7288 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7289 return BB;
7290 case ARM::t2STRH_preidx:
7291 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7292 return BB;
7293
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007294 case ARM::STRi_preidx:
7295 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00007296 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007297 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7298 // Decode the offset.
7299 unsigned Offset = MI->getOperand(4).getImm();
7300 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7301 Offset = ARM_AM::getAM2Offset(Offset);
7302 if (isSub)
7303 Offset = -Offset;
7304
Jim Grosbachf402f692011-08-12 21:02:34 +00007305 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00007306 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007307 .addOperand(MI->getOperand(0)) // Rn_wb
7308 .addOperand(MI->getOperand(1)) // Rt
7309 .addOperand(MI->getOperand(2)) // Rn
7310 .addImm(Offset) // offset (skip GPR==zero_reg)
7311 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00007312 .addOperand(MI->getOperand(6))
7313 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007314 MI->eraseFromParent();
7315 return BB;
7316 }
7317 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00007318 case ARM::STRBr_preidx:
7319 case ARM::STRH_preidx: {
7320 unsigned NewOpc;
7321 switch (MI->getOpcode()) {
7322 default: llvm_unreachable("unexpected opcode!");
7323 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7324 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7325 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7326 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007327 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7328 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7329 MIB.addOperand(MI->getOperand(i));
7330 MI->eraseFromParent();
7331 return BB;
7332 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00007333
Evan Chengbb2af352009-08-12 05:17:19 +00007334 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00007335 // To "insert" a SELECT_CC instruction, we actually have to insert the
7336 // diamond control-flow pattern. The incoming instruction knows the
7337 // destination vreg to set, the condition code register to branch on, the
7338 // true/false values to select between, and a branch opcode to use.
7339 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00007340 MachineFunction::iterator It = BB;
Evan Cheng10043e22007-01-19 07:51:42 +00007341 ++It;
7342
7343 // thisMBB:
7344 // ...
7345 // TrueVal = ...
7346 // cmpTY ccX, r1, r2
7347 // bCC copy1MBB
7348 // fallthrough --> copy0MBB
7349 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00007350 MachineFunction *F = BB->getParent();
7351 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7352 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00007353 F->insert(It, copy0MBB);
7354 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007355
7356 // Transfer the remainder of BB and its successor edges to sinkMBB.
7357 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007358 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007359 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7360
Dan Gohmanf4f04102010-07-06 15:49:48 +00007361 BB->addSuccessor(copy0MBB);
7362 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00007363
Dan Gohman34396292010-07-06 20:24:04 +00007364 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7365 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7366
Evan Cheng10043e22007-01-19 07:51:42 +00007367 // copy0MBB:
7368 // %FalseValue = ...
7369 // # fallthrough to sinkMBB
7370 BB = copy0MBB;
7371
7372 // Update machine-CFG edges
7373 BB->addSuccessor(sinkMBB);
7374
7375 // sinkMBB:
7376 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7377 // ...
7378 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007379 BuildMI(*BB, BB->begin(), dl,
7380 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00007381 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7382 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7383
Dan Gohman34396292010-07-06 20:24:04 +00007384 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00007385 return BB;
7386 }
Evan Chengb972e562009-08-07 00:34:42 +00007387
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007388 case ARM::BCCi64:
7389 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00007390 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007391 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00007392
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007393 // Compare both parts that make up the double comparison separately for
7394 // equality.
7395 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7396
7397 unsigned LHS1 = MI->getOperand(1).getReg();
7398 unsigned LHS2 = MI->getOperand(2).getReg();
7399 if (RHSisZero) {
7400 AddDefaultPred(BuildMI(BB, dl,
7401 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7402 .addReg(LHS1).addImm(0));
7403 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7404 .addReg(LHS2).addImm(0)
7405 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7406 } else {
7407 unsigned RHS1 = MI->getOperand(3).getReg();
7408 unsigned RHS2 = MI->getOperand(4).getReg();
7409 AddDefaultPred(BuildMI(BB, dl,
7410 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7411 .addReg(LHS1).addReg(RHS1));
7412 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7413 .addReg(LHS2).addReg(RHS2)
7414 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7415 }
7416
7417 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7418 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7419 if (MI->getOperand(0).getImm() == ARMCC::NE)
7420 std::swap(destMBB, exitMBB);
7421
7422 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7423 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00007424 if (isThumb2)
7425 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7426 else
7427 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007428
7429 MI->eraseFromParent(); // The pseudo instruction is gone now.
7430 return BB;
7431 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007432
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007433 case ARM::Int_eh_sjlj_setjmp:
7434 case ARM::Int_eh_sjlj_setjmp_nofp:
7435 case ARM::tInt_eh_sjlj_setjmp:
7436 case ARM::t2Int_eh_sjlj_setjmp:
7437 case ARM::t2Int_eh_sjlj_setjmp_nofp:
7438 EmitSjLjDispatchBlock(MI, BB);
7439 return BB;
7440
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007441 case ARM::ABS:
7442 case ARM::t2ABS: {
7443 // To insert an ABS instruction, we have to insert the
7444 // diamond control-flow pattern. The incoming instruction knows the
7445 // source vreg to test against 0, the destination vreg to set,
7446 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00007447 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007448 // It transforms
7449 // V1 = ABS V0
7450 // into
7451 // V2 = MOVS V0
7452 // BCC (branch to SinkBB if V0 >= 0)
7453 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00007454 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007455 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7456 MachineFunction::iterator BBI = BB;
7457 ++BBI;
7458 MachineFunction *Fn = BB->getParent();
7459 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7460 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7461 Fn->insert(BBI, RSBBB);
7462 Fn->insert(BBI, SinkBB);
7463
7464 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7465 unsigned int ABSDstReg = MI->getOperand(0).getReg();
7466 bool isThumb2 = Subtarget->isThumb2();
7467 MachineRegisterInfo &MRI = Fn->getRegInfo();
7468 // In Thumb mode S must not be specified if source register is the SP or
7469 // PC and if destination register is the SP, so restrict register class
Craig Topperc7242e02012-04-20 07:30:17 +00007470 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
7471 (const TargetRegisterClass*)&ARM::rGPRRegClass :
7472 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007473
7474 // Transfer the remainder of BB and its successor edges to sinkMBB.
7475 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007476 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007477 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7478
7479 BB->addSuccessor(RSBBB);
7480 BB->addSuccessor(SinkBB);
7481
7482 // fall through to SinkMBB
7483 RSBBB->addSuccessor(SinkBB);
7484
Manman Rene0763c72012-06-15 21:32:12 +00007485 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00007486 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00007487 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7488 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007489
7490 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00007491 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007492 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7493 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7494
7495 // insert rsbri in RSBBB
7496 // Note: BCC and rsbri will be converted into predicated rsbmi
7497 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00007498 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007499 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Rene0763c72012-06-15 21:32:12 +00007500 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007501 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7502
Andrew Trick3f07c422011-10-18 18:40:53 +00007503 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007504 // reuse ABSDstReg to not change uses of ABS instruction
7505 BuildMI(*SinkBB, SinkBB->begin(), dl,
7506 TII->get(ARM::PHI), ABSDstReg)
7507 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00007508 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007509
7510 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00007511 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007512
7513 // return last added BB
7514 return SinkBB;
7515 }
Manman Rene8735522012-06-01 19:33:18 +00007516 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00007517 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00007518 return EmitStructByval(MI, BB);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007519 case ARM::WIN__CHKSTK:
7520 return EmitLowered__chkstk(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00007521 }
7522}
7523
Evan Chenge6fba772011-08-30 19:09:48 +00007524void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7525 SDNode *Node) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007526 if (!MI->hasPostISelHook()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007527 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
7528 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
7529 return;
7530 }
7531
Evan Cheng7f8e5632011-12-07 07:15:52 +00007532 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00007533 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7534 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7535 // operand is still set to noreg. If needed, set the optional operand's
7536 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00007537 //
Andrew Trick88b24502011-10-18 19:18:52 +00007538 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00007539
Andrew Trick924123a2011-09-21 02:20:46 +00007540 // Rename pseudo opcodes.
7541 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7542 if (NewOpc) {
Eric Christopherd9134482014-08-04 21:25:23 +00007543 const ARMBaseInstrInfo *TII = static_cast<const ARMBaseInstrInfo *>(
7544 getTargetMachine().getSubtargetImpl()->getInstrInfo());
Andrew Trick88b24502011-10-18 19:18:52 +00007545 MCID = &TII->get(NewOpc);
7546
7547 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7548 "converted opcode should be the same except for cc_out");
7549
7550 MI->setDesc(*MCID);
7551
7552 // Add the optional cc_out operand
7553 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00007554 }
Andrew Trick88b24502011-10-18 19:18:52 +00007555 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00007556
7557 // Any ARM instruction that sets the 's' bit should specify an optional
7558 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00007559 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007560 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007561 return;
7562 }
Andrew Trick924123a2011-09-21 02:20:46 +00007563 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7564 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007565 bool definesCPSR = false;
7566 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00007567 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00007568 i != e; ++i) {
7569 const MachineOperand &MO = MI->getOperand(i);
7570 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7571 definesCPSR = true;
7572 if (MO.isDead())
7573 deadCPSR = true;
7574 MI->RemoveOperand(i);
7575 break;
Evan Chenge6fba772011-08-30 19:09:48 +00007576 }
7577 }
Andrew Trick8586e622011-09-20 03:17:40 +00007578 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00007579 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007580 return;
7581 }
7582 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00007583 if (deadCPSR) {
7584 assert(!MI->getOperand(ccOutIdx).getReg() &&
7585 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00007586 return;
Andrew Trick924123a2011-09-21 02:20:46 +00007587 }
Andrew Trick8586e622011-09-20 03:17:40 +00007588
Andrew Trick924123a2011-09-21 02:20:46 +00007589 // If this instruction was defined with an optional CPSR def and its dag node
7590 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007591 MachineOperand &MO = MI->getOperand(ccOutIdx);
7592 MO.setReg(ARM::CPSR);
7593 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00007594}
7595
Evan Cheng10043e22007-01-19 07:51:42 +00007596//===----------------------------------------------------------------------===//
7597// ARM Optimization Hooks
7598//===----------------------------------------------------------------------===//
7599
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007600// Helper function that checks if N is a null or all ones constant.
7601static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7602 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7603 if (!C)
7604 return false;
7605 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7606}
7607
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007608// Return true if N is conditionally 0 or all ones.
7609// Detects these expressions where cc is an i1 value:
7610//
7611// (select cc 0, y) [AllOnes=0]
7612// (select cc y, 0) [AllOnes=0]
7613// (zext cc) [AllOnes=0]
7614// (sext cc) [AllOnes=0/1]
7615// (select cc -1, y) [AllOnes=1]
7616// (select cc y, -1) [AllOnes=1]
7617//
7618// Invert is set when N is the null/all ones constant when CC is false.
7619// OtherOp is set to the alternative value of N.
7620static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7621 SDValue &CC, bool &Invert,
7622 SDValue &OtherOp,
7623 SelectionDAG &DAG) {
7624 switch (N->getOpcode()) {
7625 default: return false;
7626 case ISD::SELECT: {
7627 CC = N->getOperand(0);
7628 SDValue N1 = N->getOperand(1);
7629 SDValue N2 = N->getOperand(2);
7630 if (isZeroOrAllOnes(N1, AllOnes)) {
7631 Invert = false;
7632 OtherOp = N2;
7633 return true;
7634 }
7635 if (isZeroOrAllOnes(N2, AllOnes)) {
7636 Invert = true;
7637 OtherOp = N1;
7638 return true;
7639 }
7640 return false;
7641 }
7642 case ISD::ZERO_EXTEND:
7643 // (zext cc) can never be the all ones value.
7644 if (AllOnes)
7645 return false;
7646 // Fall through.
7647 case ISD::SIGN_EXTEND: {
7648 EVT VT = N->getValueType(0);
7649 CC = N->getOperand(0);
7650 if (CC.getValueType() != MVT::i1)
7651 return false;
7652 Invert = !AllOnes;
7653 if (AllOnes)
7654 // When looking for an AllOnes constant, N is an sext, and the 'other'
7655 // value is 0.
7656 OtherOp = DAG.getConstant(0, VT);
7657 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7658 // When looking for a 0 constant, N can be zext or sext.
7659 OtherOp = DAG.getConstant(1, VT);
7660 else
7661 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7662 return true;
7663 }
7664 }
7665}
7666
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007667// Combine a constant select operand into its use:
7668//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007669// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7670// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7671// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7672// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7673// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007674//
7675// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007676// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007677//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007678// Also recognize sext/zext from i1:
7679//
7680// (add (zext cc), x) -> (select cc (add x, 1), x)
7681// (add (sext cc), x) -> (select cc (add x, -1), x)
7682//
7683// These transformations eventually create predicated instructions.
7684//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007685// @param N The node to transform.
7686// @param Slct The N operand that is a select.
7687// @param OtherOp The other N operand (x above).
7688// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007689// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007690// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00007691static
7692SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007693 TargetLowering::DAGCombinerInfo &DCI,
7694 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00007695 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00007696 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007697 SDValue NonConstantVal;
7698 SDValue CCOp;
7699 bool SwapSelectOps;
7700 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7701 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007702 return SDValue();
7703
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007704 // Slct is now know to be the desired identity constant when CC is true.
7705 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007706 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007707 OtherOp, NonConstantVal);
7708 // Unless SwapSelectOps says CC should be false.
7709 if (SwapSelectOps)
7710 std::swap(TrueVal, FalseVal);
7711
Andrew Trickef9de2a2013-05-25 02:42:55 +00007712 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007713 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00007714}
7715
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007716// Attempt combineSelectAndUse on each operand of a commutative operator N.
7717static
7718SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7719 TargetLowering::DAGCombinerInfo &DCI) {
7720 SDValue N0 = N->getOperand(0);
7721 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007722 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007723 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7724 if (Result.getNode())
7725 return Result;
7726 }
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007727 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007728 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7729 if (Result.getNode())
7730 return Result;
7731 }
7732 return SDValue();
7733}
7734
Eric Christopher1b8b94192011-06-29 21:10:36 +00007735// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00007736// (only after legalization).
7737static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7738 TargetLowering::DAGCombinerInfo &DCI,
7739 const ARMSubtarget *Subtarget) {
7740
7741 // Only perform optimization if after legalize, and if NEON is available. We
7742 // also expected both operands to be BUILD_VECTORs.
7743 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7744 || N0.getOpcode() != ISD::BUILD_VECTOR
7745 || N1.getOpcode() != ISD::BUILD_VECTOR)
7746 return SDValue();
7747
7748 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7749 EVT VT = N->getValueType(0);
7750 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7751 return SDValue();
7752
7753 // Check that the vector operands are of the right form.
7754 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7755 // operands, where N is the size of the formed vector.
7756 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7757 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007758
7759 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00007760 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00007761 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00007762 SDValue Vec = N0->getOperand(0)->getOperand(0);
7763 SDNode *V = Vec.getNode();
7764 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00007765
Eric Christopher1b8b94192011-06-29 21:10:36 +00007766 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007767 // check to see if each of their operands are an EXTRACT_VECTOR with
7768 // the same vector and appropriate index.
7769 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7770 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7771 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00007772
Tanya Lattnere9e67052011-06-14 23:48:48 +00007773 SDValue ExtVec0 = N0->getOperand(i);
7774 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007775
Tanya Lattnere9e67052011-06-14 23:48:48 +00007776 // First operand is the vector, verify its the same.
7777 if (V != ExtVec0->getOperand(0).getNode() ||
7778 V != ExtVec1->getOperand(0).getNode())
7779 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00007780
Tanya Lattnere9e67052011-06-14 23:48:48 +00007781 // Second is the constant, verify its correct.
7782 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7783 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00007784
Tanya Lattnere9e67052011-06-14 23:48:48 +00007785 // For the constant, we want to see all the even or all the odd.
7786 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7787 || C1->getZExtValue() != nextIndex+1)
7788 return SDValue();
7789
7790 // Increment index.
7791 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00007792 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00007793 return SDValue();
7794 }
7795
7796 // Create VPADDL node.
7797 SelectionDAG &DAG = DCI.DAG;
7798 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00007799
7800 // Build operand list.
7801 SmallVector<SDValue, 8> Ops;
7802 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7803 TLI.getPointerTy()));
7804
7805 // Input is the vector.
7806 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007807
Tanya Lattnere9e67052011-06-14 23:48:48 +00007808 // Get widened type and narrowed type.
7809 MVT widenType;
7810 unsigned numElem = VT.getVectorNumElements();
Silviu Barangaa3106e62014-04-03 10:44:27 +00007811
7812 EVT inputLaneType = Vec.getValueType().getVectorElementType();
7813 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00007814 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7815 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7816 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7817 default:
Craig Toppere55c5562012-02-07 02:50:20 +00007818 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00007819 }
7820
Craig Topper48d114b2014-04-26 18:35:24 +00007821 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N), widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00007822 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
7823 return DAG.getNode(ExtOp, SDLoc(N), VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00007824}
7825
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007826static SDValue findMUL_LOHI(SDValue V) {
7827 if (V->getOpcode() == ISD::UMUL_LOHI ||
7828 V->getOpcode() == ISD::SMUL_LOHI)
7829 return V;
7830 return SDValue();
7831}
7832
7833static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7834 TargetLowering::DAGCombinerInfo &DCI,
7835 const ARMSubtarget *Subtarget) {
7836
7837 if (Subtarget->isThumb1Only()) return SDValue();
7838
7839 // Only perform the checks after legalize when the pattern is available.
7840 if (DCI.isBeforeLegalize()) return SDValue();
7841
7842 // Look for multiply add opportunities.
7843 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7844 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7845 // a glue link from the first add to the second add.
7846 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
7847 // a S/UMLAL instruction.
7848 // loAdd UMUL_LOHI
7849 // \ / :lo \ :hi
7850 // \ / \ [no multiline comment]
7851 // ADDC | hiAdd
7852 // \ :glue / /
7853 // \ / /
7854 // ADDE
7855 //
7856 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
7857 SDValue AddcOp0 = AddcNode->getOperand(0);
7858 SDValue AddcOp1 = AddcNode->getOperand(1);
7859
7860 // Check if the two operands are from the same mul_lohi node.
7861 if (AddcOp0.getNode() == AddcOp1.getNode())
7862 return SDValue();
7863
7864 assert(AddcNode->getNumValues() == 2 &&
7865 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00007866 "Expect ADDC with two result values. First: i32");
7867
7868 // Check that we have a glued ADDC node.
7869 if (AddcNode->getValueType(1) != MVT::Glue)
7870 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007871
7872 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
7873 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
7874 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
7875 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
7876 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
7877 return SDValue();
7878
7879 // Look for the glued ADDE.
7880 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00007881 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007882 return SDValue();
7883
7884 // Make sure it is really an ADDE.
7885 if (AddeNode->getOpcode() != ISD::ADDE)
7886 return SDValue();
7887
7888 assert(AddeNode->getNumOperands() == 3 &&
7889 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
7890 "ADDE node has the wrong inputs");
7891
7892 // Check for the triangle shape.
7893 SDValue AddeOp0 = AddeNode->getOperand(0);
7894 SDValue AddeOp1 = AddeNode->getOperand(1);
7895
7896 // Make sure that the ADDE operands are not coming from the same node.
7897 if (AddeOp0.getNode() == AddeOp1.getNode())
7898 return SDValue();
7899
7900 // Find the MUL_LOHI node walking up ADDE's operands.
7901 bool IsLeftOperandMUL = false;
7902 SDValue MULOp = findMUL_LOHI(AddeOp0);
7903 if (MULOp == SDValue())
7904 MULOp = findMUL_LOHI(AddeOp1);
7905 else
7906 IsLeftOperandMUL = true;
7907 if (MULOp == SDValue())
7908 return SDValue();
7909
7910 // Figure out the right opcode.
7911 unsigned Opc = MULOp->getOpcode();
7912 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
7913
7914 // Figure out the high and low input values to the MLAL node.
7915 SDValue* HiMul = &MULOp;
Craig Topper062a2ba2014-04-25 05:30:21 +00007916 SDValue* HiAdd = nullptr;
7917 SDValue* LoMul = nullptr;
7918 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007919
7920 if (IsLeftOperandMUL)
7921 HiAdd = &AddeOp1;
7922 else
7923 HiAdd = &AddeOp0;
7924
7925
7926 if (AddcOp0->getOpcode() == Opc) {
7927 LoMul = &AddcOp0;
7928 LowAdd = &AddcOp1;
7929 }
7930 if (AddcOp1->getOpcode() == Opc) {
7931 LoMul = &AddcOp1;
7932 LowAdd = &AddcOp0;
7933 }
7934
Craig Topper062a2ba2014-04-25 05:30:21 +00007935 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007936 return SDValue();
7937
7938 if (LoMul->getNode() != HiMul->getNode())
7939 return SDValue();
7940
7941 // Create the merged node.
7942 SelectionDAG &DAG = DCI.DAG;
7943
7944 // Build operand list.
7945 SmallVector<SDValue, 8> Ops;
7946 Ops.push_back(LoMul->getOperand(0));
7947 Ops.push_back(LoMul->getOperand(1));
7948 Ops.push_back(*LowAdd);
7949 Ops.push_back(*HiAdd);
7950
Andrew Trickef9de2a2013-05-25 02:42:55 +00007951 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00007952 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007953
7954 // Replace the ADDs' nodes uses by the MLA node's values.
7955 SDValue HiMLALResult(MLALNode.getNode(), 1);
7956 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
7957
7958 SDValue LoMLALResult(MLALNode.getNode(), 0);
7959 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
7960
7961 // Return original node to notify the driver to stop replacing.
7962 SDValue resNode(AddcNode, 0);
7963 return resNode;
7964}
7965
7966/// PerformADDCCombine - Target-specific dag combine transform from
7967/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
7968static SDValue PerformADDCCombine(SDNode *N,
7969 TargetLowering::DAGCombinerInfo &DCI,
7970 const ARMSubtarget *Subtarget) {
7971
7972 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
7973
7974}
7975
Bob Wilson728eb292010-07-29 20:34:14 +00007976/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7977/// operands N0 and N1. This is a helper for PerformADDCombine that is
7978/// called with the default operands, and if that fails, with commuted
7979/// operands.
7980static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007981 TargetLowering::DAGCombinerInfo &DCI,
7982 const ARMSubtarget *Subtarget){
7983
7984 // Attempt to create vpaddl for this add.
7985 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7986 if (Result.getNode())
7987 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00007988
Chris Lattner4147f082009-03-12 06:52:53 +00007989 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007990 if (N0.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00007991 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7992 if (Result.getNode()) return Result;
7993 }
Chris Lattner4147f082009-03-12 06:52:53 +00007994 return SDValue();
7995}
7996
Bob Wilson728eb292010-07-29 20:34:14 +00007997/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7998///
7999static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008000 TargetLowering::DAGCombinerInfo &DCI,
8001 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00008002 SDValue N0 = N->getOperand(0);
8003 SDValue N1 = N->getOperand(1);
8004
8005 // First try with the default operand order.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008006 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008007 if (Result.getNode())
8008 return Result;
8009
8010 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008011 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008012}
8013
Chris Lattner4147f082009-03-12 06:52:53 +00008014/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00008015///
Chris Lattner4147f082009-03-12 06:52:53 +00008016static SDValue PerformSUBCombine(SDNode *N,
8017 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00008018 SDValue N0 = N->getOperand(0);
8019 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00008020
Chris Lattner4147f082009-03-12 06:52:53 +00008021 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008022 if (N1.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008023 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
8024 if (Result.getNode()) return Result;
8025 }
Bob Wilson7117a912009-03-20 22:42:55 +00008026
Chris Lattner4147f082009-03-12 06:52:53 +00008027 return SDValue();
8028}
8029
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008030/// PerformVMULCombine
8031/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
8032/// special multiplier accumulator forwarding.
8033/// vmul d3, d0, d2
8034/// vmla d3, d1, d2
8035/// is faster than
8036/// vadd d3, d0, d1
8037/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00008038// However, for (A + B) * (A + B),
8039// vadd d2, d0, d1
8040// vmul d3, d0, d2
8041// vmla d3, d1, d2
8042// is slower than
8043// vadd d2, d0, d1
8044// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008045static SDValue PerformVMULCombine(SDNode *N,
8046 TargetLowering::DAGCombinerInfo &DCI,
8047 const ARMSubtarget *Subtarget) {
8048 if (!Subtarget->hasVMLxForwarding())
8049 return SDValue();
8050
8051 SelectionDAG &DAG = DCI.DAG;
8052 SDValue N0 = N->getOperand(0);
8053 SDValue N1 = N->getOperand(1);
8054 unsigned Opcode = N0.getOpcode();
8055 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8056 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00008057 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008058 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8059 Opcode != ISD::FADD && Opcode != ISD::FSUB)
8060 return SDValue();
8061 std::swap(N0, N1);
8062 }
8063
Weiming Zhao2052f482013-09-25 23:12:06 +00008064 if (N0 == N1)
8065 return SDValue();
8066
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008067 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008068 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008069 SDValue N00 = N0->getOperand(0);
8070 SDValue N01 = N0->getOperand(1);
8071 return DAG.getNode(Opcode, DL, VT,
8072 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
8073 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
8074}
8075
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008076static SDValue PerformMULCombine(SDNode *N,
8077 TargetLowering::DAGCombinerInfo &DCI,
8078 const ARMSubtarget *Subtarget) {
8079 SelectionDAG &DAG = DCI.DAG;
8080
8081 if (Subtarget->isThumb1Only())
8082 return SDValue();
8083
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008084 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8085 return SDValue();
8086
8087 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008088 if (VT.is64BitVector() || VT.is128BitVector())
8089 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008090 if (VT != MVT::i32)
8091 return SDValue();
8092
8093 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8094 if (!C)
8095 return SDValue();
8096
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008097 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008098 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008099
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008100 ShiftAmt = ShiftAmt & (32 - 1);
8101 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008102 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008103
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008104 SDValue Res;
8105 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008106
8107 if (MulAmt >= 0) {
8108 if (isPowerOf2_32(MulAmt - 1)) {
8109 // (mul x, 2^N + 1) => (add (shl x, N), x)
8110 Res = DAG.getNode(ISD::ADD, DL, VT,
8111 V,
8112 DAG.getNode(ISD::SHL, DL, VT,
8113 V,
8114 DAG.getConstant(Log2_32(MulAmt - 1),
8115 MVT::i32)));
8116 } else if (isPowerOf2_32(MulAmt + 1)) {
8117 // (mul x, 2^N - 1) => (sub (shl x, N), x)
8118 Res = DAG.getNode(ISD::SUB, DL, VT,
8119 DAG.getNode(ISD::SHL, DL, VT,
8120 V,
8121 DAG.getConstant(Log2_32(MulAmt + 1),
8122 MVT::i32)),
8123 V);
8124 } else
8125 return SDValue();
8126 } else {
8127 uint64_t MulAmtAbs = -MulAmt;
8128 if (isPowerOf2_32(MulAmtAbs + 1)) {
8129 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8130 Res = DAG.getNode(ISD::SUB, DL, VT,
8131 V,
8132 DAG.getNode(ISD::SHL, DL, VT,
8133 V,
8134 DAG.getConstant(Log2_32(MulAmtAbs + 1),
8135 MVT::i32)));
8136 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8137 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8138 Res = DAG.getNode(ISD::ADD, DL, VT,
8139 V,
8140 DAG.getNode(ISD::SHL, DL, VT,
8141 V,
8142 DAG.getConstant(Log2_32(MulAmtAbs-1),
8143 MVT::i32)));
8144 Res = DAG.getNode(ISD::SUB, DL, VT,
8145 DAG.getConstant(0, MVT::i32),Res);
8146
8147 } else
8148 return SDValue();
8149 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008150
8151 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008152 Res = DAG.getNode(ISD::SHL, DL, VT,
8153 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008154
8155 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008156 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008157 return SDValue();
8158}
8159
Owen Anderson30c48922010-11-05 19:27:46 +00008160static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00008161 TargetLowering::DAGCombinerInfo &DCI,
8162 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00008163
Owen Anderson30c48922010-11-05 19:27:46 +00008164 // Attempt to use immediate-form VBIC
8165 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008166 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00008167 EVT VT = N->getValueType(0);
8168 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008169
Tanya Lattner266792a2011-04-07 15:24:20 +00008170 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8171 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008172
Owen Anderson30c48922010-11-05 19:27:46 +00008173 APInt SplatBits, SplatUndef;
8174 unsigned SplatBitSize;
8175 bool HasAnyUndefs;
8176 if (BVN &&
8177 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8178 if (SplatBitSize <= 64) {
8179 EVT VbicVT;
8180 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8181 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00008182 DAG, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008183 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00008184 if (Val.getNode()) {
8185 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008186 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00008187 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008188 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00008189 }
8190 }
8191 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008192
Evan Chenge87681c2012-02-23 01:19:06 +00008193 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008194 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8195 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8196 if (Result.getNode())
8197 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008198 }
8199
Owen Anderson30c48922010-11-05 19:27:46 +00008200 return SDValue();
8201}
8202
Jim Grosbach11013ed2010-07-16 23:05:05 +00008203/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8204static SDValue PerformORCombine(SDNode *N,
8205 TargetLowering::DAGCombinerInfo &DCI,
8206 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008207 // Attempt to use immediate-form VORR
8208 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008209 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008210 EVT VT = N->getValueType(0);
8211 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008212
Tanya Lattner266792a2011-04-07 15:24:20 +00008213 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8214 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008215
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008216 APInt SplatBits, SplatUndef;
8217 unsigned SplatBitSize;
8218 bool HasAnyUndefs;
8219 if (BVN && Subtarget->hasNEON() &&
8220 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8221 if (SplatBitSize <= 64) {
8222 EVT VorrVT;
8223 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8224 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00008225 DAG, VorrVT, VT.is128BitVector(),
8226 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008227 if (Val.getNode()) {
8228 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008229 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008230 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008231 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008232 }
8233 }
8234 }
8235
Evan Chenge87681c2012-02-23 01:19:06 +00008236 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008237 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8238 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8239 if (Result.getNode())
8240 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008241 }
8242
Nadav Rotem3a94c542012-08-13 18:52:44 +00008243 // The code below optimizes (or (and X, Y), Z).
8244 // The AND operand needs to have a single user to make these optimizations
8245 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008246 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00008247 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008248 return SDValue();
8249 SDValue N1 = N->getOperand(1);
8250
8251 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8252 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8253 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8254 APInt SplatUndef;
8255 unsigned SplatBitSize;
8256 bool HasAnyUndefs;
8257
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008258 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008259 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008260 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8261 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008262 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008263 HasAnyUndefs) && !HasAnyUndefs) {
8264 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8265 HasAnyUndefs) && !HasAnyUndefs) {
8266 // Ensure that the bit width of the constants are the same and that
8267 // the splat arguments are logical inverses as per the pattern we
8268 // are trying to simplify.
8269 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
8270 SplatBits0 == ~SplatBits1) {
8271 // Canonicalize the vector type to make instruction selection
8272 // simpler.
8273 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8274 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8275 N0->getOperand(1),
8276 N0->getOperand(0),
8277 N1->getOperand(0));
8278 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8279 }
8280 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008281 }
8282 }
8283
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008284 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8285 // reasonable.
8286
Jim Grosbach11013ed2010-07-16 23:05:05 +00008287 // BFI is only available on V6T2+
8288 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8289 return SDValue();
8290
Andrew Trickef9de2a2013-05-25 02:42:55 +00008291 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008292 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008293 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008294 //
8295 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008296 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008297 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008298 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008299 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008300 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008301
Jim Grosbach11013ed2010-07-16 23:05:05 +00008302 if (VT != MVT::i32)
8303 return SDValue();
8304
Evan Cheng2e51bb42010-12-13 20:32:54 +00008305 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008306
Jim Grosbach11013ed2010-07-16 23:05:05 +00008307 // The value and the mask need to be constants so we can verify this is
8308 // actually a bitfield set. If the mask is 0xffff, we can do better
8309 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00008310 SDValue MaskOp = N0.getOperand(1);
8311 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8312 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008313 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008314 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008315 if (Mask == 0xffff)
8316 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008317 SDValue Res;
8318 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008319 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8320 if (N1C) {
8321 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00008322 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008323 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008324
Evan Cheng34345752010-12-11 04:11:38 +00008325 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008326 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00008327
Evan Cheng2e51bb42010-12-13 20:32:54 +00008328 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Cheng34345752010-12-11 04:11:38 +00008329 DAG.getConstant(Val, MVT::i32),
8330 DAG.getConstant(Mask, MVT::i32));
8331
8332 // Do not add new nodes to DAG combiner worklist.
8333 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008334 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00008335 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008336 } else if (N1.getOpcode() == ISD::AND) {
8337 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008338 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8339 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008340 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008341 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008342
Eric Christopherd5530962011-03-26 01:21:03 +00008343 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8344 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008345 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008346 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008347 // The pack halfword instruction works better for masks that fit it,
8348 // so use that when it's available.
8349 if (Subtarget->hasT2ExtractPack() &&
8350 (Mask == 0xffff || Mask == 0xffff0000))
8351 return SDValue();
8352 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008353 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008354 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopherd5530962011-03-26 01:21:03 +00008355 DAG.getConstant(amt, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008356 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008357 DAG.getConstant(Mask, MVT::i32));
8358 // Do not add new nodes to DAG combiner worklist.
8359 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008360 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008361 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008362 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008363 // The pack halfword instruction works better for masks that fit it,
8364 // so use that when it's available.
8365 if (Subtarget->hasT2ExtractPack() &&
8366 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8367 return SDValue();
8368 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008369 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008370 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008371 DAG.getConstant(lsb, MVT::i32));
8372 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopherd5530962011-03-26 01:21:03 +00008373 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008374 // Do not add new nodes to DAG combiner worklist.
8375 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008376 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008377 }
8378 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008379
Evan Cheng2e51bb42010-12-13 20:32:54 +00008380 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8381 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8382 ARM::isBitFieldInvertedMask(~Mask)) {
8383 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8384 // where lsb(mask) == #shamt and masked bits of B are known zero.
8385 SDValue ShAmt = N00.getOperand(1);
8386 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008387 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008388 if (ShAmtC != LSB)
8389 return SDValue();
8390
8391 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
8392 DAG.getConstant(~Mask, MVT::i32));
8393
8394 // Do not add new nodes to DAG combiner worklist.
8395 DCI.CombineTo(N, Res, false);
8396 }
8397
Jim Grosbach11013ed2010-07-16 23:05:05 +00008398 return SDValue();
8399}
8400
Evan Chenge87681c2012-02-23 01:19:06 +00008401static SDValue PerformXORCombine(SDNode *N,
8402 TargetLowering::DAGCombinerInfo &DCI,
8403 const ARMSubtarget *Subtarget) {
8404 EVT VT = N->getValueType(0);
8405 SelectionDAG &DAG = DCI.DAG;
8406
8407 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8408 return SDValue();
8409
8410 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008411 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8412 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8413 if (Result.getNode())
8414 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008415 }
8416
8417 return SDValue();
8418}
8419
Evan Cheng6d02d902011-06-15 01:12:31 +00008420/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8421/// the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00008422static SDValue PerformBFICombine(SDNode *N,
8423 TargetLowering::DAGCombinerInfo &DCI) {
8424 SDValue N1 = N->getOperand(1);
8425 if (N1.getOpcode() == ISD::AND) {
8426 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8427 if (!N11C)
8428 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008429 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008430 unsigned LSB = countTrailingZeros(~InvMask);
8431 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Evan Cheng6d02d902011-06-15 01:12:31 +00008432 unsigned Mask = (1 << Width)-1;
Evan Chengc1778132010-12-14 03:22:07 +00008433 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008434 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008435 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00008436 N->getOperand(0), N1.getOperand(0),
8437 N->getOperand(2));
8438 }
8439 return SDValue();
8440}
8441
Bob Wilson22806742010-09-22 22:09:21 +00008442/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8443/// ARMISD::VMOVRRD.
8444static SDValue PerformVMOVRRDCombine(SDNode *N,
8445 TargetLowering::DAGCombinerInfo &DCI) {
8446 // vmovrrd(vmovdrr x, y) -> x,y
8447 SDValue InDouble = N->getOperand(0);
8448 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
8449 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008450
8451 // vmovrrd(load f64) -> (load i32), (load i32)
8452 SDNode *InNode = InDouble.getNode();
8453 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8454 InNode->getValueType(0) == MVT::f64 &&
8455 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8456 !cast<LoadSDNode>(InNode)->isVolatile()) {
8457 // TODO: Should this be done for non-FrameIndex operands?
8458 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8459
8460 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008461 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008462 SDValue BasePtr = LD->getBasePtr();
8463 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8464 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008465 LD->isNonTemporal(), LD->isInvariant(),
8466 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008467
8468 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8469 DAG.getConstant(4, MVT::i32));
8470 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8471 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008472 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008473 std::min(4U, LD->getAlignment() / 2));
8474
8475 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
Christian Pirker762b2c62014-06-01 09:30:52 +00008476 if (DCI.DAG.getTargetLoweringInfo().isBigEndian())
8477 std::swap (NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008478 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008479 return Result;
8480 }
8481
Bob Wilson22806742010-09-22 22:09:21 +00008482 return SDValue();
8483}
8484
8485/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8486/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8487static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8488 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8489 SDValue Op0 = N->getOperand(0);
8490 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00008491 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008492 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008493 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008494 Op1 = Op1.getOperand(0);
8495 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8496 Op0.getNode() == Op1.getNode() &&
8497 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008498 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00008499 N->getValueType(0), Op0.getOperand(0));
8500 return SDValue();
8501}
8502
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008503/// PerformSTORECombine - Target-specific dag combine xforms for
8504/// ISD::STORE.
8505static SDValue PerformSTORECombine(SDNode *N,
8506 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008507 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008508 if (St->isVolatile())
8509 return SDValue();
8510
Andrew Trickbc325162012-07-18 18:34:24 +00008511 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosiere0e38f62012-04-09 20:32:02 +00008512 // pack all of the elements in one place. Next, store to memory in fewer
8513 // chunks.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008514 SDValue StVal = St->getValue();
Chad Rosiere0e38f62012-04-09 20:32:02 +00008515 EVT VT = StVal.getValueType();
8516 if (St->isTruncatingStore() && VT.isVector()) {
8517 SelectionDAG &DAG = DCI.DAG;
8518 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8519 EVT StVT = St->getMemoryVT();
8520 unsigned NumElems = VT.getVectorNumElements();
8521 assert(StVT != VT && "Cannot truncate to the same type");
8522 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
8523 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
8524
8525 // From, To sizes and ElemCount must be pow of two
8526 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
8527
8528 // We are going to use the original vector elt for storing.
8529 // Accumulated smaller vector elements must be a multiple of the store size.
8530 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
8531
8532 unsigned SizeRatio = FromEltSz / ToEltSz;
8533 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
8534
8535 // Create a type on which we perform the shuffle.
8536 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
8537 NumElems*SizeRatio);
8538 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
8539
Andrew Trickef9de2a2013-05-25 02:42:55 +00008540 SDLoc DL(St);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008541 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
8542 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Christian Pirker2cc1cf02014-06-16 09:17:30 +00008543 for (unsigned i = 0; i < NumElems; ++i)
8544 ShuffleVec[i] = TLI.isBigEndian() ? (i+1) * SizeRatio - 1 : i * SizeRatio;
Chad Rosiere0e38f62012-04-09 20:32:02 +00008545
8546 // Can't shuffle using an illegal type.
8547 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
8548
8549 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
8550 DAG.getUNDEF(WideVec.getValueType()),
8551 ShuffleVec.data());
8552 // At this point all of the data is stored at the bottom of the
8553 // register. We now need to save it to mem.
8554
8555 // Find the largest store unit
8556 MVT StoreType = MVT::i8;
8557 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
8558 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
8559 MVT Tp = (MVT::SimpleValueType)tp;
8560 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
8561 StoreType = Tp;
8562 }
8563 // Didn't find a legal store type.
8564 if (!TLI.isTypeLegal(StoreType))
8565 return SDValue();
8566
8567 // Bitcast the original vector into a vector of store-size units
8568 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
8569 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
8570 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
8571 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
8572 SmallVector<SDValue, 8> Chains;
8573 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
8574 TLI.getPointerTy());
8575 SDValue BasePtr = St->getBasePtr();
8576
8577 // Perform one or more big stores into memory.
8578 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
8579 for (unsigned I = 0; I < E; I++) {
8580 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
8581 StoreType, ShuffWide,
8582 DAG.getIntPtrConstant(I));
8583 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
8584 St->getPointerInfo(), St->isVolatile(),
8585 St->isNonTemporal(), St->getAlignment());
8586 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
8587 Increment);
8588 Chains.push_back(Ch);
8589 }
Craig Topper48d114b2014-04-26 18:35:24 +00008590 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008591 }
8592
8593 if (!ISD::isNormalStore(St))
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008594 return SDValue();
8595
Chad Rosier99cbde92012-04-09 19:38:15 +00008596 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
8597 // ARM stores of arguments in the same cache line.
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008598 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier99cbde92012-04-09 19:38:15 +00008599 StVal.getNode()->hasOneUse()) {
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008600 SelectionDAG &DAG = DCI.DAG;
Christian Pirkerb5728192014-05-08 14:06:24 +00008601 bool isBigEndian = DAG.getTargetLoweringInfo().isBigEndian();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008602 SDLoc DL(St);
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008603 SDValue BasePtr = St->getBasePtr();
8604 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
Christian Pirkerb5728192014-05-08 14:06:24 +00008605 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
8606 BasePtr, St->getPointerInfo(), St->isVolatile(),
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008607 St->isNonTemporal(), St->getAlignment());
8608
8609 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8610 DAG.getConstant(4, MVT::i32));
Christian Pirkerb5728192014-05-08 14:06:24 +00008611 return DAG.getStore(NewST1.getValue(0), DL,
8612 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008613 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
8614 St->isNonTemporal(),
8615 std::min(4U, St->getAlignment() / 2));
8616 }
8617
8618 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008619 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8620 return SDValue();
8621
Chad Rosier99cbde92012-04-09 19:38:15 +00008622 // Bitcast an i64 store extracted from a vector to f64.
8623 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008624 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008625 SDLoc dl(StVal);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008626 SDValue IntVec = StVal.getOperand(0);
8627 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8628 IntVec.getValueType().getVectorNumElements());
8629 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
8630 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
8631 Vec, StVal.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008632 dl = SDLoc(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008633 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
8634 // Make the DAGCombiner fold the bitcasts.
8635 DCI.AddToWorklist(Vec.getNode());
8636 DCI.AddToWorklist(ExtElt.getNode());
8637 DCI.AddToWorklist(V.getNode());
8638 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
8639 St->getPointerInfo(), St->isVolatile(),
8640 St->isNonTemporal(), St->getAlignment(),
Hal Finkelcc39b672014-07-24 12:16:19 +00008641 St->getAAInfo());
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008642}
8643
8644/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8645/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8646/// i64 vector to have f64 elements, since the value can then be loaded
8647/// directly into a VFP register.
8648static bool hasNormalLoadOperand(SDNode *N) {
8649 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8650 for (unsigned i = 0; i < NumElts; ++i) {
8651 SDNode *Elt = N->getOperand(i).getNode();
8652 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8653 return true;
8654 }
8655 return false;
8656}
8657
Bob Wilsoncb6db982010-09-17 22:59:05 +00008658/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8659/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008660static SDValue PerformBUILD_VECTORCombine(SDNode *N,
8661 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilsoncb6db982010-09-17 22:59:05 +00008662 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8663 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8664 // into a pair of GPRs, which is fine when the value is used as a scalar,
8665 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008666 SelectionDAG &DAG = DCI.DAG;
8667 if (N->getNumOperands() == 2) {
8668 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8669 if (RV.getNode())
8670 return RV;
8671 }
Bob Wilsoncb6db982010-09-17 22:59:05 +00008672
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008673 // Load i64 elements as f64 values so that type legalization does not split
8674 // them up into i32 values.
8675 EVT VT = N->getValueType(0);
8676 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8677 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008678 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008679 SmallVector<SDValue, 8> Ops;
8680 unsigned NumElts = VT.getVectorNumElements();
8681 for (unsigned i = 0; i < NumElts; ++i) {
8682 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8683 Ops.push_back(V);
8684 // Make the DAGCombiner fold the bitcast.
8685 DCI.AddToWorklist(V.getNode());
8686 }
8687 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00008688 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008689 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8690}
8691
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008692/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
8693static SDValue
8694PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8695 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
8696 // At that time, we may have inserted bitcasts from integer to float.
8697 // If these bitcasts have survived DAGCombine, change the lowering of this
8698 // BUILD_VECTOR in something more vector friendly, i.e., that does not
8699 // force to use floating point types.
8700
8701 // Make sure we can change the type of the vector.
8702 // This is possible iff:
8703 // 1. The vector is only used in a bitcast to a integer type. I.e.,
8704 // 1.1. Vector is used only once.
8705 // 1.2. Use is a bit convert to an integer type.
8706 // 2. The size of its operands are 32-bits (64-bits are not legal).
8707 EVT VT = N->getValueType(0);
8708 EVT EltVT = VT.getVectorElementType();
8709
8710 // Check 1.1. and 2.
8711 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
8712 return SDValue();
8713
8714 // By construction, the input type must be float.
8715 assert(EltVT == MVT::f32 && "Unexpected type!");
8716
8717 // Check 1.2.
8718 SDNode *Use = *N->use_begin();
8719 if (Use->getOpcode() != ISD::BITCAST ||
8720 Use->getValueType(0).isFloatingPoint())
8721 return SDValue();
8722
8723 // Check profitability.
8724 // Model is, if more than half of the relevant operands are bitcast from
8725 // i32, turn the build_vector into a sequence of insert_vector_elt.
8726 // Relevant operands are everything that is not statically
8727 // (i.e., at compile time) bitcasted.
8728 unsigned NumOfBitCastedElts = 0;
8729 unsigned NumElts = VT.getVectorNumElements();
8730 unsigned NumOfRelevantElts = NumElts;
8731 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
8732 SDValue Elt = N->getOperand(Idx);
8733 if (Elt->getOpcode() == ISD::BITCAST) {
8734 // Assume only bit cast to i32 will go away.
8735 if (Elt->getOperand(0).getValueType() == MVT::i32)
8736 ++NumOfBitCastedElts;
8737 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt))
8738 // Constants are statically casted, thus do not count them as
8739 // relevant operands.
8740 --NumOfRelevantElts;
8741 }
8742
8743 // Check if more than half of the elements require a non-free bitcast.
8744 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
8745 return SDValue();
8746
8747 SelectionDAG &DAG = DCI.DAG;
8748 // Create the new vector type.
8749 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
8750 // Check if the type is legal.
8751 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8752 if (!TLI.isTypeLegal(VecVT))
8753 return SDValue();
8754
8755 // Combine:
8756 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
8757 // => BITCAST INSERT_VECTOR_ELT
8758 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
8759 // (BITCAST EN), N.
8760 SDValue Vec = DAG.getUNDEF(VecVT);
8761 SDLoc dl(N);
8762 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
8763 SDValue V = N->getOperand(Idx);
8764 if (V.getOpcode() == ISD::UNDEF)
8765 continue;
8766 if (V.getOpcode() == ISD::BITCAST &&
8767 V->getOperand(0).getValueType() == MVT::i32)
8768 // Fold obvious case.
8769 V = V.getOperand(0);
8770 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00008771 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008772 // Make the DAGCombiner fold the bitcasts.
8773 DCI.AddToWorklist(V.getNode());
8774 }
8775 SDValue LaneIdx = DAG.getConstant(Idx, MVT::i32);
8776 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
8777 }
8778 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
8779 // Make the DAGCombiner fold the bitcasts.
8780 DCI.AddToWorklist(Vec.getNode());
8781 return Vec;
8782}
8783
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008784/// PerformInsertEltCombine - Target-specific dag combine xforms for
8785/// ISD::INSERT_VECTOR_ELT.
8786static SDValue PerformInsertEltCombine(SDNode *N,
8787 TargetLowering::DAGCombinerInfo &DCI) {
8788 // Bitcast an i64 load inserted into a vector to f64.
8789 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8790 EVT VT = N->getValueType(0);
8791 SDNode *Elt = N->getOperand(1).getNode();
8792 if (VT.getVectorElementType() != MVT::i64 ||
8793 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8794 return SDValue();
8795
8796 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008797 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008798 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8799 VT.getVectorNumElements());
8800 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8801 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8802 // Make the DAGCombiner fold the bitcasts.
8803 DCI.AddToWorklist(Vec.getNode());
8804 DCI.AddToWorklist(V.getNode());
8805 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8806 Vec, V, N->getOperand(2));
8807 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00008808}
8809
Bob Wilsonc7334a12010-10-27 20:38:28 +00008810/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8811/// ISD::VECTOR_SHUFFLE.
8812static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8813 // The LLVM shufflevector instruction does not require the shuffle mask
8814 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8815 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8816 // operands do not match the mask length, they are extended by concatenating
8817 // them with undef vectors. That is probably the right thing for other
8818 // targets, but for NEON it is better to concatenate two double-register
8819 // size vector operands into a single quad-register size vector. Do that
8820 // transformation here:
8821 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8822 // shuffle(concat(v1, v2), undef)
8823 SDValue Op0 = N->getOperand(0);
8824 SDValue Op1 = N->getOperand(1);
8825 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8826 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8827 Op0.getNumOperands() != 2 ||
8828 Op1.getNumOperands() != 2)
8829 return SDValue();
8830 SDValue Concat0Op1 = Op0.getOperand(1);
8831 SDValue Concat1Op1 = Op1.getOperand(1);
8832 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8833 Concat1Op1.getOpcode() != ISD::UNDEF)
8834 return SDValue();
8835 // Skip the transformation if any of the types are illegal.
8836 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8837 EVT VT = N->getValueType(0);
8838 if (!TLI.isTypeLegal(VT) ||
8839 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8840 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8841 return SDValue();
8842
Andrew Trickef9de2a2013-05-25 02:42:55 +00008843 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008844 Op0.getOperand(0), Op1.getOperand(0));
8845 // Translate the shuffle mask.
8846 SmallVector<int, 16> NewMask;
8847 unsigned NumElts = VT.getVectorNumElements();
8848 unsigned HalfElts = NumElts/2;
8849 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8850 for (unsigned n = 0; n < NumElts; ++n) {
8851 int MaskElt = SVN->getMaskElt(n);
8852 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00008853 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00008854 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00008855 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00008856 NewElt = HalfElts + MaskElt - NumElts;
8857 NewMask.push_back(NewElt);
8858 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00008859 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008860 DAG.getUNDEF(VT), NewMask.data());
8861}
8862
Bob Wilson06fce872011-02-07 17:43:21 +00008863/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
8864/// NEON load/store intrinsics to merge base address updates.
8865static SDValue CombineBaseUpdate(SDNode *N,
8866 TargetLowering::DAGCombinerInfo &DCI) {
8867 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8868 return SDValue();
8869
8870 SelectionDAG &DAG = DCI.DAG;
8871 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8872 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
8873 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
8874 SDValue Addr = N->getOperand(AddrOpIdx);
8875
8876 // Search for a use of the address operand that is an increment.
8877 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8878 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8879 SDNode *User = *UI;
8880 if (User->getOpcode() != ISD::ADD ||
8881 UI.getUse().getResNo() != Addr.getResNo())
8882 continue;
8883
8884 // Check that the add is independent of the load/store. Otherwise, folding
8885 // it would create a cycle.
8886 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8887 continue;
8888
8889 // Find the new opcode for the updating load/store.
8890 bool isLoad = true;
8891 bool isLaneOp = false;
8892 unsigned NewOpc = 0;
8893 unsigned NumVecs = 0;
8894 if (isIntrinsic) {
8895 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8896 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00008897 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008898 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8899 NumVecs = 1; break;
8900 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8901 NumVecs = 2; break;
8902 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8903 NumVecs = 3; break;
8904 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8905 NumVecs = 4; break;
8906 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8907 NumVecs = 2; isLaneOp = true; break;
8908 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8909 NumVecs = 3; isLaneOp = true; break;
8910 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8911 NumVecs = 4; isLaneOp = true; break;
8912 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
8913 NumVecs = 1; isLoad = false; break;
8914 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
8915 NumVecs = 2; isLoad = false; break;
8916 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8917 NumVecs = 3; isLoad = false; break;
8918 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8919 NumVecs = 4; isLoad = false; break;
8920 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8921 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8922 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8923 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8924 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8925 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8926 }
8927 } else {
8928 isLaneOp = true;
8929 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00008930 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008931 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8932 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8933 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8934 }
8935 }
8936
8937 // Find the size of memory referenced by the load/store.
8938 EVT VecTy;
8939 if (isLoad)
8940 VecTy = N->getValueType(0);
Owen Anderson77aa2662011-04-05 21:48:57 +00008941 else
Bob Wilson06fce872011-02-07 17:43:21 +00008942 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8943 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8944 if (isLaneOp)
8945 NumBytes /= VecTy.getVectorNumElements();
8946
8947 // If the increment is a constant, it must match the memory ref size.
8948 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8949 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8950 uint64_t IncVal = CInc->getZExtValue();
8951 if (IncVal != NumBytes)
8952 continue;
8953 } else if (NumBytes >= 3 * 16) {
8954 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8955 // separate instructions that make it harder to use a non-constant update.
8956 continue;
8957 }
8958
8959 // Create the new updating load/store node.
8960 EVT Tys[6];
8961 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8962 unsigned n;
8963 for (n = 0; n < NumResultVecs; ++n)
8964 Tys[n] = VecTy;
8965 Tys[n++] = MVT::i32;
8966 Tys[n] = MVT::Other;
Craig Topperabb4ac72014-04-16 06:10:51 +00008967 SDVTList SDTys = DAG.getVTList(ArrayRef<EVT>(Tys, NumResultVecs+2));
Bob Wilson06fce872011-02-07 17:43:21 +00008968 SmallVector<SDValue, 8> Ops;
8969 Ops.push_back(N->getOperand(0)); // incoming chain
8970 Ops.push_back(N->getOperand(AddrOpIdx));
8971 Ops.push_back(Inc);
8972 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8973 Ops.push_back(N->getOperand(i));
8974 }
8975 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008976 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, SDLoc(N), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00008977 Ops, MemInt->getMemoryVT(),
Bob Wilson06fce872011-02-07 17:43:21 +00008978 MemInt->getMemOperand());
8979
8980 // Update the uses.
8981 std::vector<SDValue> NewResults;
8982 for (unsigned i = 0; i < NumResultVecs; ++i) {
8983 NewResults.push_back(SDValue(UpdN.getNode(), i));
8984 }
8985 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8986 DCI.CombineTo(N, NewResults);
8987 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8988
8989 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00008990 }
Bob Wilson06fce872011-02-07 17:43:21 +00008991 return SDValue();
8992}
8993
Bob Wilson2d790df2010-11-28 06:51:26 +00008994/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8995/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8996/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8997/// return true.
8998static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8999 SelectionDAG &DAG = DCI.DAG;
9000 EVT VT = N->getValueType(0);
9001 // vldN-dup instructions only support 64-bit vectors for N > 1.
9002 if (!VT.is64BitVector())
9003 return false;
9004
9005 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
9006 SDNode *VLD = N->getOperand(0).getNode();
9007 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
9008 return false;
9009 unsigned NumVecs = 0;
9010 unsigned NewOpc = 0;
9011 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
9012 if (IntNo == Intrinsic::arm_neon_vld2lane) {
9013 NumVecs = 2;
9014 NewOpc = ARMISD::VLD2DUP;
9015 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
9016 NumVecs = 3;
9017 NewOpc = ARMISD::VLD3DUP;
9018 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
9019 NumVecs = 4;
9020 NewOpc = ARMISD::VLD4DUP;
9021 } else {
9022 return false;
9023 }
9024
9025 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
9026 // numbers match the load.
9027 unsigned VLDLaneNo =
9028 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
9029 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9030 UI != UE; ++UI) {
9031 // Ignore uses of the chain result.
9032 if (UI.getUse().getResNo() == NumVecs)
9033 continue;
9034 SDNode *User = *UI;
9035 if (User->getOpcode() != ARMISD::VDUPLANE ||
9036 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
9037 return false;
9038 }
9039
9040 // Create the vldN-dup node.
9041 EVT Tys[5];
9042 unsigned n;
9043 for (n = 0; n < NumVecs; ++n)
9044 Tys[n] = VT;
9045 Tys[n] = MVT::Other;
Craig Topperabb4ac72014-04-16 06:10:51 +00009046 SDVTList SDTys = DAG.getVTList(ArrayRef<EVT>(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00009047 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
9048 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009049 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00009050 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00009051 VLDMemInt->getMemOperand());
9052
9053 // Update the uses.
9054 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9055 UI != UE; ++UI) {
9056 unsigned ResNo = UI.getUse().getResNo();
9057 // Ignore uses of the chain result.
9058 if (ResNo == NumVecs)
9059 continue;
9060 SDNode *User = *UI;
9061 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
9062 }
9063
9064 // Now the vldN-lane intrinsic is dead except for its chain result.
9065 // Update uses of the chain.
9066 std::vector<SDValue> VLDDupResults;
9067 for (unsigned n = 0; n < NumVecs; ++n)
9068 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
9069 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
9070 DCI.CombineTo(VLD, VLDDupResults);
9071
9072 return true;
9073}
9074
Bob Wilson103a0dc2010-07-14 01:22:12 +00009075/// PerformVDUPLANECombine - Target-specific dag combine xforms for
9076/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00009077static SDValue PerformVDUPLANECombine(SDNode *N,
9078 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00009079 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009080
Bob Wilson2d790df2010-11-28 06:51:26 +00009081 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
9082 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
9083 if (CombineVLDDUP(N, DCI))
9084 return SDValue(N, 0);
9085
9086 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
9087 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00009088 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009089 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00009090 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009091 return SDValue();
9092
9093 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
9094 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
9095 // The canonical VMOV for a zero vector uses a 32-bit element size.
9096 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9097 unsigned EltBits;
9098 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
9099 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00009100 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009101 if (EltSize > VT.getVectorElementType().getSizeInBits())
9102 return SDValue();
9103
Andrew Trickef9de2a2013-05-25 02:42:55 +00009104 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009105}
9106
Eric Christopher1b8b94192011-06-29 21:10:36 +00009107// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosierfa8d8932011-06-24 19:23:04 +00009108// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
9109static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
9110{
Chad Rosier6b610b32011-06-28 17:26:57 +00009111 integerPart cN;
9112 integerPart c0 = 0;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009113 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
9114 I != E; I++) {
9115 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
9116 if (!C)
9117 return false;
9118
Eric Christopher1b8b94192011-06-29 21:10:36 +00009119 bool isExact;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009120 APFloat APF = C->getValueAPF();
9121 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
9122 != APFloat::opOK || !isExact)
9123 return false;
9124
9125 c0 = (I == 0) ? cN : c0;
9126 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
9127 return false;
9128 }
9129 C = c0;
9130 return true;
9131}
9132
9133/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
9134/// can replace combinations of VMUL and VCVT (floating-point to integer)
9135/// when the VMUL has a constant operand that is a power of 2.
9136///
9137/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9138/// vmul.f32 d16, d17, d16
9139/// vcvt.s32.f32 d16, d16
9140/// becomes:
9141/// vcvt.s32.f32 d16, d16, #3
9142static SDValue PerformVCVTCombine(SDNode *N,
9143 TargetLowering::DAGCombinerInfo &DCI,
9144 const ARMSubtarget *Subtarget) {
9145 SelectionDAG &DAG = DCI.DAG;
9146 SDValue Op = N->getOperand(0);
9147
9148 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
9149 Op.getOpcode() != ISD::FMUL)
9150 return SDValue();
9151
9152 uint64_t C;
9153 SDValue N0 = Op->getOperand(0);
9154 SDValue ConstVec = Op->getOperand(1);
9155 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
9156
Eric Christopher1b8b94192011-06-29 21:10:36 +00009157 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosierfa8d8932011-06-24 19:23:04 +00009158 !isConstVecPow2(ConstVec, isSigned, C))
9159 return SDValue();
9160
Tim Northover7cbc2152013-06-28 15:29:25 +00009161 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
9162 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
9163 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9164 // These instructions only exist converting from f32 to i32. We can handle
9165 // smaller integers by generating an extra truncate, but larger ones would
9166 // be lossy.
9167 return SDValue();
9168 }
9169
Chad Rosierfa8d8932011-06-24 19:23:04 +00009170 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
9171 Intrinsic::arm_neon_vcvtfp2fxu;
Tim Northover7cbc2152013-06-28 15:29:25 +00009172 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9173 SDValue FixConv = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
9174 NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9175 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
9176 DAG.getConstant(Log2_64(C), MVT::i32));
9177
9178 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9179 FixConv = DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), FixConv);
9180
9181 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009182}
9183
9184/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
9185/// can replace combinations of VCVT (integer to floating-point) and VDIV
9186/// when the VDIV has a constant operand that is a power of 2.
9187///
9188/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9189/// vcvt.f32.s32 d16, d16
9190/// vdiv.f32 d16, d17, d16
9191/// becomes:
9192/// vcvt.f32.s32 d16, d16, #3
9193static SDValue PerformVDIVCombine(SDNode *N,
9194 TargetLowering::DAGCombinerInfo &DCI,
9195 const ARMSubtarget *Subtarget) {
9196 SelectionDAG &DAG = DCI.DAG;
9197 SDValue Op = N->getOperand(0);
9198 unsigned OpOpcode = Op.getNode()->getOpcode();
9199
9200 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
9201 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
9202 return SDValue();
9203
9204 uint64_t C;
9205 SDValue ConstVec = N->getOperand(1);
9206 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
9207
9208 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
9209 !isConstVecPow2(ConstVec, isSigned, C))
9210 return SDValue();
9211
Tim Northover7cbc2152013-06-28 15:29:25 +00009212 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
9213 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
9214 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9215 // These instructions only exist converting from i32 to f32. We can handle
9216 // smaller integers by generating an extra extend, but larger ones would
9217 // be lossy.
9218 return SDValue();
9219 }
9220
9221 SDValue ConvInput = Op.getOperand(0);
9222 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9223 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9224 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
9225 SDLoc(N), NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9226 ConvInput);
9227
Eric Christopher1b8b94192011-06-29 21:10:36 +00009228 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +00009229 Intrinsic::arm_neon_vcvtfxu2fp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009230 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
Chad Rosierfa8d8932011-06-24 19:23:04 +00009231 Op.getValueType(),
Eric Christopher1b8b94192011-06-29 21:10:36 +00009232 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Tim Northover7cbc2152013-06-28 15:29:25 +00009233 ConvInput, DAG.getConstant(Log2_64(C), MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +00009234}
9235
9236/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +00009237/// operand of a vector shift operation, where all the elements of the
9238/// build_vector must have the same constant integer value.
9239static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
9240 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +00009241 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00009242 Op = Op.getOperand(0);
9243 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
9244 APInt SplatBits, SplatUndef;
9245 unsigned SplatBitSize;
9246 bool HasAnyUndefs;
9247 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
9248 HasAnyUndefs, ElementBits) ||
9249 SplatBitSize > ElementBits)
9250 return false;
9251 Cnt = SplatBits.getSExtValue();
9252 return true;
9253}
9254
9255/// isVShiftLImm - Check if this is a valid build_vector for the immediate
9256/// operand of a vector shift left operation. That value must be in the range:
9257/// 0 <= Value < ElementBits for a left shift; or
9258/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009259static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009260 assert(VT.isVector() && "vector shift count is not a vector type");
9261 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9262 if (! getVShiftImm(Op, ElementBits, Cnt))
9263 return false;
9264 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
9265}
9266
9267/// isVShiftRImm - Check if this is a valid build_vector for the immediate
9268/// operand of a vector shift right operation. For a shift opcode, the value
9269/// is positive, but for an intrinsic the value count must be negative. The
9270/// absolute value must be in the range:
9271/// 1 <= |Value| <= ElementBits for a right shift; or
9272/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009273static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +00009274 int64_t &Cnt) {
9275 assert(VT.isVector() && "vector shift count is not a vector type");
9276 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9277 if (! getVShiftImm(Op, ElementBits, Cnt))
9278 return false;
9279 if (isIntrinsic)
9280 Cnt = -Cnt;
9281 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
9282}
9283
9284/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
9285static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
9286 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
9287 switch (IntNo) {
9288 default:
9289 // Don't do anything for most intrinsics.
9290 break;
9291
9292 // Vector shifts: check for immediate versions and lower them.
9293 // Note: This is done during DAG combining instead of DAG legalizing because
9294 // the build_vectors for 64-bit vector element shift counts are generally
9295 // not legal, and it is hard to see their values after they get legalized to
9296 // loads from a constant pool.
9297 case Intrinsic::arm_neon_vshifts:
9298 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +00009299 case Intrinsic::arm_neon_vrshifts:
9300 case Intrinsic::arm_neon_vrshiftu:
9301 case Intrinsic::arm_neon_vrshiftn:
9302 case Intrinsic::arm_neon_vqshifts:
9303 case Intrinsic::arm_neon_vqshiftu:
9304 case Intrinsic::arm_neon_vqshiftsu:
9305 case Intrinsic::arm_neon_vqshiftns:
9306 case Intrinsic::arm_neon_vqshiftnu:
9307 case Intrinsic::arm_neon_vqshiftnsu:
9308 case Intrinsic::arm_neon_vqrshiftns:
9309 case Intrinsic::arm_neon_vqrshiftnu:
9310 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009311 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009312 int64_t Cnt;
9313 unsigned VShiftOpc = 0;
9314
9315 switch (IntNo) {
9316 case Intrinsic::arm_neon_vshifts:
9317 case Intrinsic::arm_neon_vshiftu:
9318 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
9319 VShiftOpc = ARMISD::VSHL;
9320 break;
9321 }
9322 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
9323 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
9324 ARMISD::VSHRs : ARMISD::VSHRu);
9325 break;
9326 }
9327 return SDValue();
9328
Bob Wilson2e076c42009-06-22 23:27:02 +00009329 case Intrinsic::arm_neon_vrshifts:
9330 case Intrinsic::arm_neon_vrshiftu:
9331 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9332 break;
9333 return SDValue();
9334
9335 case Intrinsic::arm_neon_vqshifts:
9336 case Intrinsic::arm_neon_vqshiftu:
9337 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9338 break;
9339 return SDValue();
9340
9341 case Intrinsic::arm_neon_vqshiftsu:
9342 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9343 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +00009344 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009345
Bob Wilson2e076c42009-06-22 23:27:02 +00009346 case Intrinsic::arm_neon_vrshiftn:
9347 case Intrinsic::arm_neon_vqshiftns:
9348 case Intrinsic::arm_neon_vqshiftnu:
9349 case Intrinsic::arm_neon_vqshiftnsu:
9350 case Intrinsic::arm_neon_vqrshiftns:
9351 case Intrinsic::arm_neon_vqrshiftnu:
9352 case Intrinsic::arm_neon_vqrshiftnsu:
9353 // Narrowing shifts require an immediate right shift.
9354 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9355 break;
Jim Grosbach84511e12010-06-02 21:53:11 +00009356 llvm_unreachable("invalid shift count for narrowing vector shift "
9357 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009358
9359 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00009360 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00009361 }
9362
9363 switch (IntNo) {
9364 case Intrinsic::arm_neon_vshifts:
9365 case Intrinsic::arm_neon_vshiftu:
9366 // Opcode already set above.
9367 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00009368 case Intrinsic::arm_neon_vrshifts:
9369 VShiftOpc = ARMISD::VRSHRs; break;
9370 case Intrinsic::arm_neon_vrshiftu:
9371 VShiftOpc = ARMISD::VRSHRu; break;
9372 case Intrinsic::arm_neon_vrshiftn:
9373 VShiftOpc = ARMISD::VRSHRN; break;
9374 case Intrinsic::arm_neon_vqshifts:
9375 VShiftOpc = ARMISD::VQSHLs; break;
9376 case Intrinsic::arm_neon_vqshiftu:
9377 VShiftOpc = ARMISD::VQSHLu; break;
9378 case Intrinsic::arm_neon_vqshiftsu:
9379 VShiftOpc = ARMISD::VQSHLsu; break;
9380 case Intrinsic::arm_neon_vqshiftns:
9381 VShiftOpc = ARMISD::VQSHRNs; break;
9382 case Intrinsic::arm_neon_vqshiftnu:
9383 VShiftOpc = ARMISD::VQSHRNu; break;
9384 case Intrinsic::arm_neon_vqshiftnsu:
9385 VShiftOpc = ARMISD::VQSHRNsu; break;
9386 case Intrinsic::arm_neon_vqrshiftns:
9387 VShiftOpc = ARMISD::VQRSHRNs; break;
9388 case Intrinsic::arm_neon_vqrshiftnu:
9389 VShiftOpc = ARMISD::VQRSHRNu; break;
9390 case Intrinsic::arm_neon_vqrshiftnsu:
9391 VShiftOpc = ARMISD::VQRSHRNsu; break;
9392 }
9393
Andrew Trickef9de2a2013-05-25 02:42:55 +00009394 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009395 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009396 }
9397
9398 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009399 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009400 int64_t Cnt;
9401 unsigned VShiftOpc = 0;
9402
9403 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9404 VShiftOpc = ARMISD::VSLI;
9405 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9406 VShiftOpc = ARMISD::VSRI;
9407 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009408 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009409 }
9410
Andrew Trickef9de2a2013-05-25 02:42:55 +00009411 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +00009412 N->getOperand(1), N->getOperand(2),
Owen Anderson9f944592009-08-11 20:47:22 +00009413 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009414 }
9415
9416 case Intrinsic::arm_neon_vqrshifts:
9417 case Intrinsic::arm_neon_vqrshiftu:
9418 // No immediate versions of these to check for.
9419 break;
9420 }
9421
9422 return SDValue();
9423}
9424
9425/// PerformShiftCombine - Checks for immediate versions of vector shifts and
9426/// lowers them. As with the vector shift intrinsics, this is done during DAG
9427/// combining instead of DAG legalizing because the build_vectors for 64-bit
9428/// vector element shift counts are generally not legal, and it is hard to see
9429/// their values after they get legalized to loads from a constant pool.
9430static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9431 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009432 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +00009433 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9434 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9435 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9436 SDValue N1 = N->getOperand(1);
9437 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9438 SDValue N0 = N->getOperand(0);
9439 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9440 DAG.MaskedValueIsZero(N0.getOperand(0),
9441 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009442 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +00009443 }
9444 }
Bob Wilson2e076c42009-06-22 23:27:02 +00009445
9446 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +00009447 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9448 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +00009449 return SDValue();
9450
9451 assert(ST->hasNEON() && "unexpected vector shift");
9452 int64_t Cnt;
9453
9454 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009455 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009456
9457 case ISD::SHL:
9458 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009459 return DAG.getNode(ARMISD::VSHL, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009460 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009461 break;
9462
9463 case ISD::SRA:
9464 case ISD::SRL:
9465 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
9466 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
9467 ARMISD::VSHRs : ARMISD::VSHRu);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009468 return DAG.getNode(VShiftOpc, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009469 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009470 }
9471 }
9472 return SDValue();
9473}
9474
9475/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
9476/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
9477static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
9478 const ARMSubtarget *ST) {
9479 SDValue N0 = N->getOperand(0);
9480
9481 // Check for sign- and zero-extensions of vector extract operations of 8-
9482 // and 16-bit vector elements. NEON supports these directly. They are
9483 // handled during DAG combining because type legalization will promote them
9484 // to 32-bit types and it is messy to recognize the operations after that.
9485 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9486 SDValue Vec = N0.getOperand(0);
9487 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00009488 EVT VT = N->getValueType(0);
9489 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009490 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9491
Owen Anderson9f944592009-08-11 20:47:22 +00009492 if (VT == MVT::i32 &&
9493 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +00009494 TLI.isTypeLegal(Vec.getValueType()) &&
9495 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009496
9497 unsigned Opc = 0;
9498 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009499 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009500 case ISD::SIGN_EXTEND:
9501 Opc = ARMISD::VGETLANEs;
9502 break;
9503 case ISD::ZERO_EXTEND:
9504 case ISD::ANY_EXTEND:
9505 Opc = ARMISD::VGETLANEu;
9506 break;
9507 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009508 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +00009509 }
9510 }
9511
9512 return SDValue();
9513}
9514
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009515/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9516/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9517static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9518 const ARMSubtarget *ST) {
9519 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng55f0c6b2010-07-15 22:07:12 +00009520 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009521 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9522 // a NaN; only do the transformation when it matches that behavior.
9523
9524 // For now only do this when using NEON for FP operations; if using VFP, it
9525 // is not obvious that the benefit outweighs the cost of switching to the
9526 // NEON pipeline.
9527 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9528 N->getValueType(0) != MVT::f32)
9529 return SDValue();
9530
9531 SDValue CondLHS = N->getOperand(0);
9532 SDValue CondRHS = N->getOperand(1);
9533 SDValue LHS = N->getOperand(2);
9534 SDValue RHS = N->getOperand(3);
9535 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9536
9537 unsigned Opcode = 0;
9538 bool IsReversed;
Bob Wilsonba8ac742010-02-24 22:15:53 +00009539 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009540 IsReversed = false; // x CC y ? x : y
Bob Wilsonba8ac742010-02-24 22:15:53 +00009541 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009542 IsReversed = true ; // x CC y ? y : x
9543 } else {
9544 return SDValue();
9545 }
9546
Bob Wilsonba8ac742010-02-24 22:15:53 +00009547 bool IsUnordered;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009548 switch (CC) {
9549 default: break;
9550 case ISD::SETOLT:
9551 case ISD::SETOLE:
9552 case ISD::SETLT:
9553 case ISD::SETLE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009554 case ISD::SETULT:
9555 case ISD::SETULE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009556 // If LHS is NaN, an ordered comparison will be false and the result will
9557 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
9558 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9559 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
9560 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9561 break;
9562 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
9563 // will return -0, so vmin can only be used for unsafe math or if one of
9564 // the operands is known to be nonzero.
9565 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009566 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009567 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9568 break;
9569 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009570 break;
9571
9572 case ISD::SETOGT:
9573 case ISD::SETOGE:
9574 case ISD::SETGT:
9575 case ISD::SETGE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009576 case ISD::SETUGT:
9577 case ISD::SETUGE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009578 // If LHS is NaN, an ordered comparison will be false and the result will
9579 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
9580 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9581 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
9582 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9583 break;
9584 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
9585 // will return +0, so vmax can only be used for unsafe math or if one of
9586 // the operands is known to be nonzero.
9587 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009588 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009589 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9590 break;
9591 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009592 break;
9593 }
9594
9595 if (!Opcode)
9596 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009597 return DAG.getNode(Opcode, SDLoc(N), N->getValueType(0), LHS, RHS);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009598}
9599
Evan Chengf863e3f2011-07-13 00:42:17 +00009600/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
9601SDValue
9602ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
9603 SDValue Cmp = N->getOperand(4);
9604 if (Cmp.getOpcode() != ARMISD::CMPZ)
9605 // Only looking at EQ and NE cases.
9606 return SDValue();
9607
9608 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009609 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +00009610 SDValue LHS = Cmp.getOperand(0);
9611 SDValue RHS = Cmp.getOperand(1);
9612 SDValue FalseVal = N->getOperand(0);
9613 SDValue TrueVal = N->getOperand(1);
9614 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +00009615 ARMCC::CondCodes CC =
9616 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +00009617
9618 // Simplify
9619 // mov r1, r0
9620 // cmp r1, x
9621 // mov r0, y
9622 // moveq r0, x
9623 // to
9624 // cmp r0, x
9625 // movne r0, y
9626 //
9627 // mov r1, r0
9628 // cmp r1, x
9629 // mov r0, x
9630 // movne r0, y
9631 // to
9632 // cmp r0, x
9633 // movne r0, y
9634 /// FIXME: Turn this into a target neutral optimization?
9635 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +00009636 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +00009637 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
9638 N->getOperand(3), Cmp);
9639 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
9640 SDValue ARMcc;
9641 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
9642 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
9643 N->getOperand(3), NewCmp);
9644 }
9645
9646 if (Res.getNode()) {
9647 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00009648 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +00009649 // Capture demanded bits information that would be otherwise lost.
9650 if (KnownZero == 0xfffffffe)
9651 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9652 DAG.getValueType(MVT::i1));
9653 else if (KnownZero == 0xffffff00)
9654 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9655 DAG.getValueType(MVT::i8));
9656 else if (KnownZero == 0xffff0000)
9657 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9658 DAG.getValueType(MVT::i16));
9659 }
9660
9661 return Res;
9662}
9663
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009664SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +00009665 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009666 switch (N->getOpcode()) {
9667 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009668 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +00009669 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009670 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009671 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +00009672 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +00009673 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
9674 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +00009675 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00009676 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson22806742010-09-22 22:09:21 +00009677 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009678 case ISD::STORE: return PerformSTORECombine(N, DCI);
9679 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
9680 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +00009681 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +00009682 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +00009683 case ISD::FP_TO_SINT:
9684 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
9685 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009686 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00009687 case ISD::SHL:
9688 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009689 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00009690 case ISD::SIGN_EXTEND:
9691 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009692 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
9693 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +00009694 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson06fce872011-02-07 17:43:21 +00009695 case ARMISD::VLD2DUP:
9696 case ARMISD::VLD3DUP:
9697 case ARMISD::VLD4DUP:
9698 return CombineBaseUpdate(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009699 case ARMISD::BUILD_VECTOR:
9700 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +00009701 case ISD::INTRINSIC_VOID:
9702 case ISD::INTRINSIC_W_CHAIN:
9703 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9704 case Intrinsic::arm_neon_vld1:
9705 case Intrinsic::arm_neon_vld2:
9706 case Intrinsic::arm_neon_vld3:
9707 case Intrinsic::arm_neon_vld4:
9708 case Intrinsic::arm_neon_vld2lane:
9709 case Intrinsic::arm_neon_vld3lane:
9710 case Intrinsic::arm_neon_vld4lane:
9711 case Intrinsic::arm_neon_vst1:
9712 case Intrinsic::arm_neon_vst2:
9713 case Intrinsic::arm_neon_vst3:
9714 case Intrinsic::arm_neon_vst4:
9715 case Intrinsic::arm_neon_vst2lane:
9716 case Intrinsic::arm_neon_vst3lane:
9717 case Intrinsic::arm_neon_vst4lane:
9718 return CombineBaseUpdate(N, DCI);
9719 default: break;
9720 }
9721 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009722 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009723 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009724}
9725
Evan Chengd42641c2011-02-02 01:06:55 +00009726bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9727 EVT VT) const {
9728 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9729}
9730
Matt Arsenault6f2a5262014-07-27 17:46:40 +00009731bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
9732 unsigned,
9733 unsigned,
9734 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009735 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +00009736 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009737
9738 switch (VT.getSimpleVT().SimpleTy) {
9739 default:
9740 return false;
9741 case MVT::i8:
9742 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009743 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009744 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +00009745 if (AllowsUnaligned) {
9746 if (Fast)
9747 *Fast = Subtarget->hasV7Ops();
9748 return true;
9749 }
9750 return false;
9751 }
Evan Chengeec6bc62012-08-15 17:44:53 +00009752 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009753 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009754 // For any little-endian targets with neon, we can support unaligned ld/st
9755 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +00009756 // A big-endian target may also explicitly support unaligned accesses
Evan Cheng79e2ca92012-12-10 23:21:26 +00009757 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) {
9758 if (Fast)
9759 *Fast = true;
9760 return true;
9761 }
9762 return false;
9763 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009764 }
9765}
9766
Lang Hames9929c422011-11-02 22:52:45 +00009767static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
9768 unsigned AlignCheck) {
9769 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
9770 (DstAlign == 0 || DstAlign % AlignCheck == 0));
9771}
9772
9773EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
9774 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00009775 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +00009776 bool MemcpyStrSrc,
9777 MachineFunction &MF) const {
9778 const Function *F = MF.getFunction();
9779
9780 // See if we can use NEON instructions for this...
Evan Cheng962711e2012-12-12 02:34:41 +00009781 if ((!IsMemset || ZeroMemset) &&
Evan Cheng79e2ca92012-12-10 23:21:26 +00009782 Subtarget->hasNEON() &&
Bill Wendling698e84f2012-12-30 10:32:01 +00009783 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
9784 Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009785 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +00009786 if (Size >= 16 &&
9787 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +00009788 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009789 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +00009790 } else if (Size >= 8 &&
9791 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +00009792 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) &&
9793 Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009794 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +00009795 }
9796 }
9797
Lang Hamesb85fcd02011-11-08 18:56:23 +00009798 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +00009799 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +00009800 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +00009801 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +00009802 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +00009803
Lang Hames9929c422011-11-02 22:52:45 +00009804 // Let the target-independent logic figure it out.
9805 return MVT::Other;
9806}
9807
Evan Cheng9ec512d2012-12-06 19:13:27 +00009808bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
9809 if (Val.getOpcode() != ISD::LOAD)
9810 return false;
9811
9812 EVT VT1 = Val.getValueType();
9813 if (!VT1.isSimple() || !VT1.isInteger() ||
9814 !VT2.isSimple() || !VT2.isInteger())
9815 return false;
9816
9817 switch (VT1.getSimpleVT().SimpleTy) {
9818 default: break;
9819 case MVT::i1:
9820 case MVT::i8:
9821 case MVT::i16:
9822 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
9823 return true;
9824 }
9825
9826 return false;
9827}
9828
Tim Northovercc2e9032013-08-06 13:58:03 +00009829bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
9830 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
9831 return false;
9832
9833 if (!isTypeLegal(EVT::getEVT(Ty1)))
9834 return false;
9835
9836 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
9837
9838 // Assuming the caller doesn't have a zeroext or signext return parameter,
9839 // truncation all the way down to i1 is valid.
9840 return true;
9841}
9842
9843
Evan Chengdc49a8d2009-08-14 20:09:37 +00009844static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
9845 if (V < 0)
9846 return false;
9847
9848 unsigned Scale = 1;
9849 switch (VT.getSimpleVT().SimpleTy) {
9850 default: return false;
9851 case MVT::i1:
9852 case MVT::i8:
9853 // Scale == 1;
9854 break;
9855 case MVT::i16:
9856 // Scale == 2;
9857 Scale = 2;
9858 break;
9859 case MVT::i32:
9860 // Scale == 4;
9861 Scale = 4;
9862 break;
9863 }
9864
9865 if ((V & (Scale - 1)) != 0)
9866 return false;
9867 V /= Scale;
9868 return V == (V & ((1LL << 5) - 1));
9869}
9870
9871static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
9872 const ARMSubtarget *Subtarget) {
9873 bool isNeg = false;
9874 if (V < 0) {
9875 isNeg = true;
9876 V = - V;
9877 }
9878
9879 switch (VT.getSimpleVT().SimpleTy) {
9880 default: return false;
9881 case MVT::i1:
9882 case MVT::i8:
9883 case MVT::i16:
9884 case MVT::i32:
9885 // + imm12 or - imm8
9886 if (isNeg)
9887 return V == (V & ((1LL << 8) - 1));
9888 return V == (V & ((1LL << 12) - 1));
9889 case MVT::f32:
9890 case MVT::f64:
9891 // Same as ARM mode. FIXME: NEON?
9892 if (!Subtarget->hasVFP2())
9893 return false;
9894 if ((V & 3) != 0)
9895 return false;
9896 V >>= 2;
9897 return V == (V & ((1LL << 8) - 1));
9898 }
9899}
9900
Evan Cheng2150b922007-03-12 23:30:29 +00009901/// isLegalAddressImmediate - Return true if the integer value can be used
9902/// as the offset of the target addressing mode for load / store of the
9903/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009904static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009905 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +00009906 if (V == 0)
9907 return true;
9908
Evan Chengce5dfb62009-03-09 19:15:00 +00009909 if (!VT.isSimple())
9910 return false;
9911
Evan Chengdc49a8d2009-08-14 20:09:37 +00009912 if (Subtarget->isThumb1Only())
9913 return isLegalT1AddressImmediate(V, VT);
9914 else if (Subtarget->isThumb2())
9915 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +00009916
Evan Chengdc49a8d2009-08-14 20:09:37 +00009917 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +00009918 if (V < 0)
9919 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +00009920 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +00009921 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +00009922 case MVT::i1:
9923 case MVT::i8:
9924 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +00009925 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009926 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +00009927 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +00009928 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009929 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +00009930 case MVT::f32:
9931 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009932 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +00009933 return false;
Evan Chengbef131de2007-05-03 02:00:18 +00009934 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +00009935 return false;
9936 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009937 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +00009938 }
Evan Cheng10043e22007-01-19 07:51:42 +00009939}
9940
Evan Chengdc49a8d2009-08-14 20:09:37 +00009941bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
9942 EVT VT) const {
9943 int Scale = AM.Scale;
9944 if (Scale < 0)
9945 return false;
9946
9947 switch (VT.getSimpleVT().SimpleTy) {
9948 default: return false;
9949 case MVT::i1:
9950 case MVT::i8:
9951 case MVT::i16:
9952 case MVT::i32:
9953 if (Scale == 1)
9954 return true;
9955 // r + r << imm
9956 Scale = Scale & ~1;
9957 return Scale == 2 || Scale == 4 || Scale == 8;
9958 case MVT::i64:
9959 // r + r
9960 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
9961 return true;
9962 return false;
9963 case MVT::isVoid:
9964 // Note, we allow "void" uses (basically, uses that aren't loads or
9965 // stores), because arm allows folding a scale into many arithmetic
9966 // operations. This should be made more precise and revisited later.
9967
9968 // Allow r << imm, but the imm has to be a multiple of two.
9969 if (Scale & 1) return false;
9970 return isPowerOf2_32(Scale);
9971 }
9972}
9973
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009974/// isLegalAddressingMode - Return true if the addressing mode represented
9975/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson7117a912009-03-20 22:42:55 +00009976bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +00009977 Type *Ty) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009978 EVT VT = getValueType(Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +00009979 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +00009980 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009981
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009982 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +00009983 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009984 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009985
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009986 switch (AM.Scale) {
9987 case 0: // no scale reg, must be "r+i" or "r", or "i".
9988 break;
9989 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009990 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009991 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +00009992 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009993 default:
Chris Lattner502c3f42007-04-13 06:50:55 +00009994 // ARM doesn't support any R+R*scale+imm addr modes.
9995 if (AM.BaseOffs)
9996 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009997
Bob Wilson866c1742009-04-08 17:55:28 +00009998 if (!VT.isSimple())
9999 return false;
10000
Evan Chengdc49a8d2009-08-14 20:09:37 +000010001 if (Subtarget->isThumb2())
10002 return isLegalT2ScaledAddressingMode(AM, VT);
10003
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010004 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +000010005 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010006 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010007 case MVT::i1:
10008 case MVT::i8:
10009 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010010 if (Scale < 0) Scale = -Scale;
10011 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010012 return true;
10013 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +000010014 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +000010015 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010016 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010017 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010018 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010019 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +000010020 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010021
Owen Anderson9f944592009-08-11 20:47:22 +000010022 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010023 // Note, we allow "void" uses (basically, uses that aren't loads or
10024 // stores), because arm allows folding a scale into many arithmetic
10025 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +000010026
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010027 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +000010028 if (Scale & 1) return false;
10029 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010030 }
Evan Cheng2150b922007-03-12 23:30:29 +000010031 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010032 return true;
Evan Cheng2150b922007-03-12 23:30:29 +000010033}
10034
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010035/// isLegalICmpImmediate - Return true if the specified immediate is legal
10036/// icmp immediate, that is the target has icmp instructions which can compare
10037/// a register against the immediate without having to materialize the
10038/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +000010039bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010040 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010041 if (!Subtarget->isThumb())
Chandler Carruth8a102c22012-04-06 20:10:52 +000010042 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010043 if (Subtarget->isThumb2())
Chandler Carruth8a102c22012-04-06 20:10:52 +000010044 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010045 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +000010046 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010047}
10048
Andrew Tricka22cdb72012-07-18 18:34:27 +000010049/// isLegalAddImmediate - Return true if the specified immediate is a legal add
10050/// *or sub* immediate, that is the target has add or sub instructions which can
10051/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +000010052/// immediate into a register.
10053bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +000010054 // Same encoding for add/sub, just flip the sign.
10055 int64_t AbsImm = llvm::abs64(Imm);
10056 if (!Subtarget->isThumb())
10057 return ARM_AM::getSOImmVal(AbsImm) != -1;
10058 if (Subtarget->isThumb2())
10059 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
10060 // Thumb1 only has 8-bit unsigned immediate.
10061 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +000010062}
10063
Owen Anderson53aa7a92009-08-10 22:56:29 +000010064static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010065 bool isSEXTLoad, SDValue &Base,
10066 SDValue &Offset, bool &isInc,
10067 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +000010068 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10069 return false;
10070
Owen Anderson9f944592009-08-11 20:47:22 +000010071 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +000010072 // AddressingMode 3
10073 Base = Ptr->getOperand(0);
10074 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010075 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010076 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010077 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010078 isInc = false;
10079 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10080 return true;
10081 }
10082 }
10083 isInc = (Ptr->getOpcode() == ISD::ADD);
10084 Offset = Ptr->getOperand(1);
10085 return true;
Owen Anderson9f944592009-08-11 20:47:22 +000010086 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +000010087 // AddressingMode 2
10088 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010089 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010090 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010091 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010092 isInc = false;
10093 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10094 Base = Ptr->getOperand(0);
10095 return true;
10096 }
10097 }
10098
10099 if (Ptr->getOpcode() == ISD::ADD) {
10100 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +000010101 ARM_AM::ShiftOpc ShOpcVal=
10102 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +000010103 if (ShOpcVal != ARM_AM::no_shift) {
10104 Base = Ptr->getOperand(1);
10105 Offset = Ptr->getOperand(0);
10106 } else {
10107 Base = Ptr->getOperand(0);
10108 Offset = Ptr->getOperand(1);
10109 }
10110 return true;
10111 }
10112
10113 isInc = (Ptr->getOpcode() == ISD::ADD);
10114 Base = Ptr->getOperand(0);
10115 Offset = Ptr->getOperand(1);
10116 return true;
10117 }
10118
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000010119 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000010120 return false;
10121}
10122
Owen Anderson53aa7a92009-08-10 22:56:29 +000010123static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010124 bool isSEXTLoad, SDValue &Base,
10125 SDValue &Offset, bool &isInc,
10126 SelectionDAG &DAG) {
10127 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10128 return false;
10129
10130 Base = Ptr->getOperand(0);
10131 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
10132 int RHSC = (int)RHS->getZExtValue();
10133 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
10134 assert(Ptr->getOpcode() == ISD::ADD);
10135 isInc = false;
10136 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10137 return true;
10138 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
10139 isInc = Ptr->getOpcode() == ISD::ADD;
10140 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
10141 return true;
10142 }
10143 }
10144
10145 return false;
10146}
10147
Evan Cheng10043e22007-01-19 07:51:42 +000010148/// getPreIndexedAddressParts - returns true by value, base pointer and
10149/// offset pointer and addressing mode by reference if the node's address
10150/// can be legally represented as pre-indexed load / store address.
10151bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010152ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
10153 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010154 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010155 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010156 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010157 return false;
10158
Owen Anderson53aa7a92009-08-10 22:56:29 +000010159 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010160 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010161 bool isSEXTLoad = false;
10162 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
10163 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010164 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010165 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10166 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
10167 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010168 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010169 } else
10170 return false;
10171
10172 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010173 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010174 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010175 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
10176 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010177 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010178 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000010179 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000010180 if (!isLegal)
10181 return false;
10182
10183 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
10184 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010185}
10186
10187/// getPostIndexedAddressParts - returns true by value, base pointer and
10188/// offset pointer and addressing mode by reference if this node can be
10189/// combined with a load / store to form a post-indexed load / store.
10190bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010191 SDValue &Base,
10192 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010193 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010194 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010195 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010196 return false;
10197
Owen Anderson53aa7a92009-08-10 22:56:29 +000010198 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010199 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010200 bool isSEXTLoad = false;
10201 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010202 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010203 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010204 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10205 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010206 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010207 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010208 } else
10209 return false;
10210
10211 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010212 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010213 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010214 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000010215 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010216 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010217 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
10218 isInc, DAG);
10219 if (!isLegal)
10220 return false;
10221
Evan Chengf19384d2010-05-18 21:31:17 +000010222 if (Ptr != Base) {
10223 // Swap base ptr and offset to catch more post-index load / store when
10224 // it's legal. In Thumb2 mode, offset must be an immediate.
10225 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
10226 !Subtarget->isThumb2())
10227 std::swap(Base, Offset);
10228
10229 // Post-indexed load / store update the base pointer.
10230 if (Ptr != Base)
10231 return false;
10232 }
10233
Evan Cheng84c6cda2009-07-02 07:28:31 +000010234 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
10235 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010236}
10237
Jay Foada0653a32014-05-14 21:14:37 +000010238void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
10239 APInt &KnownZero,
10240 APInt &KnownOne,
10241 const SelectionDAG &DAG,
10242 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000010243 unsigned BitWidth = KnownOne.getBitWidth();
10244 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000010245 switch (Op.getOpcode()) {
10246 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000010247 case ARMISD::ADDC:
10248 case ARMISD::ADDE:
10249 case ARMISD::SUBC:
10250 case ARMISD::SUBE:
10251 // These nodes' second result is a boolean
10252 if (Op.getResNo() == 0)
10253 break;
10254 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
10255 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010256 case ARMISD::CMOV: {
10257 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000010258 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010259 if (KnownZero == 0 && KnownOne == 0) return;
10260
Dan Gohmanf990faf2008-02-13 00:35:47 +000010261 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000010262 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010263 KnownZero &= KnownZeroRHS;
10264 KnownOne &= KnownOneRHS;
10265 return;
10266 }
Tim Northover01b4aa92014-04-03 15:10:35 +000010267 case ISD::INTRINSIC_W_CHAIN: {
10268 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
10269 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
10270 switch (IntID) {
10271 default: return;
10272 case Intrinsic::arm_ldaex:
10273 case Intrinsic::arm_ldrex: {
10274 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
10275 unsigned MemBits = VT.getScalarType().getSizeInBits();
10276 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
10277 return;
10278 }
10279 }
10280 }
Evan Cheng10043e22007-01-19 07:51:42 +000010281 }
10282}
10283
10284//===----------------------------------------------------------------------===//
10285// ARM Inline Assembly Support
10286//===----------------------------------------------------------------------===//
10287
Evan Cheng078b0b02011-01-08 01:24:27 +000010288bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
10289 // Looking for "rev" which is V6+.
10290 if (!Subtarget->hasV6Ops())
10291 return false;
10292
10293 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10294 std::string AsmStr = IA->getAsmString();
10295 SmallVector<StringRef, 4> AsmPieces;
10296 SplitString(AsmStr, AsmPieces, ";\n");
10297
10298 switch (AsmPieces.size()) {
10299 default: return false;
10300 case 1:
10301 AsmStr = AsmPieces[0];
10302 AsmPieces.clear();
10303 SplitString(AsmStr, AsmPieces, " \t,");
10304
10305 // rev $0, $1
10306 if (AsmPieces.size() == 3 &&
10307 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
10308 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000010309 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000010310 if (Ty && Ty->getBitWidth() == 32)
10311 return IntrinsicLowering::LowerToByteSwap(CI);
10312 }
10313 break;
10314 }
10315
10316 return false;
10317}
10318
Evan Cheng10043e22007-01-19 07:51:42 +000010319/// getConstraintType - Given a constraint letter, return the type of
10320/// constraint it is for this target.
10321ARMTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +000010322ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
10323 if (Constraint.size() == 1) {
10324 switch (Constraint[0]) {
10325 default: break;
10326 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000010327 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000010328 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000010329 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000010330 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000010331 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000010332 // An address with a single base register. Due to the way we
10333 // currently handle addresses it is the same as an 'r' memory constraint.
10334 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000010335 }
Eric Christophere256cd02011-06-21 22:10:57 +000010336 } else if (Constraint.size() == 2) {
10337 switch (Constraint[0]) {
10338 default: break;
10339 // All 'U+' constraints are addresses.
10340 case 'U': return C_Memory;
10341 }
Evan Cheng10043e22007-01-19 07:51:42 +000010342 }
Chris Lattnerd6855142007-03-25 02:14:49 +000010343 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000010344}
10345
John Thompsone8360b72010-10-29 17:29:13 +000010346/// Examine constraint type and operand type and determine a weight value.
10347/// This object must already have been set up with the operand type
10348/// and the current alternative constraint selected.
10349TargetLowering::ConstraintWeight
10350ARMTargetLowering::getSingleConstraintMatchWeight(
10351 AsmOperandInfo &info, const char *constraint) const {
10352 ConstraintWeight weight = CW_Invalid;
10353 Value *CallOperandVal = info.CallOperandVal;
10354 // If we don't have a value, we can't do a match,
10355 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000010356 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000010357 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000010358 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000010359 // Look at the constraint type.
10360 switch (*constraint) {
10361 default:
10362 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10363 break;
10364 case 'l':
10365 if (type->isIntegerTy()) {
10366 if (Subtarget->isThumb())
10367 weight = CW_SpecificReg;
10368 else
10369 weight = CW_Register;
10370 }
10371 break;
10372 case 'w':
10373 if (type->isFloatingPointTy())
10374 weight = CW_Register;
10375 break;
10376 }
10377 return weight;
10378}
10379
Eric Christophercf2007c2011-06-30 23:50:52 +000010380typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
10381RCPair
Evan Cheng10043e22007-01-19 07:51:42 +000010382ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +000010383 MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000010384 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010385 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000010386 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000010387 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010388 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010389 return RCPair(0U, &ARM::tGPRRegClass);
10390 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000010391 case 'h': // High regs or no regs.
10392 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010393 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000010394 break;
Chris Lattner6223e832007-04-02 17:24:08 +000010395 case 'r':
Craig Topperc7242e02012-04-20 07:30:17 +000010396 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010397 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000010398 if (VT == MVT::Other)
10399 break;
Owen Anderson9f944592009-08-11 20:47:22 +000010400 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010401 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000010402 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010403 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000010404 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010405 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010406 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010407 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000010408 if (VT == MVT::Other)
10409 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010410 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010411 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010412 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010413 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010414 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010415 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010416 break;
Eric Christopherc011d312011-07-01 00:30:46 +000010417 case 't':
10418 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010419 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000010420 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010421 }
10422 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000010423 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000010424 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000010425
Evan Cheng10043e22007-01-19 07:51:42 +000010426 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
10427}
10428
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010429/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10430/// vector. If it is invalid, don't add anything to Ops.
10431void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000010432 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010433 std::vector<SDValue>&Ops,
10434 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000010435 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010436
Eric Christopherde9399b2011-06-02 23:16:42 +000010437 // Currently only support length 1 constraints.
10438 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000010439
Eric Christopherde9399b2011-06-02 23:16:42 +000010440 char ConstraintLetter = Constraint[0];
10441 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010442 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000010443 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010444 case 'I': case 'J': case 'K': case 'L':
10445 case 'M': case 'N': case 'O':
10446 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10447 if (!C)
10448 return;
10449
10450 int64_t CVal64 = C->getSExtValue();
10451 int CVal = (int) CVal64;
10452 // None of these constraints allow values larger than 32 bits. Check
10453 // that the value fits in an int.
10454 if (CVal != CVal64)
10455 return;
10456
Eric Christopherde9399b2011-06-02 23:16:42 +000010457 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000010458 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000010459 // Constant suitable for movw, must be between 0 and
10460 // 65535.
10461 if (Subtarget->hasV6T2Ops())
10462 if (CVal >= 0 && CVal <= 65535)
10463 break;
10464 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010465 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000010466 if (Subtarget->isThumb1Only()) {
10467 // This must be a constant between 0 and 255, for ADD
10468 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010469 if (CVal >= 0 && CVal <= 255)
10470 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010471 } else if (Subtarget->isThumb2()) {
10472 // A constant that can be used as an immediate value in a
10473 // data-processing instruction.
10474 if (ARM_AM::getT2SOImmVal(CVal) != -1)
10475 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010476 } else {
10477 // A constant that can be used as an immediate value in a
10478 // data-processing instruction.
10479 if (ARM_AM::getSOImmVal(CVal) != -1)
10480 break;
10481 }
10482 return;
10483
10484 case 'J':
David Goodwin22c2fba2009-07-08 23:10:31 +000010485 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010486 // This must be a constant between -255 and -1, for negated ADD
10487 // immediates. This can be used in GCC with an "n" modifier that
10488 // prints the negated value, for use with SUB instructions. It is
10489 // not useful otherwise but is implemented for compatibility.
10490 if (CVal >= -255 && CVal <= -1)
10491 break;
10492 } else {
10493 // This must be a constant between -4095 and 4095. It is not clear
10494 // what this constraint is intended for. Implemented for
10495 // compatibility with GCC.
10496 if (CVal >= -4095 && CVal <= 4095)
10497 break;
10498 }
10499 return;
10500
10501 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000010502 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010503 // A 32-bit value where only one byte has a nonzero value. Exclude
10504 // zero to match GCC. This constraint is used by GCC internally for
10505 // constants that can be loaded with a move/shift combination.
10506 // It is not useful otherwise but is implemented for compatibility.
10507 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
10508 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010509 } else if (Subtarget->isThumb2()) {
10510 // A constant whose bitwise inverse can be used as an immediate
10511 // value in a data-processing instruction. This can be used in GCC
10512 // with a "B" modifier that prints the inverted value, for use with
10513 // BIC and MVN instructions. It is not useful otherwise but is
10514 // implemented for compatibility.
10515 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
10516 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010517 } else {
10518 // A constant whose bitwise inverse can be used as an immediate
10519 // value in a data-processing instruction. This can be used in GCC
10520 // with a "B" modifier that prints the inverted value, for use with
10521 // BIC and MVN instructions. It is not useful otherwise but is
10522 // implemented for compatibility.
10523 if (ARM_AM::getSOImmVal(~CVal) != -1)
10524 break;
10525 }
10526 return;
10527
10528 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000010529 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010530 // This must be a constant between -7 and 7,
10531 // for 3-operand ADD/SUB immediate instructions.
10532 if (CVal >= -7 && CVal < 7)
10533 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010534 } else if (Subtarget->isThumb2()) {
10535 // A constant whose negation can be used as an immediate value in a
10536 // data-processing instruction. This can be used in GCC with an "n"
10537 // modifier that prints the negated value, for use with SUB
10538 // instructions. It is not useful otherwise but is implemented for
10539 // compatibility.
10540 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
10541 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010542 } else {
10543 // A constant whose negation can be used as an immediate value in a
10544 // data-processing instruction. This can be used in GCC with an "n"
10545 // modifier that prints the negated value, for use with SUB
10546 // instructions. It is not useful otherwise but is implemented for
10547 // compatibility.
10548 if (ARM_AM::getSOImmVal(-CVal) != -1)
10549 break;
10550 }
10551 return;
10552
10553 case 'M':
David Goodwin22c2fba2009-07-08 23:10:31 +000010554 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010555 // This must be a multiple of 4 between 0 and 1020, for
10556 // ADD sp + immediate.
10557 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
10558 break;
10559 } else {
10560 // A power of two or a constant between 0 and 32. This is used in
10561 // GCC for the shift amount on shifted register operands, but it is
10562 // useful in general for any shift amounts.
10563 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
10564 break;
10565 }
10566 return;
10567
10568 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000010569 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010570 // This must be a constant between 0 and 31, for shift amounts.
10571 if (CVal >= 0 && CVal <= 31)
10572 break;
10573 }
10574 return;
10575
10576 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000010577 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010578 // This must be a multiple of 4 between -508 and 508, for
10579 // ADD/SUB sp = sp + immediate.
10580 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
10581 break;
10582 }
10583 return;
10584 }
10585 Result = DAG.getTargetConstant(CVal, Op.getValueType());
10586 break;
10587 }
10588
10589 if (Result.getNode()) {
10590 Ops.push_back(Result);
10591 return;
10592 }
Dale Johannesence97d552010-06-25 21:55:36 +000010593 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010594}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010595
Renato Golin87610692013-07-16 09:32:17 +000010596SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
10597 assert(Subtarget->isTargetAEABI() && "Register-based DivRem lowering only");
10598 unsigned Opcode = Op->getOpcode();
10599 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
Saleem Abdulrasool740be892014-08-17 22:50:59 +000010600 "Invalid opcode for Div/Rem lowering");
Renato Golin87610692013-07-16 09:32:17 +000010601 bool isSigned = (Opcode == ISD::SDIVREM);
10602 EVT VT = Op->getValueType(0);
10603 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
10604
10605 RTLIB::Libcall LC;
10606 switch (VT.getSimpleVT().SimpleTy) {
10607 default: llvm_unreachable("Unexpected request for libcall!");
Saleem Abdulrasool740be892014-08-17 22:50:59 +000010608 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
10609 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
10610 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
10611 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
Renato Golin87610692013-07-16 09:32:17 +000010612 }
10613
10614 SDValue InChain = DAG.getEntryNode();
10615
10616 TargetLowering::ArgListTy Args;
10617 TargetLowering::ArgListEntry Entry;
10618 for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) {
10619 EVT ArgVT = Op->getOperand(i).getValueType();
10620 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
10621 Entry.Node = Op->getOperand(i);
10622 Entry.Ty = ArgTy;
10623 Entry.isSExt = isSigned;
10624 Entry.isZExt = !isSigned;
10625 Args.push_back(Entry);
10626 }
10627
10628 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
10629 getPointerTy());
10630
10631 Type *RetTy = (Type*)StructType::get(Ty, Ty, NULL);
10632
10633 SDLoc dl(Op);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010634 TargetLowering::CallLoweringInfo CLI(DAG);
10635 CLI.setDebugLoc(dl).setChain(InChain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +000010636 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010637 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000010638
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010639 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000010640 return CallInfo.first;
10641}
10642
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000010643SDValue
10644ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
10645 assert(Subtarget->isTargetWindows() && "unsupported target platform");
10646 SDLoc DL(Op);
10647
10648 // Get the inputs.
10649 SDValue Chain = Op.getOperand(0);
10650 SDValue Size = Op.getOperand(1);
10651
10652 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
10653 DAG.getConstant(2, MVT::i32));
10654
10655 SDValue Flag;
10656 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
10657 Flag = Chain.getValue(1);
10658
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +000010659 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000010660 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
10661
10662 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
10663 Chain = NewSP.getValue(1);
10664
10665 SDValue Ops[2] = { NewSP, Chain };
10666 return DAG.getMergeValues(Ops, DL);
10667}
10668
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010669bool
10670ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
10671 // The ARM target isn't yet aware of offsets.
10672 return false;
10673}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010674
Jim Grosbach11013ed2010-07-16 23:05:05 +000010675bool ARM::isBitFieldInvertedMask(unsigned v) {
10676 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010677 return false;
10678
Jim Grosbach11013ed2010-07-16 23:05:05 +000010679 // there can be 1's on either or both "outsides", all the "inside"
10680 // bits must be 0's
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010681 unsigned TO = CountTrailingOnes_32(v);
10682 unsigned LO = CountLeadingOnes_32(v);
10683 v = (v >> TO) << TO;
10684 v = (v << LO) >> LO;
10685 return v == 0;
Jim Grosbach11013ed2010-07-16 23:05:05 +000010686}
10687
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010688/// isFPImmLegal - Returns true if the target can instruction select the
10689/// specified FP immediate natively. If false, the legalizer will
10690/// materialize the FP immediate as a load from a constant pool.
10691bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
10692 if (!Subtarget->hasVFP3())
10693 return false;
10694 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000010695 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010696 if (VT == MVT::f64)
Jim Grosbachefc761a2011-09-30 00:50:06 +000010697 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010698 return false;
10699}
Bob Wilson5549d492010-09-21 17:56:22 +000010700
Wesley Peck527da1b2010-11-23 03:31:01 +000010701/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000010702/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
10703/// specified in the intrinsic calls.
10704bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
10705 const CallInst &I,
10706 unsigned Intrinsic) const {
10707 switch (Intrinsic) {
10708 case Intrinsic::arm_neon_vld1:
10709 case Intrinsic::arm_neon_vld2:
10710 case Intrinsic::arm_neon_vld3:
10711 case Intrinsic::arm_neon_vld4:
10712 case Intrinsic::arm_neon_vld2lane:
10713 case Intrinsic::arm_neon_vld3lane:
10714 case Intrinsic::arm_neon_vld4lane: {
10715 Info.opc = ISD::INTRINSIC_W_CHAIN;
10716 // Conservatively set memVT to the entire set of vectors loaded.
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010717 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010718 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10719 Info.ptrVal = I.getArgOperand(0);
10720 Info.offset = 0;
10721 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10722 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10723 Info.vol = false; // volatile loads with NEON intrinsics not supported
10724 Info.readMem = true;
10725 Info.writeMem = false;
10726 return true;
10727 }
10728 case Intrinsic::arm_neon_vst1:
10729 case Intrinsic::arm_neon_vst2:
10730 case Intrinsic::arm_neon_vst3:
10731 case Intrinsic::arm_neon_vst4:
10732 case Intrinsic::arm_neon_vst2lane:
10733 case Intrinsic::arm_neon_vst3lane:
10734 case Intrinsic::arm_neon_vst4lane: {
10735 Info.opc = ISD::INTRINSIC_VOID;
10736 // Conservatively set memVT to the entire set of vectors stored.
10737 unsigned NumElts = 0;
10738 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000010739 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000010740 if (!ArgTy->isVectorTy())
10741 break;
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010742 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010743 }
10744 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10745 Info.ptrVal = I.getArgOperand(0);
10746 Info.offset = 0;
10747 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10748 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10749 Info.vol = false; // volatile stores with NEON intrinsics not supported
10750 Info.readMem = false;
10751 Info.writeMem = true;
10752 return true;
10753 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010754 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000010755 case Intrinsic::arm_ldrex: {
10756 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
10757 Info.opc = ISD::INTRINSIC_W_CHAIN;
10758 Info.memVT = MVT::getVT(PtrTy->getElementType());
10759 Info.ptrVal = I.getArgOperand(0);
10760 Info.offset = 0;
10761 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10762 Info.vol = true;
10763 Info.readMem = true;
10764 Info.writeMem = false;
10765 return true;
10766 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010767 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000010768 case Intrinsic::arm_strex: {
10769 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
10770 Info.opc = ISD::INTRINSIC_W_CHAIN;
10771 Info.memVT = MVT::getVT(PtrTy->getElementType());
10772 Info.ptrVal = I.getArgOperand(1);
10773 Info.offset = 0;
10774 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10775 Info.vol = true;
10776 Info.readMem = false;
10777 Info.writeMem = true;
10778 return true;
10779 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010780 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010781 case Intrinsic::arm_strexd: {
10782 Info.opc = ISD::INTRINSIC_W_CHAIN;
10783 Info.memVT = MVT::i64;
10784 Info.ptrVal = I.getArgOperand(2);
10785 Info.offset = 0;
10786 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000010787 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010788 Info.readMem = false;
10789 Info.writeMem = true;
10790 return true;
10791 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010792 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010793 case Intrinsic::arm_ldrexd: {
10794 Info.opc = ISD::INTRINSIC_W_CHAIN;
10795 Info.memVT = MVT::i64;
10796 Info.ptrVal = I.getArgOperand(0);
10797 Info.offset = 0;
10798 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000010799 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010800 Info.readMem = true;
10801 Info.writeMem = false;
10802 return true;
10803 }
Bob Wilson5549d492010-09-21 17:56:22 +000010804 default:
10805 break;
10806 }
10807
10808 return false;
10809}
Juergen Ributzka659ce002014-01-28 01:20:14 +000010810
10811/// \brief Returns true if it is beneficial to convert a load of a constant
10812/// to just the constant itself.
10813bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
10814 Type *Ty) const {
10815 assert(Ty->isIntegerTy());
10816
10817 unsigned Bits = Ty->getPrimitiveSizeInBits();
10818 if (Bits == 0 || Bits > 32)
10819 return false;
10820 return true;
10821}
Tim Northover037f26f22014-04-17 18:22:47 +000010822
10823bool ARMTargetLowering::shouldExpandAtomicInIR(Instruction *Inst) const {
10824 // Loads and stores less than 64-bits are already atomic; ones above that
10825 // are doomed anyway, so defer to the default libcall and blame the OS when
Tim Northoverb45c3b72014-06-16 18:49:36 +000010826 // things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
10827 // anything for those.
10828 bool IsMClass = Subtarget->isMClass();
10829 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
10830 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
10831 return Size == 64 && !IsMClass;
10832 } else if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
10833 return LI->getType()->getPrimitiveSizeInBits() == 64 && !IsMClass;
10834 }
Tim Northover037f26f22014-04-17 18:22:47 +000010835
Tim Northoverb45c3b72014-06-16 18:49:36 +000010836 // For the real atomic operations, we have ldrex/strex up to 32 bits,
10837 // and up to 64 bits on the non-M profiles
10838 unsigned AtomicLimit = IsMClass ? 32 : 64;
10839 return Inst->getType()->getPrimitiveSizeInBits() <= AtomicLimit;
Tim Northover037f26f22014-04-17 18:22:47 +000010840}
10841
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000010842// This has so far only been implemented for MachO.
10843bool ARMTargetLowering::useLoadStackGuardNode() const {
10844 return Subtarget->getTargetTriple().getObjectFormat() == Triple::MachO;
10845}
10846
Tim Northover037f26f22014-04-17 18:22:47 +000010847Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
10848 AtomicOrdering Ord) const {
10849 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
10850 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
10851 bool IsAcquire =
10852 Ord == Acquire || Ord == AcquireRelease || Ord == SequentiallyConsistent;
10853
10854 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
10855 // intrinsic must return {i32, i32} and we have to recombine them into a
10856 // single i64 here.
10857 if (ValTy->getPrimitiveSizeInBits() == 64) {
10858 Intrinsic::ID Int =
10859 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
10860 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
10861
10862 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
10863 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
10864
10865 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
10866 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000010867 if (!Subtarget->isLittle())
10868 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000010869 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
10870 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
10871 return Builder.CreateOr(
10872 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
10873 }
10874
10875 Type *Tys[] = { Addr->getType() };
10876 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
10877 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
10878
10879 return Builder.CreateTruncOrBitCast(
10880 Builder.CreateCall(Ldrex, Addr),
10881 cast<PointerType>(Addr->getType())->getElementType());
10882}
10883
10884Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
10885 Value *Addr,
10886 AtomicOrdering Ord) const {
10887 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
10888 bool IsRelease =
10889 Ord == Release || Ord == AcquireRelease || Ord == SequentiallyConsistent;
10890
10891 // Since the intrinsics must have legal type, the i64 intrinsics take two
10892 // parameters: "i32, i32". We must marshal Val into the appropriate form
10893 // before the call.
10894 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
10895 Intrinsic::ID Int =
10896 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
10897 Function *Strex = Intrinsic::getDeclaration(M, Int);
10898 Type *Int32Ty = Type::getInt32Ty(M->getContext());
10899
10900 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
10901 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000010902 if (!Subtarget->isLittle())
10903 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000010904 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
10905 return Builder.CreateCall3(Strex, Lo, Hi, Addr);
10906 }
10907
10908 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
10909 Type *Tys[] = { Addr->getType() };
10910 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
10911
10912 return Builder.CreateCall2(
10913 Strex, Builder.CreateZExtOrBitCast(
10914 Val, Strex->getFunctionType()->getParamType(0)),
10915 Addr);
10916}
Oliver Stannardc24f2172014-05-09 14:01:47 +000010917
10918enum HABaseType {
10919 HA_UNKNOWN = 0,
10920 HA_FLOAT,
10921 HA_DOUBLE,
10922 HA_VECT64,
10923 HA_VECT128
10924};
10925
10926static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
10927 uint64_t &Members) {
10928 if (const StructType *ST = dyn_cast<StructType>(Ty)) {
10929 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
10930 uint64_t SubMembers = 0;
10931 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
10932 return false;
10933 Members += SubMembers;
10934 }
10935 } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
10936 uint64_t SubMembers = 0;
10937 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
10938 return false;
10939 Members += SubMembers * AT->getNumElements();
10940 } else if (Ty->isFloatTy()) {
10941 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
10942 return false;
10943 Members = 1;
10944 Base = HA_FLOAT;
10945 } else if (Ty->isDoubleTy()) {
10946 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
10947 return false;
10948 Members = 1;
10949 Base = HA_DOUBLE;
10950 } else if (const VectorType *VT = dyn_cast<VectorType>(Ty)) {
10951 Members = 1;
10952 switch (Base) {
10953 case HA_FLOAT:
10954 case HA_DOUBLE:
10955 return false;
10956 case HA_VECT64:
10957 return VT->getBitWidth() == 64;
10958 case HA_VECT128:
10959 return VT->getBitWidth() == 128;
10960 case HA_UNKNOWN:
10961 switch (VT->getBitWidth()) {
10962 case 64:
10963 Base = HA_VECT64;
10964 return true;
10965 case 128:
10966 Base = HA_VECT128;
10967 return true;
10968 default:
10969 return false;
10970 }
10971 }
10972 }
10973
10974 return (Members > 0 && Members <= 4);
10975}
10976
10977/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate.
10978bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
10979 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000010980 if (getEffectiveCallingConv(CallConv, isVarArg) !=
10981 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000010982 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000010983
10984 HABaseType Base = HA_UNKNOWN;
10985 uint64_t Members = 0;
10986 bool result = isHomogeneousAggregate(Ty, Base, Members);
Justin Bognerc0087f32014-08-12 03:24:59 +000010987 DEBUG(dbgs() << "isHA: " << result << " "; Ty->dump());
Tim Northover4f1909f2014-05-27 10:43:38 +000010988 return result;
Oliver Stannardc24f2172014-05-09 14:01:47 +000010989}