blob: fc7a02f5e6bd645d7df61c45f0a187b480f05404 [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"
Eric Christopher79cc1e32014-09-02 22:28:02 +000032#include "llvm/CodeGen/MachineJumpTableInfo.h"
Bill Wendling202803e2011-10-05 00:02:33 +000033#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng10043e22007-01-19 07:51:42 +000035#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000036#include "llvm/IR/CallingConv.h"
37#include "llvm/IR/Constants.h"
38#include "llvm/IR/Function.h"
39#include "llvm/IR/GlobalValue.h"
Tim Northover037f26f22014-04-17 18:22:47 +000040#include "llvm/IR/IRBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000041#include "llvm/IR/Instruction.h"
42#include "llvm/IR/Instructions.h"
43#include "llvm/IR/Intrinsics.h"
44#include "llvm/IR/Type.h"
Bill Wendling46ffefc2010-03-09 02:46:12 +000045#include "llvm/MC/MCSectionMachO.h"
Jim Grosbach32bb3622010-04-14 22:28:31 +000046#include "llvm/Support/CommandLine.h"
Oliver Stannardc24f2172014-05-09 14:01:47 +000047#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000048#include "llvm/Support/ErrorHandling.h"
Evan Cheng2150b922007-03-12 23:30:29 +000049#include "llvm/Support/MathExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000050#include "llvm/Target/TargetOptions.h"
David Peixottoc32e24a2013-10-17 19:49:22 +000051#include <utility>
Evan Cheng10043e22007-01-19 07:51:42 +000052using namespace llvm;
53
Chandler Carruth84e68b22014-04-22 02:41:26 +000054#define DEBUG_TYPE "arm-isel"
55
Dale Johannesend679ff72010-06-03 21:09:53 +000056STATISTIC(NumTailCalls, "Number of tail calls");
Evan Cheng68aec142011-01-19 02:16:49 +000057STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Manman Ren9f911162012-06-01 02:44:42 +000058STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
Dale Johannesend679ff72010-06-03 21:09:53 +000059
Eric Christopher347f4c32010-12-15 23:47:29 +000060cl::opt<bool>
Jim Grosbach32bb3622010-04-14 22:28:31 +000061EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng25f93642010-07-08 02:08:50 +000062 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbach32bb3622010-04-14 22:28:31 +000063 cl::init(false));
64
Evan Chengf128bdc2010-06-16 07:35:02 +000065static cl::opt<bool>
66ARMInterworking("arm-interworking", cl::Hidden,
67 cl::desc("Enable / disable ARM interworking (for debugging only)"),
68 cl::init(true));
69
Benjamin Kramer7ba71be2011-11-26 23:01:57 +000070namespace {
Cameron Zwarich89019782011-06-10 20:59:24 +000071 class ARMCCState : public CCState {
72 public:
73 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
Eric Christopherb5217502014-08-06 18:45:26 +000074 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
75 ParmContext PC)
76 : CCState(CC, isVarArg, MF, locs, C) {
Cameron Zwarich89019782011-06-10 20:59:24 +000077 assert(((PC == Call) || (PC == Prologue)) &&
78 "ARMCCState users must specify whether their context is call"
79 "or prologue generation.");
80 CallOrPrologue = PC;
81 }
82 };
83}
84
Stuart Hastings45fe3c32011-04-20 16:47:52 +000085// The APCS parameter registers.
Craig Topper840beec2014-04-04 05:16:06 +000086static const MCPhysReg GPRArgRegs[] = {
Stuart Hastings45fe3c32011-04-20 16:47:52 +000087 ARM::R0, ARM::R1, ARM::R2, ARM::R3
88};
89
Craig Topper4fa625f2012-08-12 03:16:37 +000090void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT,
91 MVT PromotedBitwiseVT) {
Bob Wilson2e076c42009-06-22 23:27:02 +000092 if (VT != PromotedLdStVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +000093 setOperationAction(ISD::LOAD, VT, Promote);
94 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000095
Craig Topper4fa625f2012-08-12 03:16:37 +000096 setOperationAction(ISD::STORE, VT, Promote);
97 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000098 }
99
Craig Topper4fa625f2012-08-12 03:16:37 +0000100 MVT ElemTy = VT.getVectorElementType();
Owen Anderson9f944592009-08-11 20:47:22 +0000101 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Craig Topper4fa625f2012-08-12 03:16:37 +0000102 setOperationAction(ISD::SETCC, VT, Custom);
103 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
104 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000105 if (ElemTy == MVT::i32) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000106 setOperationAction(ISD::SINT_TO_FP, VT, Custom);
107 setOperationAction(ISD::UINT_TO_FP, VT, Custom);
108 setOperationAction(ISD::FP_TO_SINT, VT, Custom);
109 setOperationAction(ISD::FP_TO_UINT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000110 } else {
Craig Topper4fa625f2012-08-12 03:16:37 +0000111 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
112 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
113 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
114 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Bob Wilson5d8cfb22009-09-16 20:20:44 +0000115 }
Craig Topper4fa625f2012-08-12 03:16:37 +0000116 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
117 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
118 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal);
119 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
120 setOperationAction(ISD::SELECT, VT, Expand);
121 setOperationAction(ISD::SELECT_CC, VT, Expand);
Jim Grosbach30af4422012-10-12 22:59:21 +0000122 setOperationAction(ISD::VSELECT, VT, Expand);
Craig Topper4fa625f2012-08-12 03:16:37 +0000123 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000124 if (VT.isInteger()) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000125 setOperationAction(ISD::SHL, VT, Custom);
126 setOperationAction(ISD::SRA, VT, Custom);
127 setOperationAction(ISD::SRL, VT, Custom);
Bob Wilson2e076c42009-06-22 23:27:02 +0000128 }
129
130 // Promote all bit-wise operations.
131 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000132 setOperationAction(ISD::AND, VT, Promote);
133 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
134 setOperationAction(ISD::OR, VT, Promote);
135 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT);
136 setOperationAction(ISD::XOR, VT, Promote);
137 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
Bob Wilson2e076c42009-06-22 23:27:02 +0000138 }
Bob Wilson4ed397c2009-09-16 00:17:28 +0000139
140 // Neon does not support vector divide/remainder operations.
Craig Topper4fa625f2012-08-12 03:16:37 +0000141 setOperationAction(ISD::SDIV, VT, Expand);
142 setOperationAction(ISD::UDIV, VT, Expand);
143 setOperationAction(ISD::FDIV, VT, Expand);
144 setOperationAction(ISD::SREM, VT, Expand);
145 setOperationAction(ISD::UREM, VT, Expand);
146 setOperationAction(ISD::FREM, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000147}
148
Craig Topper4fa625f2012-08-12 03:16:37 +0000149void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topperc7242e02012-04-20 07:30:17 +0000150 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000151 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000152}
153
Craig Topper4fa625f2012-08-12 03:16:37 +0000154void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Jakob Stoklund Olesen20912062014-01-14 06:18:34 +0000155 addRegisterClass(VT, &ARM::DPairRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000156 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000157}
158
Eric Christopher89958332014-05-31 00:07:32 +0000159static TargetLoweringObjectFile *createTLOF(const Triple &TT) {
160 if (TT.isOSBinFormatMachO())
Bill Wendlingbbcaa402010-03-15 21:09:38 +0000161 return new TargetLoweringObjectFileMachO();
Eric Christopher89958332014-05-31 00:07:32 +0000162 if (TT.isOSWindows())
Saleem Abdulrasool46fed302014-05-17 04:28:08 +0000163 return new TargetLoweringObjectFileCOFF();
Chris Lattner4e7dfaf2009-08-02 00:34:36 +0000164 return new ARMElfTargetObjectFile();
Chris Lattner5e693ed2009-07-28 03:13:23 +0000165}
166
Eric Christopher5312afe2014-10-03 00:17:59 +0000167ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM)
Eric Christopher89958332014-05-31 00:07:32 +0000168 : TargetLowering(TM, createTLOF(Triple(TM.getTargetTriple()))) {
Evan Cheng10043e22007-01-19 07:51:42 +0000169 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopherd9134482014-08-04 21:25:23 +0000170 RegInfo = TM.getSubtargetImpl()->getRegisterInfo();
171 Itins = TM.getSubtargetImpl()->getInstrItineraryData();
Evan Cheng10043e22007-01-19 07:51:42 +0000172
Duncan Sandsf2641e12011-09-06 19:07:46 +0000173 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
174
Tim Northoverd6a729b2014-01-06 14:28:05 +0000175 if (Subtarget->isTargetMachO()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000176 // Uses VFP for Thumb libfuncs if available.
Jim Grosbach1d1d6d42013-10-24 23:07:11 +0000177 if (Subtarget->isThumb() && Subtarget->hasVFP2() &&
Tim Northover978d25f2014-04-22 10:10:09 +0000178 Subtarget->hasARMOps() && !TM.Options.UseSoftFloat) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000179 // Single-precision floating-point arithmetic.
180 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
181 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
182 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
183 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000184
Evan Chengc9f22fd12007-04-27 08:15:43 +0000185 // Double-precision floating-point arithmetic.
186 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
187 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
188 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
189 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng143576d2007-01-31 09:30:58 +0000190
Evan Chengc9f22fd12007-04-27 08:15:43 +0000191 // Single-precision comparisons.
192 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
193 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
194 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
195 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
196 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
197 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
198 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
199 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000200
Evan Chengc9f22fd12007-04-27 08:15:43 +0000201 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
202 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
203 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
204 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
205 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
206 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng143576d2007-01-31 09:30:58 +0000209
Evan Chengc9f22fd12007-04-27 08:15:43 +0000210 // Double-precision comparisons.
211 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
212 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
213 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
214 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
215 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
216 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
217 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
218 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000219
Evan Chengc9f22fd12007-04-27 08:15:43 +0000220 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
221 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
222 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
223 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
224 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
225 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
226 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
227 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Cheng10043e22007-01-19 07:51:42 +0000228
Evan Chengc9f22fd12007-04-27 08:15:43 +0000229 // Floating-point to integer conversions.
230 // i64 conversions are done via library routines even when generating VFP
231 // instructions, so use the same ones.
232 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
233 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
234 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
235 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000236
Evan Chengc9f22fd12007-04-27 08:15:43 +0000237 // Conversions between floating types.
238 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
239 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
240
241 // Integer to floating-point conversions.
242 // i64 conversions are done via library routines even when generating VFP
243 // instructions, so use the same ones.
Bob Wilsondc40d5a2009-03-20 23:16:43 +0000244 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
245 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengc9f22fd12007-04-27 08:15:43 +0000246 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
247 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
248 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
249 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
250 }
Evan Cheng10043e22007-01-19 07:51:42 +0000251 }
252
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000253 // These libcalls are not available in 32-bit.
Craig Topper062a2ba2014-04-25 05:30:21 +0000254 setLibcallName(RTLIB::SHL_I128, nullptr);
255 setLibcallName(RTLIB::SRL_I128, nullptr);
256 setLibcallName(RTLIB::SRA_I128, nullptr);
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000257
Saleem Abdulrasoolcd130822014-04-02 20:32:05 +0000258 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetMachO() &&
259 !Subtarget->isTargetWindows()) {
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000260 static const struct {
261 const RTLIB::Libcall Op;
262 const char * const Name;
263 const CallingConv::ID CC;
264 const ISD::CondCode Cond;
265 } LibraryCalls[] = {
266 // Double-precision floating-point arithmetic helper functions
267 // RTABI chapter 4.1.2, Table 2
268 { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
269 { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
270 { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
271 { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000272
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000273 // Double-precision floating-point comparison helper functions
274 // RTABI chapter 4.1.2, Table 3
275 { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
276 { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
277 { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
278 { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
279 { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
280 { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
281 { RTLIB::UO_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
282 { RTLIB::O_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000283
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000284 // Single-precision floating-point arithmetic helper functions
285 // RTABI chapter 4.1.2, Table 4
286 { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
287 { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
288 { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
289 { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000290
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000291 // Single-precision floating-point comparison helper functions
292 // RTABI chapter 4.1.2, Table 5
293 { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
294 { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
295 { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
296 { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
297 { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
298 { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
299 { RTLIB::UO_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
300 { RTLIB::O_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000301
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000302 // Floating-point to integer conversions.
303 // RTABI chapter 4.1.2, Table 6
304 { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
305 { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
306 { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
307 { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
308 { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
309 { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
310 { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
311 { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000312
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000313 // Conversions between floating types.
314 // RTABI chapter 4.1.2, Table 7
315 { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", 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 },
Chad Rosierad7c9102014-08-23 18:29:43 +0000317 { RTLIB::FPEXT_F32_F64, "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000318
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000319 // Integer to floating-point conversions.
320 // RTABI chapter 4.1.2, Table 8
321 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
322 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
323 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
324 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
325 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
326 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
327 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
328 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000329
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000330 // Long long helper functions
331 // RTABI chapter 4.2, Table 9
Chad Rosierad7c9102014-08-23 18:29:43 +0000332 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
333 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
334 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
335 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000336
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000337 // Integer division functions
338 // RTABI chapter 4.3.1
Chad Rosierad7c9102014-08-23 18:29:43 +0000339 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
340 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
341 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
342 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
343 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
344 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
345 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
346 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Renato Golin4cd51872011-05-22 21:41:23 +0000347
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000348 // Memory operations
349 // RTABI chapter 4.3.4
350 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
351 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
352 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
353 };
354
355 for (const auto &LC : LibraryCalls) {
356 setLibcallName(LC.Op, LC.Name);
357 setLibcallCallingConv(LC.Op, LC.CC);
358 if (LC.Cond != ISD::SETCC_INVALID)
359 setCmpLibcallCC(LC.Op, LC.Cond);
360 }
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +0000361 }
362
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000363 if (Subtarget->isTargetWindows()) {
364 static const struct {
365 const RTLIB::Libcall Op;
366 const char * const Name;
367 const CallingConv::ID CC;
368 } LibraryCalls[] = {
369 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP },
370 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP },
371 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP },
372 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP },
373 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP },
374 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP },
375 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP },
376 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP },
377 };
378
379 for (const auto &LC : LibraryCalls) {
380 setLibcallName(LC.Op, LC.Name);
381 setLibcallCallingConv(LC.Op, LC.CC);
382 }
383 }
384
Bob Wilsonbc158992011-10-07 16:59:21 +0000385 // Use divmod compiler-rt calls for iOS 5.0 and later.
Cameron Esfahani943908b2013-08-29 20:23:14 +0000386 if (Subtarget->getTargetTriple().isiOS() &&
Bob Wilsonbc158992011-10-07 16:59:21 +0000387 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
388 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
389 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
390 }
391
Oliver Stannard11790b22014-08-11 09:12:32 +0000392 // The half <-> float conversion functions are always soft-float, but are
393 // needed for some targets which use a hard-float calling convention by
394 // default.
395 if (Subtarget->isAAPCS_ABI()) {
396 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS);
397 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS);
398 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS);
399 } else {
400 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS);
401 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS);
402 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS);
403 }
404
David Goodwin22c2fba2009-07-08 23:10:31 +0000405 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000406 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000407 else
Craig Topperc7242e02012-04-20 07:30:17 +0000408 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000409 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
410 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000411 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000412 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Evan Cheng10043e22007-01-19 07:51:42 +0000413 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000414
Eli Friedman6f84fed2011-11-08 01:43:53 +0000415 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
416 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
417 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
418 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
419 setTruncStoreAction((MVT::SimpleValueType)VT,
420 (MVT::SimpleValueType)InnerVT, Expand);
421 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
422 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
423 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Benjamin Kramer4dae5982014-04-26 12:06:28 +0000424
425 setOperationAction(ISD::MULHS, (MVT::SimpleValueType)VT, Expand);
426 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
427 setOperationAction(ISD::MULHU, (MVT::SimpleValueType)VT, Expand);
428 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000429
430 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Eli Friedman6f84fed2011-11-08 01:43:53 +0000431 }
432
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000433 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000434 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000435
Bob Wilson2e076c42009-06-22 23:27:02 +0000436 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000437 addDRTypeForNEON(MVT::v2f32);
438 addDRTypeForNEON(MVT::v8i8);
439 addDRTypeForNEON(MVT::v4i16);
440 addDRTypeForNEON(MVT::v2i32);
441 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000442
Owen Anderson9f944592009-08-11 20:47:22 +0000443 addQRTypeForNEON(MVT::v4f32);
444 addQRTypeForNEON(MVT::v2f64);
445 addQRTypeForNEON(MVT::v16i8);
446 addQRTypeForNEON(MVT::v8i16);
447 addQRTypeForNEON(MVT::v4i32);
448 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000449
Bob Wilson194a2512009-09-15 23:55:57 +0000450 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
451 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000452 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
453 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000454 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
455 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
456 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000457 // FIXME: Code duplication: FDIV and FREM are expanded always, see
458 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000459 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
460 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000461 // FIXME: Create unittest.
462 // In another words, find a way when "copysign" appears in DAG with vector
463 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000464 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000465 // FIXME: Code duplication: SETCC has custom operation action, see
466 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000467 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000468 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000469 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
470 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
471 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
472 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
473 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
474 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
475 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
476 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
477 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
478 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
479 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
480 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000481 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000482 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
483 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
484 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
485 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
486 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000487 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000488
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000489 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
490 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
491 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
492 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
493 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
494 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
495 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
496 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
497 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
498 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000499 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
500 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
501 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
502 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000503 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000504
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000505 // Mark v2f32 intrinsics.
506 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
507 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
508 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
509 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
510 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
511 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
512 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
513 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
514 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
515 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
516 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
517 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
518 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
519 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
520 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
521
Bob Wilson6cc46572009-09-16 00:32:15 +0000522 // Neon does not support some operations on v1i64 and v2i64 types.
523 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000524 // Custom handling for some quad-vector types to detect VMULL.
525 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
526 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
527 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000528 // Custom handling for some vector types to avoid expensive expansions
529 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
530 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
531 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
532 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000533 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
534 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000535 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000536 // a destination type that is wider than the source, and nor does
537 // it have a FP_TO_[SU]INT instruction with a narrower destination than
538 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000539 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
540 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000541 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
542 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000543
Eli Friedmane6385e62012-11-15 22:44:27 +0000544 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000545 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000546
Evan Chengb4eae132012-12-04 22:41:50 +0000547 // NEON does not have single instruction CTPOP for vectors with element
548 // types wider than 8-bits. However, custom lowering can leverage the
549 // v8i8/v16i8 vcnt instruction.
550 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
551 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
552 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
553 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
554
Jim Grosbach5f215872013-02-27 21:31:12 +0000555 // NEON only has FMA instructions as of VFP4.
556 if (!Subtarget->hasVFP4()) {
557 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
558 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
559 }
560
Bob Wilson06fce872011-02-07 17:43:21 +0000561 setTargetDAGCombine(ISD::INTRINSIC_VOID);
562 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000563 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
564 setTargetDAGCombine(ISD::SHL);
565 setTargetDAGCombine(ISD::SRL);
566 setTargetDAGCombine(ISD::SRA);
567 setTargetDAGCombine(ISD::SIGN_EXTEND);
568 setTargetDAGCombine(ISD::ZERO_EXTEND);
569 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsonc6c13a32010-02-18 06:05:53 +0000570 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000571 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000572 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000573 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
574 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000575 setTargetDAGCombine(ISD::FP_TO_SINT);
576 setTargetDAGCombine(ISD::FP_TO_UINT);
577 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem097106b2011-10-15 20:03:12 +0000578
James Molloy547d4c02012-02-20 09:24:05 +0000579 // It is legal to extload from v4i8 to v4i16 or v4i32.
580 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
581 MVT::v4i16, MVT::v2i16,
582 MVT::v2i32};
583 for (unsigned i = 0; i < 6; ++i) {
584 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
585 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
586 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
587 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000588 }
589
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000590 // ARM and Thumb2 support UMLAL/SMLAL.
591 if (!Subtarget->isThumb1Only())
592 setTargetDAGCombine(ISD::ADDC);
593
Oliver Stannard51b1d462014-08-21 12:50:31 +0000594 if (Subtarget->isFPOnlySP()) {
595 // When targetting a floating-point unit with only single-precision
596 // operations, f64 is legal for the few double-precision instructions which
597 // are present However, no double-precision operations other than moves,
598 // loads and stores are provided by the hardware.
599 setOperationAction(ISD::FADD, MVT::f64, Expand);
600 setOperationAction(ISD::FSUB, MVT::f64, Expand);
601 setOperationAction(ISD::FMUL, MVT::f64, Expand);
602 setOperationAction(ISD::FMA, MVT::f64, Expand);
603 setOperationAction(ISD::FDIV, MVT::f64, Expand);
604 setOperationAction(ISD::FREM, MVT::f64, Expand);
605 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
606 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand);
607 setOperationAction(ISD::FNEG, MVT::f64, Expand);
608 setOperationAction(ISD::FABS, MVT::f64, Expand);
609 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
610 setOperationAction(ISD::FSIN, MVT::f64, Expand);
611 setOperationAction(ISD::FCOS, MVT::f64, Expand);
612 setOperationAction(ISD::FPOWI, MVT::f64, Expand);
613 setOperationAction(ISD::FPOW, MVT::f64, Expand);
614 setOperationAction(ISD::FLOG, MVT::f64, Expand);
615 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
616 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
617 setOperationAction(ISD::FEXP, MVT::f64, Expand);
618 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
619 setOperationAction(ISD::FCEIL, MVT::f64, Expand);
620 setOperationAction(ISD::FTRUNC, MVT::f64, Expand);
621 setOperationAction(ISD::FRINT, MVT::f64, Expand);
622 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
623 setOperationAction(ISD::FFLOOR, MVT::f64, Expand);
624 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
625 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom);
626 }
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000627
Evan Cheng6addd652007-05-18 00:19:34 +0000628 computeRegisterProperties();
Evan Cheng10043e22007-01-19 07:51:42 +0000629
Tim Northover4e80b582014-07-18 13:01:19 +0000630 // ARM does not have floating-point extending loads.
Owen Anderson9f944592009-08-11 20:47:22 +0000631 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Tim Northover4e80b582014-07-18 13:01:19 +0000632 setLoadExtAction(ISD::EXTLOAD, MVT::f16, Expand);
633
634 // ... or truncating stores
635 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
636 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
637 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000638
Duncan Sands95d46ef2008-01-23 20:39:46 +0000639 // ARM does not have i1 sign extending load.
Owen Anderson9f944592009-08-11 20:47:22 +0000640 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000641
Evan Cheng10043e22007-01-19 07:51:42 +0000642 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000643 if (!Subtarget->isThumb1Only()) {
644 for (unsigned im = (unsigned)ISD::PRE_INC;
645 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000646 setIndexedLoadAction(im, MVT::i1, Legal);
647 setIndexedLoadAction(im, MVT::i8, Legal);
648 setIndexedLoadAction(im, MVT::i16, Legal);
649 setIndexedLoadAction(im, MVT::i32, Legal);
650 setIndexedStoreAction(im, MVT::i1, Legal);
651 setIndexedStoreAction(im, MVT::i8, Legal);
652 setIndexedStoreAction(im, MVT::i16, Legal);
653 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000654 }
Evan Cheng10043e22007-01-19 07:51:42 +0000655 }
656
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000657 setOperationAction(ISD::SADDO, MVT::i32, Custom);
658 setOperationAction(ISD::UADDO, MVT::i32, Custom);
659 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
660 setOperationAction(ISD::USUBO, MVT::i32, Custom);
661
Evan Cheng10043e22007-01-19 07:51:42 +0000662 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000663 setOperationAction(ISD::MUL, MVT::i64, Expand);
664 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000665 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000666 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
667 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000668 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000669 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
670 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000671 setOperationAction(ISD::MULHS, MVT::i32, Expand);
672
Jim Grosbach5d994042009-10-31 19:38:01 +0000673 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000674 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000675 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000676 setOperationAction(ISD::SRL, MVT::i64, Custom);
677 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000678
Evan Chenge8916542011-08-30 01:34:54 +0000679 if (!Subtarget->isThumb1Only()) {
680 // FIXME: We should do this for Thumb1 as well.
681 setOperationAction(ISD::ADDC, MVT::i32, Custom);
682 setOperationAction(ISD::ADDE, MVT::i32, Custom);
683 setOperationAction(ISD::SUBC, MVT::i32, Custom);
684 setOperationAction(ISD::SUBE, MVT::i32, Custom);
685 }
686
Evan Cheng10043e22007-01-19 07:51:42 +0000687 // ARM does not have ROTL.
Owen Anderson9f944592009-08-11 20:47:22 +0000688 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach8546ec92010-01-18 19:58:49 +0000689 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000690 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000691 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000692 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000693
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000694 // These just redirect to CTTZ and CTLZ on ARM.
695 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
696 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
697
Tim Northoverbc933082013-05-23 19:11:20 +0000698 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
699
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000700 // Only ARMv6 has BSWAP.
701 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000702 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000703
Bob Wilsone8a549c2012-09-29 21:43:49 +0000704 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
705 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
706 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbach92d999002010-05-05 20:44:35 +0000707 setOperationAction(ISD::SDIV, MVT::i32, Expand);
708 setOperationAction(ISD::UDIV, MVT::i32, Expand);
709 }
Renato Golin87610692013-07-16 09:32:17 +0000710
711 // FIXME: Also set divmod for SREM on EABI
Chad Rosierad7c9102014-08-23 18:29:43 +0000712 setOperationAction(ISD::SREM, MVT::i32, Expand);
713 setOperationAction(ISD::UREM, MVT::i32, Expand);
714 // Register based DivRem for AEABI (RTABI 4.2)
715 if (Subtarget->isTargetAEABI()) {
716 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
717 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
718 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
719 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
720 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
721 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
722 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
723 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
724
725 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
726 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
727 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
728 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
729 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
730 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
731 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
732 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
733
734 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
735 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
736 } else {
Renato Golin87610692013-07-16 09:32:17 +0000737 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
738 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
739 }
Bob Wilson7117a912009-03-20 22:42:55 +0000740
Owen Anderson9f944592009-08-11 20:47:22 +0000741 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
742 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
743 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
744 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000745 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000746
Evan Cheng74d92c12011-04-08 21:37:21 +0000747 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000748
Evan Cheng10043e22007-01-19 07:51:42 +0000749 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000750 setOperationAction(ISD::VASTART, MVT::Other, Custom);
751 setOperationAction(ISD::VAARG, MVT::Other, Expand);
752 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
753 setOperationAction(ISD::VAEND, MVT::Other, Expand);
754 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
755 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000756
Tim Northoverd6a729b2014-01-06 14:28:05 +0000757 if (!Subtarget->isTargetMachO()) {
758 // Non-MachO platforms may return values in these registers via the
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000759 // personality function.
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000760 setExceptionPointerRegister(ARM::R0);
761 setExceptionSelectorRegister(ARM::R1);
762 }
Anton Korobeynikovf3a62312011-01-24 22:38:45 +0000763
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +0000764 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
765 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
766 else
767 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
768
Evan Cheng6e809de2010-08-11 06:22:01 +0000769 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000770 // the default expansion. If we are targeting a single threaded system,
771 // then set them all for expand so we can lower them later into their
772 // non-atomic form.
773 if (TM.Options.ThreadModel == ThreadModel::Single)
774 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
775 else if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000776 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
777 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000778 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000779
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000780 // On v8, we have particularly efficient implementations of atomic fences
781 // if they can be combined with nearby atomic loads and stores.
782 if (!Subtarget->hasV8Ops()) {
Robin Morissetd18cda62014-08-15 22:17:28 +0000783 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc.
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000784 setInsertFencesForAtomic(true);
785 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000786 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000787 // If there's anything we can use as a barrier, go through custom lowering
788 // for ATOMIC_FENCE.
789 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
790 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
791
Jim Grosbach6860bb72010-06-18 22:35:32 +0000792 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000793 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000794 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000795 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000796 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000797 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000798 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000799 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000800 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000801 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000802 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000803 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000804 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000805 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
806 // Unordered/Monotonic case.
807 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
808 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000809 }
Evan Cheng10043e22007-01-19 07:51:42 +0000810
Evan Cheng21acf9f2010-11-04 05:19:35 +0000811 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000812
Eli Friedman8cfa7712010-06-26 04:36:50 +0000813 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
814 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000815 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
816 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000817 }
Owen Anderson9f944592009-08-11 20:47:22 +0000818 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000819
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000820 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
821 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000822 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000823 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000824 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000825 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
826 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000827
828 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000829 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbach31984832010-07-07 00:07:57 +0000830 if (Subtarget->isTargetDarwin()) {
831 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
832 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall7d84ece2011-05-29 19:50:32 +0000833 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbach31984832010-07-07 00:07:57 +0000834 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000835
Owen Anderson9f944592009-08-11 20:47:22 +0000836 setOperationAction(ISD::SETCC, MVT::i32, Expand);
837 setOperationAction(ISD::SETCC, MVT::f32, Expand);
838 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000839 setOperationAction(ISD::SELECT, MVT::i32, Custom);
840 setOperationAction(ISD::SELECT, MVT::f32, Custom);
841 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000842 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
843 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
844 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000845
Owen Anderson9f944592009-08-11 20:47:22 +0000846 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
847 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
848 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
849 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
850 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000851
Dan Gohman482732a2007-10-11 23:21:31 +0000852 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000853 setOperationAction(ISD::FSIN, MVT::f64, Expand);
854 setOperationAction(ISD::FSIN, MVT::f32, Expand);
855 setOperationAction(ISD::FCOS, MVT::f32, Expand);
856 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000857 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
858 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000859 setOperationAction(ISD::FREM, MVT::f64, Expand);
860 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000861 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
862 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000863 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
864 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000865 }
Owen Anderson9f944592009-08-11 20:47:22 +0000866 setOperationAction(ISD::FPOW, MVT::f64, Expand);
867 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000868
Evan Chengd0007f32012-04-10 21:40:28 +0000869 if (!Subtarget->hasVFP4()) {
870 setOperationAction(ISD::FMA, MVT::f64, Expand);
871 setOperationAction(ISD::FMA, MVT::f32, Expand);
872 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000873
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000874 // Various VFP goodness
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000875 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilsone4191e72010-03-19 22:51:32 +0000876 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
877 if (Subtarget->hasVFP2()) {
878 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
879 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
880 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
881 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
882 }
Tim Northover53f3bcf2014-07-17 11:27:04 +0000883
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000884 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded.
885 if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) {
Tim Northover53f3bcf2014-07-17 11:27:04 +0000886 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
887 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
888 }
889
890 // fp16 is a special v7 extension that adds f16 <-> f32 conversions.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000891 if (!Subtarget->hasFP16()) {
Tim Northoverfd7e4242014-07-17 10:51:23 +0000892 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
893 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000894 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000895 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000896
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000897 // Combine sin / cos into one node or libcall if possible.
898 if (Subtarget->hasSinCos()) {
899 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
900 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Bob Wilson9868d712014-10-09 05:43:30 +0000901 if (Subtarget->getTargetTriple().isiOS()) {
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000902 // For iOS, we don't want to the normal expansion of a libcall to
903 // sincos. We want to issue a libcall to __sincos_stret.
904 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
905 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
906 }
907 }
Evan Cheng10043e22007-01-19 07:51:42 +0000908
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000909 // FP-ARMv8 implements a lot of rounding-like FP operations.
910 if (Subtarget->hasFPARMv8()) {
911 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
912 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
913 setOperationAction(ISD::FROUND, MVT::f32, Legal);
914 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
915 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
916 setOperationAction(ISD::FRINT, MVT::f32, Legal);
917 if (!Subtarget->isFPOnlySP()) {
918 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
919 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
920 setOperationAction(ISD::FROUND, MVT::f64, Legal);
921 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
922 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
923 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Chad Rosierb1bbf6f2014-08-15 21:38:16 +0000924 }
925 }
Chris Lattnerf3f4ad92007-11-27 22:36:16 +0000926 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000927 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +0000928 setTargetDAGCombine(ISD::ADD);
929 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +0000930 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +0000931 setTargetDAGCombine(ISD::AND);
932 setTargetDAGCombine(ISD::OR);
933 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +0000934
Evan Chengf258a152012-02-23 02:58:19 +0000935 if (Subtarget->hasV6Ops())
936 setTargetDAGCombine(ISD::SRL);
937
Evan Cheng10043e22007-01-19 07:51:42 +0000938 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +0000939
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000940 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
941 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +0000942 setSchedulingPreference(Sched::RegPressure);
943 else
944 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +0000945
Evan Cheng3ae2b792011-01-06 06:52:41 +0000946 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000947 MaxStoresPerMemset = 8;
948 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
949 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
950 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
951 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
952 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
Evan Chengb71233f2010-06-26 01:52:05 +0000953
Rafael Espindolaa76eccf2010-07-11 04:01:49 +0000954 // On ARM arguments smaller than 4 bytes are extended, so all arguments
955 // are at least 4 bytes aligned.
956 setMinStackArgumentAlignment(4);
957
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000958 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000959 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000960
Eli Friedman2518f832011-05-06 20:34:06 +0000961 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +0000962}
963
Andrew Trick43f25632011-01-19 02:35:27 +0000964// FIXME: It might make sense to define the representative register class as the
965// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
966// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
967// SPR's representative would be DPR_VFP2. This should work well if register
968// pressure tracking were modified such that a register use would increment the
969// pressure of the register class's representative and all of it's super
970// classes' representatives transitively. We have not implemented this because
971// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000972// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +0000973// and extractions.
Evan Chenga77f3d32010-07-21 06:09:07 +0000974std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000975ARMTargetLowering::findRepresentativeClass(MVT VT) const{
Craig Topper062a2ba2014-04-25 05:30:21 +0000976 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +0000977 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000978 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +0000979 default:
Evan Chenga77f3d32010-07-21 06:09:07 +0000980 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng28590382010-07-21 23:53:58 +0000981 // Use DPR as representative register class for all floating point
982 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
983 // the cost is 1 for both f32 and f64.
984 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +0000985 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +0000986 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +0000987 // When NEON is used for SP, only half of the register file is available
988 // because operations that define both SP and DP results will be constrained
989 // to the VFP2 class (D0-D15). We currently model this constraint prior to
990 // coalescing by double-counting the SP regs. See the FIXME above.
991 if (Subtarget->useNEONForSinglePrecisionFP())
992 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000993 break;
994 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
995 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +0000996 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000997 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000998 break;
999 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001000 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001001 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +00001002 break;
1003 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001004 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001005 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +00001006 break;
Evan Cheng10f99a32010-07-19 22:15:08 +00001007 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001008 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +00001009}
1010
Evan Cheng10043e22007-01-19 07:51:42 +00001011const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
1012 switch (Opcode) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001013 default: return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +00001014 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +00001015 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +00001016 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
1017 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +00001018 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +00001019 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
1020 case ARMISD::tCALL: return "ARMISD::tCALL";
1021 case ARMISD::BRCOND: return "ARMISD::BRCOND";
1022 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +00001023 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +00001024 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +00001025 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +00001026 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
1027 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +00001028 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +00001029 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +00001030 case ARMISD::CMPFP: return "ARMISD::CMPFP";
1031 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +00001032 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +00001033 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +00001034
Evan Cheng10043e22007-01-19 07:51:42 +00001035 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +00001036
Jim Grosbach8546ec92010-01-18 19:58:49 +00001037 case ARMISD::RBIT: return "ARMISD::RBIT";
1038
Bob Wilsone4191e72010-03-19 22:51:32 +00001039 case ARMISD::FTOSI: return "ARMISD::FTOSI";
1040 case ARMISD::FTOUI: return "ARMISD::FTOUI";
1041 case ARMISD::SITOF: return "ARMISD::SITOF";
1042 case ARMISD::UITOF: return "ARMISD::UITOF";
1043
Evan Cheng10043e22007-01-19 07:51:42 +00001044 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
1045 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
1046 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +00001047
Evan Chenge8916542011-08-30 01:34:54 +00001048 case ARMISD::ADDC: return "ARMISD::ADDC";
1049 case ARMISD::ADDE: return "ARMISD::ADDE";
1050 case ARMISD::SUBC: return "ARMISD::SUBC";
1051 case ARMISD::SUBE: return "ARMISD::SUBE";
1052
Bob Wilson22806742010-09-22 22:09:21 +00001053 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
1054 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001055
Evan Chengec6d7c92009-10-28 06:55:03 +00001056 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
1057 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
1058
Dale Johannesend679ff72010-06-03 21:09:53 +00001059 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001060
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001061 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001062
Evan Chengb972e562009-08-07 00:34:42 +00001063 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1064
Bob Wilson7ed59712010-10-30 00:54:37 +00001065 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001066
Evan Cheng8740ee32010-11-03 06:34:55 +00001067 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1068
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001069 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK";
1070
Bob Wilson2e076c42009-06-22 23:27:02 +00001071 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001072 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001073 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001074 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1075 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001076 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1077 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001078 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1079 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001080 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1081 case ARMISD::VTST: return "ARMISD::VTST";
1082
1083 case ARMISD::VSHL: return "ARMISD::VSHL";
1084 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1085 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001086 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1087 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1088 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1089 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1090 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1091 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1092 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1093 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1094 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1095 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1096 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1097 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
1098 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1099 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001100 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001101 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001102 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001103 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001104 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001105 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001106 case ARMISD::VREV64: return "ARMISD::VREV64";
1107 case ARMISD::VREV32: return "ARMISD::VREV32";
1108 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001109 case ARMISD::VZIP: return "ARMISD::VZIP";
1110 case ARMISD::VUZP: return "ARMISD::VUZP";
1111 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001112 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1113 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001114 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1115 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001116 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1117 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001118 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilsonc6c13a32010-02-18 06:05:53 +00001119 case ARMISD::FMAX: return "ARMISD::FMAX";
1120 case ARMISD::FMIN: return "ARMISD::FMIN";
Joey Goulye3dd6842013-08-23 12:01:13 +00001121 case ARMISD::VMAXNM: return "ARMISD::VMAX";
1122 case ARMISD::VMINNM: return "ARMISD::VMIN";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001123 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001124 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1125 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001126 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilson2d790df2010-11-28 06:51:26 +00001127 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1128 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1129 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001130 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1131 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1132 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1133 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1134 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1135 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1136 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1137 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1138 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1139 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1140 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1141 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1142 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1143 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1144 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1145 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1146 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001147 }
1148}
1149
Matt Arsenault758659232013-05-18 00:21:46 +00001150EVT ARMTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Duncan Sandsf2641e12011-09-06 19:07:46 +00001151 if (!VT.isVector()) return getPointerTy();
1152 return VT.changeVectorElementTypeToInteger();
1153}
1154
Evan Cheng4cad68e2010-05-15 02:18:07 +00001155/// getRegClassFor - Return the register class that should be used for the
1156/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001157const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001158 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1159 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1160 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001161 if (Subtarget->hasNEON()) {
1162 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001163 return &ARM::QQPRRegClass;
1164 if (VT == MVT::v8i64)
1165 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001166 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001167 return TargetLowering::getRegClassFor(VT);
1168}
1169
Eric Christopher84bdfd82010-07-21 22:26:11 +00001170// Create a fast isel object.
1171FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001172ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1173 const TargetLibraryInfo *libInfo) const {
1174 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001175}
1176
Anton Korobeynikov19edda02010-07-24 21:52:08 +00001177/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1178/// be used for loads / stores from the global.
1179unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1180 return (Subtarget->isThumb1Only() ? 127 : 4095);
1181}
1182
Evan Cheng4401f882010-05-20 23:26:43 +00001183Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001184 unsigned NumVals = N->getNumValues();
1185 if (!NumVals)
1186 return Sched::RegPressure;
1187
1188 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001189 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001190 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001191 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001192 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001193 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001194 }
Evan Chengbf914992010-05-28 23:25:23 +00001195
1196 if (!N->isMachineOpcode())
1197 return Sched::RegPressure;
1198
1199 // Load are scheduled for latency even if there instruction itinerary
1200 // is not available.
Eric Christopherd9134482014-08-04 21:25:23 +00001201 const TargetInstrInfo *TII =
1202 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001203 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001204
Evan Cheng6cc775f2011-06-28 19:10:37 +00001205 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001206 return Sched::RegPressure;
1207 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001208 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001209 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001210
Evan Cheng4401f882010-05-20 23:26:43 +00001211 return Sched::RegPressure;
1212}
1213
Evan Cheng10043e22007-01-19 07:51:42 +00001214//===----------------------------------------------------------------------===//
1215// Lowering Code
1216//===----------------------------------------------------------------------===//
1217
Evan Cheng10043e22007-01-19 07:51:42 +00001218/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1219static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1220 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001221 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001222 case ISD::SETNE: return ARMCC::NE;
1223 case ISD::SETEQ: return ARMCC::EQ;
1224 case ISD::SETGT: return ARMCC::GT;
1225 case ISD::SETGE: return ARMCC::GE;
1226 case ISD::SETLT: return ARMCC::LT;
1227 case ISD::SETLE: return ARMCC::LE;
1228 case ISD::SETUGT: return ARMCC::HI;
1229 case ISD::SETUGE: return ARMCC::HS;
1230 case ISD::SETULT: return ARMCC::LO;
1231 case ISD::SETULE: return ARMCC::LS;
1232 }
1233}
1234
Bob Wilsona2e83332009-09-09 23:14:54 +00001235/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1236static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001237 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001238 CondCode2 = ARMCC::AL;
1239 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001240 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001241 case ISD::SETEQ:
1242 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1243 case ISD::SETGT:
1244 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1245 case ISD::SETGE:
1246 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1247 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001248 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001249 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1250 case ISD::SETO: CondCode = ARMCC::VC; break;
1251 case ISD::SETUO: CondCode = ARMCC::VS; break;
1252 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1253 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1254 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1255 case ISD::SETLT:
1256 case ISD::SETULT: CondCode = ARMCC::LT; break;
1257 case ISD::SETLE:
1258 case ISD::SETULE: CondCode = ARMCC::LE; break;
1259 case ISD::SETNE:
1260 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1261 }
Evan Cheng10043e22007-01-19 07:51:42 +00001262}
1263
Bob Wilsona4c22902009-04-17 19:07:39 +00001264//===----------------------------------------------------------------------===//
1265// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001266//===----------------------------------------------------------------------===//
1267
1268#include "ARMGenCallingConv.inc"
1269
Oliver Stannardc24f2172014-05-09 14:01:47 +00001270/// getEffectiveCallingConv - Get the effective calling convention, taking into
1271/// account presence of floating point hardware and calling convention
1272/// limitations, such as support for variadic functions.
1273CallingConv::ID
1274ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1275 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001276 switch (CC) {
1277 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001278 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001279 case CallingConv::ARM_AAPCS:
1280 case CallingConv::ARM_APCS:
1281 case CallingConv::GHC:
1282 return CC;
1283 case CallingConv::ARM_AAPCS_VFP:
1284 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1285 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001286 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001287 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001288 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001289 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1290 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001291 return CallingConv::ARM_AAPCS_VFP;
1292 else
1293 return CallingConv::ARM_AAPCS;
1294 case CallingConv::Fast:
1295 if (!Subtarget->isAAPCS_ABI()) {
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001296 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001297 return CallingConv::Fast;
1298 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001299 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001300 return CallingConv::ARM_AAPCS_VFP;
1301 else
1302 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001303 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001304}
1305
1306/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1307/// CallingConvention.
1308CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1309 bool Return,
1310 bool isVarArg) const {
1311 switch (getEffectiveCallingConv(CC, isVarArg)) {
1312 default:
1313 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001314 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001315 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001316 case CallingConv::ARM_AAPCS:
1317 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1318 case CallingConv::ARM_AAPCS_VFP:
1319 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1320 case CallingConv::Fast:
1321 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001322 case CallingConv::GHC:
1323 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001324 }
1325}
1326
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001327/// LowerCallResult - Lower the result values of a call into the
1328/// appropriate copies out of appropriate physical registers.
1329SDValue
1330ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001331 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001332 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001333 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001334 SmallVectorImpl<SDValue> &InVals,
1335 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001336
Bob Wilsona4c22902009-04-17 19:07:39 +00001337 // Assign locations to each value returned by this call.
1338 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001339 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1340 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001341 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001342 CCAssignFnForNode(CallConv, /* Return*/ true,
1343 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001344
1345 // Copy all of the result registers out of their specified physreg.
1346 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1347 CCValAssign VA = RVLocs[i];
1348
Stephen Linb8bd2322013-04-20 05:14:40 +00001349 // Pass 'this' value directly from the argument to return value, to avoid
1350 // reg unit interference
1351 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001352 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1353 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001354 InVals.push_back(ThisVal);
1355 continue;
1356 }
1357
Bob Wilson0041bd32009-04-25 00:33:20 +00001358 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001359 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001360 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001361 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001362 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001363 Chain = Lo.getValue(1);
1364 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001365 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001366 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001367 InFlag);
1368 Chain = Hi.getValue(1);
1369 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001370 if (!Subtarget->isLittle())
1371 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001372 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001373
Owen Anderson9f944592009-08-11 20:47:22 +00001374 if (VA.getLocVT() == MVT::v2f64) {
1375 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1376 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1377 DAG.getConstant(0, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001378
1379 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001380 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001381 Chain = Lo.getValue(1);
1382 InFlag = Lo.getValue(2);
1383 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001384 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001385 Chain = Hi.getValue(1);
1386 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001387 if (!Subtarget->isLittle())
1388 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001389 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001390 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1391 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001392 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001393 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001394 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1395 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001396 Chain = Val.getValue(1);
1397 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001398 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001399
1400 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001401 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001402 case CCValAssign::Full: break;
1403 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001404 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001405 break;
1406 }
1407
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001408 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001409 }
1410
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001411 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001412}
1413
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001414/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001415SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001416ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1417 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001418 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001419 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001420 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001421 unsigned LocMemOffset = VA.getLocMemOffset();
1422 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1423 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilsona4c22902009-04-17 19:07:39 +00001424 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner886250c2010-09-21 18:51:21 +00001425 MachinePointerInfo::getStack(LocMemOffset),
David Greene0d0149f2010-02-15 16:55:24 +00001426 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001427}
1428
Andrew Trickef9de2a2013-05-25 02:42:55 +00001429void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001430 SDValue Chain, SDValue &Arg,
1431 RegsToPassVector &RegsToPass,
1432 CCValAssign &VA, CCValAssign &NextVA,
1433 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001434 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001435 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001436
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001437 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001438 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001439 unsigned id = Subtarget->isLittle() ? 0 : 1;
1440 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001441
1442 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001443 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001444 else {
1445 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001446 if (!StackPtr.getNode())
Bob Wilson2e076c42009-06-22 23:27:02 +00001447 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1448
Christian Pirkerb5728192014-05-08 14:06:24 +00001449 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001450 dl, DAG, NextVA,
1451 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001452 }
1453}
1454
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001455/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001456/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1457/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001458SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001459ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001460 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001461 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001462 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001463 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1464 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1465 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001466 SDValue Chain = CLI.Chain;
1467 SDValue Callee = CLI.Callee;
1468 bool &isTailCall = CLI.IsTailCall;
1469 CallingConv::ID CallConv = CLI.CallConv;
1470 bool doesNotRet = CLI.DoesNotReturn;
1471 bool isVarArg = CLI.IsVarArg;
1472
Dale Johannesend679ff72010-06-03 21:09:53 +00001473 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001474 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1475 bool isThisReturn = false;
1476 bool isSibCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001477
Bob Wilson8decdc42011-10-07 17:17:49 +00001478 // Disable tail calls if they're not supported.
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001479 if (!Subtarget->supportsTailCall() || MF.getTarget().Options.DisableTailCalls)
Bob Wilson3c9ed762010-08-13 22:43:33 +00001480 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001481
Dale Johannesend679ff72010-06-03 21:09:53 +00001482 if (isTailCall) {
1483 // Check if it's really possible to do a tail call.
1484 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001485 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001486 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001487 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1488 report_fatal_error("failed to perform tail call elimination on a call "
1489 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001490 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1491 // detected sibcalls.
1492 if (isTailCall) {
1493 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001494 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001495 }
1496 }
Evan Cheng10043e22007-01-19 07:51:42 +00001497
Bob Wilsona4c22902009-04-17 19:07:39 +00001498 // Analyze operands of the call, assigning locations to each operand.
1499 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001500 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1501 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001502 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001503 CCAssignFnForNode(CallConv, /* Return*/ false,
1504 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001505
Bob Wilsona4c22902009-04-17 19:07:39 +00001506 // Get a count of how many bytes are to be pushed on the stack.
1507 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001508
Dale Johannesend679ff72010-06-03 21:09:53 +00001509 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001510 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001511 NumBytes = 0;
1512
Evan Cheng10043e22007-01-19 07:51:42 +00001513 // Adjust the stack pointer for the new arguments...
1514 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001515 if (!isSibCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00001516 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
1517 dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001518
Jim Grosbach6ad4bcb2010-02-24 01:43:03 +00001519 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Cheng10043e22007-01-19 07:51:42 +00001520
Bob Wilson2e076c42009-06-22 23:27:02 +00001521 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001522 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001523
Bob Wilsona4c22902009-04-17 19:07:39 +00001524 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001525 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001526 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1527 i != e;
1528 ++i, ++realArgIdx) {
1529 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001530 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001531 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001532 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001533
Bob Wilsona4c22902009-04-17 19:07:39 +00001534 // Promote the value if needed.
1535 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001536 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001537 case CCValAssign::Full: break;
1538 case CCValAssign::SExt:
1539 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1540 break;
1541 case CCValAssign::ZExt:
1542 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1543 break;
1544 case CCValAssign::AExt:
1545 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1546 break;
1547 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001548 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001549 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001550 }
1551
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001552 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001553 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001554 if (VA.getLocVT() == MVT::v2f64) {
1555 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1556 DAG.getConstant(0, MVT::i32));
1557 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1558 DAG.getConstant(1, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001559
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001560 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001561 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1562
1563 VA = ArgLocs[++i]; // skip ahead to next loc
1564 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001565 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001566 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1567 } else {
1568 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001569
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001570 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1571 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001572 }
1573 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001574 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001575 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001576 }
1577 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001578 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1579 assert(VA.getLocVT() == MVT::i32 &&
1580 "unexpected calling convention register assignment");
1581 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001582 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001583 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001584 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001585 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001586 } else if (isByVal) {
1587 assert(VA.isMemLoc());
1588 unsigned offset = 0;
1589
1590 // True if this byval aggregate will be split between registers
1591 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001592 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
1593 unsigned CurByValIdx = CCInfo.getInRegsParamsProceed();
1594
1595 if (CurByValIdx < ByValArgsCount) {
1596
1597 unsigned RegBegin, RegEnd;
1598 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1599
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001600 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1601 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001602 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001603 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1604 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1605 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1606 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001607 false, false, false,
1608 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001609 MemOpChains.push_back(Load.getValue(1));
1610 RegsToPass.push_back(std::make_pair(j, Load));
1611 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001612
1613 // If parameter size outsides register area, "offset" value
1614 // helps us to calculate stack slot for remained part properly.
1615 offset = RegEnd - RegBegin;
1616
1617 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001618 }
1619
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001620 if (Flags.getByValSize() > 4*offset) {
Manman Ren9f911162012-06-01 02:44:42 +00001621 unsigned LocMemOffset = VA.getLocMemOffset();
1622 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1623 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1624 StkPtrOff);
1625 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1626 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1627 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1628 MVT::i32);
Manman Rene8735522012-06-01 19:33:18 +00001629 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001630
Manman Ren9f911162012-06-01 02:44:42 +00001631 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001632 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001633 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001634 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001635 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001636 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001637 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001638
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001639 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1640 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001641 }
Evan Cheng10043e22007-01-19 07:51:42 +00001642 }
1643
1644 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001645 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001646
1647 // Build a sequence of copy-to-reg nodes chained together with token chain
1648 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001649 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001650 // Tail call byval lowering might overwrite argument registers so in case of
1651 // tail call optimization the copies to registers are lowered later.
1652 if (!isTailCall)
1653 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1654 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1655 RegsToPass[i].second, InFlag);
1656 InFlag = Chain.getValue(1);
1657 }
Evan Cheng10043e22007-01-19 07:51:42 +00001658
Dale Johannesend679ff72010-06-03 21:09:53 +00001659 // For tail calls lower the arguments to the 'real' stack slot.
1660 if (isTailCall) {
1661 // Force all the incoming stack arguments to be loaded from the stack
1662 // before any new outgoing arguments are stored to the stack, because the
1663 // outgoing stack slots may alias the incoming argument stack slots, and
1664 // the alias isn't otherwise explicit. This is slightly more conservative
1665 // than necessary, because it means that each store effectively depends
1666 // on every argument instead of just those arguments it would clobber.
1667
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001668 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001669 InFlag = SDValue();
1670 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1671 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1672 RegsToPass[i].second, InFlag);
1673 InFlag = Chain.getValue(1);
1674 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001675 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001676 }
1677
Bill Wendling24c79f22008-09-16 21:48:12 +00001678 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1679 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1680 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001681 bool isDirect = false;
1682 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001683 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001684 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001685
1686 if (EnableARMLongCalls) {
Saleem Abdulrasool90386ad2014-06-07 20:29:27 +00001687 assert((Subtarget->isTargetWindows() ||
1688 getTargetMachine().getRelocationModel() == Reloc::Static) &&
1689 "long-calls with non-static relocation model!");
Jim Grosbach32bb3622010-04-14 22:28:31 +00001690 // Handle a global address or an external symbol. If it's not one of
1691 // those, the target's already in a register, so we don't need to do
1692 // anything extra.
1693 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001694 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001695 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001696 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001697 ARMConstantPoolValue *CPV =
1698 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1699
Jim Grosbach32bb3622010-04-14 22:28:31 +00001700 // Get the address of the callee into a register
1701 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1702 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1703 Callee = DAG.getLoad(getPointerTy(), dl,
1704 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001705 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001706 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001707 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1708 const char *Sym = S->getSymbol();
1709
1710 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001711 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001712 ARMConstantPoolValue *CPV =
1713 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1714 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001715 // Get the address of the callee into a register
1716 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1717 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1718 Callee = DAG.getLoad(getPointerTy(), dl,
1719 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001720 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001721 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001722 }
1723 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001724 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001725 isDirect = true;
Chris Lattner55452c22009-07-15 04:12:33 +00001726 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Tim Northoverd6a729b2014-01-06 14:28:05 +00001727 bool isStub = (isExt && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001728 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001729 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Chengc3c949b42007-06-19 21:05:09 +00001730 // ARM call to a local ARM function is predicable.
Evan Chengf128bdc2010-06-16 07:35:02 +00001731 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001732 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001733 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001734 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Tim Northover72360d22013-12-02 10:35:41 +00001735 Callee = DAG.getNode(ARMISD::WrapperPIC, dl, getPointerTy(),
Tim Northoverd4d294d2014-08-06 11:13:06 +00001736 DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
1737 0, ARMII::MO_NONLAZY));
1738 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
1739 MachinePointerInfo::getGOT(), false, false, true, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001740 } else if (Subtarget->isTargetCOFF()) {
1741 assert(Subtarget->isTargetWindows() &&
1742 "Windows is the only supported COFF target");
1743 unsigned TargetFlags = GV->hasDLLImportStorageClass()
1744 ? ARMII::MO_DLLIMPORT
1745 : ARMII::MO_NO_FLAG;
1746 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), /*Offset=*/0,
1747 TargetFlags);
1748 if (GV->hasDLLImportStorageClass())
1749 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
1750 DAG.getNode(ARMISD::Wrapper, dl, getPointerTy(),
1751 Callee), MachinePointerInfo::getGOT(),
1752 false, false, false, 0);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001753 } else {
1754 // On ELF targets for PIC code, direct calls should go through the PLT
1755 unsigned OpFlags = 0;
1756 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001757 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001758 OpFlags = ARMII::MO_PLT;
1759 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1760 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001761 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001762 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001763 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001764 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001765 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Cheng83f35172007-01-30 20:37:08 +00001766 // tBX takes a register source operand.
1767 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001768 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001769 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001770 ARMConstantPoolValue *CPV =
1771 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1772 ARMPCLabelIndex, 4);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001773 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001774 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen021052a2009-02-04 20:06:27 +00001775 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Chengcdbb70c2009-10-31 03:39:36 +00001776 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001777 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001778 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00001779 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson7117a912009-03-20 22:42:55 +00001780 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen021052a2009-02-04 20:06:27 +00001781 getPointerTy(), Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001782 } else {
1783 unsigned OpFlags = 0;
1784 // On ELF targets for PIC code, direct calls should go through the PLT
1785 if (Subtarget->isTargetELF() &&
1786 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1787 OpFlags = ARMII::MO_PLT;
1788 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1789 }
Evan Cheng10043e22007-01-19 07:51:42 +00001790 }
1791
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001792 // FIXME: handle tail calls differently.
1793 unsigned CallOpc;
Eric Christopherc1058df2014-07-04 01:55:26 +00001794 bool HasMinSizeAttr = MF.getFunction()->getAttributes().hasAttribute(
1795 AttributeSet::FunctionIndex, Attribute::MinSize);
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001796 if (Subtarget->isThumb()) {
1797 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001798 CallOpc = ARMISD::CALL_NOLINK;
1799 else
1800 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1801 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001802 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001803 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001804 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet8e1fe842012-11-02 21:32:17 +00001805 // Emit regular call when code size is the priority
1806 !HasMinSizeAttr)
Evan Cheng65f9d192012-02-28 18:51:51 +00001807 // "mov lr, pc; b _foo" to avoid confusing the RSP
1808 CallOpc = ARMISD::CALL_NOLINK;
1809 else
1810 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001811 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001812
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001813 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001814 Ops.push_back(Chain);
1815 Ops.push_back(Callee);
1816
1817 // Add argument registers to the end of the list so that they are known live
1818 // into the call.
1819 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1820 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1821 RegsToPass[i].second.getValueType()));
1822
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001823 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001824 if (!isTailCall) {
1825 const uint32_t *Mask;
Eric Christopherd9134482014-08-04 21:25:23 +00001826 const TargetRegisterInfo *TRI =
1827 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
Matthias Braunc22630e2013-10-04 16:52:54 +00001828 const ARMBaseRegisterInfo *ARI = static_cast<const ARMBaseRegisterInfo*>(TRI);
1829 if (isThisReturn) {
1830 // For 'this' returns, use the R0-preserving mask if applicable
1831 Mask = ARI->getThisReturnPreservedMask(CallConv);
1832 if (!Mask) {
1833 // Set isThisReturn to false if the calling convention is not one that
1834 // allows 'returned' to be modeled in this way, so LowerCallResult does
1835 // not try to pass 'this' straight through
1836 isThisReturn = false;
1837 Mask = ARI->getCallPreservedMask(CallConv);
1838 }
1839 } else
Stephen Linff7fcee2013-06-26 21:42:14 +00001840 Mask = ARI->getCallPreservedMask(CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001841
Matthias Braunc22630e2013-10-04 16:52:54 +00001842 assert(Mask && "Missing call preserved mask for calling convention");
1843 Ops.push_back(DAG.getRegisterMask(Mask));
1844 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001845
Gabor Greiff304a7a2008-08-28 21:40:38 +00001846 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001847 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001848
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001849 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001850 if (isTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001851 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Dale Johannesend679ff72010-06-03 21:09:53 +00001852
Duncan Sands739a0542008-07-02 17:40:58 +00001853 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001854 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001855 InFlag = Chain.getValue(1);
1856
Chris Lattner27539552008-10-11 22:08:30 +00001857 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001858 DAG.getIntPtrConstant(0, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001859 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001860 InFlag = Chain.getValue(1);
1861
Bob Wilsona4c22902009-04-17 19:07:39 +00001862 // Handle result values, copying them out of physregs into vregs that we
1863 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001864 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001865 InVals, isThisReturn,
1866 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001867}
1868
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001869/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001870/// on the stack. Remember the next parameter register to allocate,
1871/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001872/// this.
1873void
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001874ARMTargetLowering::HandleByVal(
1875 CCState *State, unsigned &size, unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001876 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1877 assert((State->getCallOrPrologue() == Prologue ||
1878 State->getCallOrPrologue() == Call) &&
1879 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001880
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001881 if ((ARM::R0 <= reg) && (reg <= ARM::R3)) {
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001882 if (Subtarget->isAAPCS_ABI() && Align > 4) {
1883 unsigned AlignInRegs = Align / 4;
1884 unsigned Waste = (ARM::R4 - reg) % AlignInRegs;
1885 for (unsigned i = 0; i < Waste; ++i)
1886 reg = State->AllocateReg(GPRArgRegs, 4);
1887 }
1888 if (reg != 0) {
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001889 unsigned excess = 4 * (ARM::R4 - reg);
1890
1891 // Special case when NSAA != SP and parameter size greater than size of
1892 // all remained GPR regs. In that case we can't split parameter, we must
1893 // send it to stack. We also must set NCRN to R4, so waste all
1894 // remained registers.
Oliver Stannardd55e1152014-03-05 15:25:27 +00001895 const unsigned NSAAOffset = State->getNextStackOffset();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001896 if (Subtarget->isAAPCS_ABI() && NSAAOffset != 0 && size > excess) {
1897 while (State->AllocateReg(GPRArgRegs, 4))
1898 ;
1899 return;
1900 }
1901
1902 // First register for byval parameter is the first register that wasn't
1903 // allocated before this method call, so it would be "reg".
1904 // If parameter is small enough to be saved in range [reg, r4), then
1905 // the end (first after last) register would be reg + param-size-in-regs,
1906 // else parameter would be splitted between registers and stack,
1907 // end register would be r4 in this case.
1908 unsigned ByValRegBegin = reg;
Stepan Dyatkovskiy2703bca2013-05-08 14:51:27 +00001909 unsigned ByValRegEnd = (size < excess) ? reg + size/4 : (unsigned)ARM::R4;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001910 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
1911 // Note, first register is allocated in the beginning of function already,
1912 // allocate remained amount of registers we need.
1913 for (unsigned i = reg+1; i != ByValRegEnd; ++i)
1914 State->AllocateReg(GPRArgRegs, 4);
Oliver Stannardd55e1152014-03-05 15:25:27 +00001915 // A byval parameter that is split between registers and memory needs its
1916 // size truncated here.
1917 // In the case where the entire structure fits in registers, we set the
1918 // size in memory to zero.
1919 if (size < excess)
1920 size = 0;
1921 else
1922 size -= excess;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001923 }
1924 }
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001925}
1926
Dale Johannesend679ff72010-06-03 21:09:53 +00001927/// MatchingStackOffset - Return true if the given stack call argument is
1928/// already available in the same position (relatively) of the caller's
1929/// incoming argument stack.
1930static
1931bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1932 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00001933 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00001934 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1935 int FI = INT_MAX;
1936 if (Arg.getOpcode() == ISD::CopyFromReg) {
1937 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001938 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00001939 return false;
1940 MachineInstr *Def = MRI->getVRegDef(VR);
1941 if (!Def)
1942 return false;
1943 if (!Flags.isByVal()) {
1944 if (!TII->isLoadFromStackSlot(Def, FI))
1945 return false;
1946 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00001947 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00001948 }
1949 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1950 if (Flags.isByVal())
1951 // ByVal argument is passed in as a pointer but it's now being
1952 // dereferenced. e.g.
1953 // define @foo(%struct.X* %A) {
1954 // tail call @bar(%struct.X* byval %A)
1955 // }
1956 return false;
1957 SDValue Ptr = Ld->getBasePtr();
1958 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1959 if (!FINode)
1960 return false;
1961 FI = FINode->getIndex();
1962 } else
1963 return false;
1964
1965 assert(FI != INT_MAX);
1966 if (!MFI->isFixedObjectIndex(FI))
1967 return false;
1968 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1969}
1970
1971/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1972/// for tail call optimization. Targets which want to do tail call
1973/// optimization should implement this function.
1974bool
1975ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1976 CallingConv::ID CalleeCC,
1977 bool isVarArg,
1978 bool isCalleeStructRet,
1979 bool isCallerStructRet,
1980 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001981 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00001982 const SmallVectorImpl<ISD::InputArg> &Ins,
1983 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00001984 const Function *CallerF = DAG.getMachineFunction().getFunction();
1985 CallingConv::ID CallerCC = CallerF->getCallingConv();
1986 bool CCMatch = CallerCC == CalleeCC;
1987
1988 // Look for obvious safe cases to perform tail call optimization that do not
1989 // require ABI changes. This is what gcc calls sibcall.
1990
Jim Grosbache3864cc2010-06-16 23:45:49 +00001991 // Do not sibcall optimize vararg calls unless the call site is not passing
1992 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00001993 if (isVarArg && !Outs.empty())
1994 return false;
1995
Tim Northoverd8407452013-10-01 14:33:28 +00001996 // Exception-handling functions need a special set of instructions to indicate
1997 // a return to the hardware. Tail-calling another function would probably
1998 // break this.
1999 if (CallerF->hasFnAttribute("interrupt"))
2000 return false;
2001
Dale Johannesend679ff72010-06-03 21:09:53 +00002002 // Also avoid sibcall optimization if either caller or callee uses struct
2003 // return semantics.
2004 if (isCalleeStructRet || isCallerStructRet)
2005 return false;
2006
Dale Johannesend24c66b2010-06-23 18:52:34 +00002007 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach3840c902011-07-08 20:18:11 +00002008 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
2009 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
2010 // support in the assembler and linker to be used. This would need to be
2011 // fixed to fully support tail calls in Thumb1.
2012 //
Dale Johannesene2289282010-07-08 01:18:23 +00002013 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
2014 // LR. This means if we need to reload LR, it takes an extra instructions,
2015 // which outweighs the value of the tail call; but here we don't know yet
2016 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach535d3b42010-09-08 03:54:02 +00002017 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesene2289282010-07-08 01:18:23 +00002018 // emitEpilogue if LR is used.
Dale Johannesene2289282010-07-08 01:18:23 +00002019
2020 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
2021 // but we need to make sure there are enough registers; the only valid
2022 // registers are the 4 used for parameters. We don't currently do this
2023 // case.
Evan Chengd4b08732010-11-30 23:55:39 +00002024 if (Subtarget->isThumb1Only())
2025 return false;
Dale Johannesen3ac52b32010-06-18 18:13:11 +00002026
Oliver Stannard12993dd2014-08-18 12:42:15 +00002027 // Externally-defined functions with weak linkage should not be
2028 // tail-called on ARM when the OS does not support dynamic
2029 // pre-emption of symbols, as the AAELF spec requires normal calls
2030 // to undefined weak functions to be replaced with a NOP or jump to the
2031 // next instruction. The behaviour of branch instructions in this
2032 // situation (as used for tail calls) is implementation-defined, so we
2033 // cannot rely on the linker replacing the tail call with a return.
2034 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2035 const GlobalValue *GV = G->getGlobal();
2036 if (GV->hasExternalWeakLinkage())
2037 return false;
2038 }
2039
Dale Johannesend679ff72010-06-03 21:09:53 +00002040 // If the calling conventions do not match, then we'd better make sure the
2041 // results are returned in the same way as what the caller expects.
2042 if (!CCMatch) {
2043 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopherb5217502014-08-06 18:45:26 +00002044 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1,
2045 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002046 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
2047
2048 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopherb5217502014-08-06 18:45:26 +00002049 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2,
2050 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002051 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
2052
2053 if (RVLocs1.size() != RVLocs2.size())
2054 return false;
2055 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2056 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2057 return false;
2058 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2059 return false;
2060 if (RVLocs1[i].isRegLoc()) {
2061 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2062 return false;
2063 } else {
2064 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2065 return false;
2066 }
2067 }
2068 }
2069
Manman Ren7e48b252012-10-12 23:39:43 +00002070 // If Caller's vararg or byval argument has been split between registers and
2071 // stack, do not perform tail call, since part of the argument is in caller's
2072 // local frame.
2073 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
2074 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002075 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00002076 return false;
2077
Dale Johannesend679ff72010-06-03 21:09:53 +00002078 // If the callee takes no arguments then go on to check the results of the
2079 // call.
2080 if (!Outs.empty()) {
2081 // Check if stack adjustment is needed. For now, do not do this if any
2082 // argument is passed on the stack.
2083 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002084 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
2085 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002086 CCInfo.AnalyzeCallOperands(Outs,
2087 CCAssignFnForNode(CalleeCC, false, isVarArg));
2088 if (CCInfo.getNextStackOffset()) {
2089 MachineFunction &MF = DAG.getMachineFunction();
2090
2091 // Check if the arguments are already laid out in the right way as
2092 // the caller's fixed stack objects.
2093 MachineFrameInfo *MFI = MF.getFrameInfo();
2094 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Eric Christopherd9134482014-08-04 21:25:23 +00002095 const TargetInstrInfo *TII =
2096 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002097 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2098 i != e;
2099 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002100 CCValAssign &VA = ArgLocs[i];
2101 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002102 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002103 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00002104 if (VA.getLocInfo() == CCValAssign::Indirect)
2105 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002106 if (VA.needsCustom()) {
2107 // f64 and vector types are split into multiple registers or
2108 // register/stack-slot combinations. The types will not match
2109 // the registers; give up on memory f64 refs until we figure
2110 // out what to do about this.
2111 if (!VA.isRegLoc())
2112 return false;
2113 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002114 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002115 if (RegVT == MVT::v2f64) {
2116 if (!ArgLocs[++i].isRegLoc())
2117 return false;
2118 if (!ArgLocs[++i].isRegLoc())
2119 return false;
2120 }
2121 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002122 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2123 MFI, MRI, TII))
2124 return false;
2125 }
2126 }
2127 }
2128 }
2129
2130 return true;
2131}
2132
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002133bool
2134ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2135 MachineFunction &MF, bool isVarArg,
2136 const SmallVectorImpl<ISD::OutputArg> &Outs,
2137 LLVMContext &Context) const {
2138 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002139 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002140 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2141 isVarArg));
2142}
2143
Tim Northoverd8407452013-10-01 14:33:28 +00002144static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2145 SDLoc DL, SelectionDAG &DAG) {
2146 const MachineFunction &MF = DAG.getMachineFunction();
2147 const Function *F = MF.getFunction();
2148
2149 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2150
2151 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2152 // version of the "preferred return address". These offsets affect the return
2153 // instruction if this is a return from PL1 without hypervisor extensions.
2154 // IRQ/FIQ: +4 "subs pc, lr, #4"
2155 // SWI: 0 "subs pc, lr, #0"
2156 // ABORT: +4 "subs pc, lr, #4"
2157 // UNDEF: +4/+2 "subs pc, lr, #0"
2158 // UNDEF varies depending on where the exception came from ARM or Thumb
2159 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2160
2161 int64_t LROffset;
2162 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2163 IntKind == "ABORT")
2164 LROffset = 4;
2165 else if (IntKind == "SWI" || IntKind == "UNDEF")
2166 LROffset = 0;
2167 else
2168 report_fatal_error("Unsupported interrupt attribute. If present, value "
2169 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2170
2171 RetOps.insert(RetOps.begin() + 1, DAG.getConstant(LROffset, MVT::i32, false));
2172
Craig Topper48d114b2014-04-26 18:35:24 +00002173 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002174}
2175
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002176SDValue
2177ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002178 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002179 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002180 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002181 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002182
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002183 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002184 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002185
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002186 // CCState - Info about the registers and stack slots.
Eric Christopherb5217502014-08-06 18:45:26 +00002187 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2188 *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002189
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002190 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002191 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2192 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002193
Bob Wilsona4c22902009-04-17 19:07:39 +00002194 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002195 SmallVector<SDValue, 4> RetOps;
2196 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002197 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002198
Jonathan Roelofsef84bda2014-08-05 21:32:21 +00002199 MachineFunction &MF = DAG.getMachineFunction();
2200 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2201 AFI->setReturnRegsCount(RVLocs.size());
2202
Bob Wilsona4c22902009-04-17 19:07:39 +00002203 // Copy the result values into the output registers.
2204 for (unsigned i = 0, realRVLocIdx = 0;
2205 i != RVLocs.size();
2206 ++i, ++realRVLocIdx) {
2207 CCValAssign &VA = RVLocs[i];
2208 assert(VA.isRegLoc() && "Can only return in registers!");
2209
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002210 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002211
2212 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002213 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002214 case CCValAssign::Full: break;
2215 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002216 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002217 break;
2218 }
2219
Bob Wilsona4c22902009-04-17 19:07:39 +00002220 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002221 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002222 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002223 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2224 DAG.getConstant(0, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002225 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002226 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002227
Christian Pirkerb5728192014-05-08 14:06:24 +00002228 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2229 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2230 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002231 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002232 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002233 VA = RVLocs[++i]; // skip ahead to next loc
2234 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002235 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2236 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002237 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002238 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002239 VA = RVLocs[++i]; // skip ahead to next loc
2240
2241 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002242 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2243 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002244 }
2245 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2246 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002247 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002248 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002249 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2250 fmrrd.getValue(isLittleEndian ? 0 : 1),
2251 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002252 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002253 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002254 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002255 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2256 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002257 Flag);
2258 } else
2259 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2260
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002261 // Guarantee that all emitted copies are
2262 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002263 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002264 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002265 }
2266
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002267 // Update chain and glue.
2268 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002269 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002270 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002271
Tim Northoverd8407452013-10-01 14:33:28 +00002272 // CPUs which aren't M-class use a special sequence to return from
2273 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2274 // though we use "subs pc, lr, #N").
2275 //
2276 // M-class CPUs actually use a normal return sequence with a special
2277 // (hardware-provided) value in LR, so the normal code path works.
2278 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2279 !Subtarget->isMClass()) {
2280 if (Subtarget->isThumb1Only())
2281 report_fatal_error("interrupt attribute is not supported in Thumb1");
2282 return LowerInterruptReturn(RetOps, dl, DAG);
2283 }
2284
Craig Topper48d114b2014-04-26 18:35:24 +00002285 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002286}
2287
Evan Chengf8bad082012-04-10 01:51:00 +00002288bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002289 if (N->getNumValues() != 1)
2290 return false;
2291 if (!N->hasNUsesOfValue(1, 0))
2292 return false;
2293
Evan Chengf8bad082012-04-10 01:51:00 +00002294 SDValue TCChain = Chain;
2295 SDNode *Copy = *N->use_begin();
2296 if (Copy->getOpcode() == ISD::CopyToReg) {
2297 // If the copy has a glue operand, we conservatively assume it isn't safe to
2298 // perform a tail call.
2299 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2300 return false;
2301 TCChain = Copy->getOperand(0);
2302 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2303 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002304 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002305 SmallPtrSet<SDNode*, 2> Copies;
2306 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002307 UI != UE; ++UI) {
2308 if (UI->getOpcode() != ISD::CopyToReg)
2309 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002310 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002311 }
Evan Chengf8bad082012-04-10 01:51:00 +00002312 if (Copies.size() > 2)
2313 return false;
2314
2315 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2316 UI != UE; ++UI) {
2317 SDValue UseChain = UI->getOperand(0);
2318 if (Copies.count(UseChain.getNode()))
2319 // Second CopyToReg
2320 Copy = *UI;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002321 else {
2322 // We are at the top of this chain.
2323 // If the copy has a glue operand, we conservatively assume it
2324 // isn't safe to perform a tail call.
2325 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue)
2326 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002327 // First CopyToReg
2328 TCChain = UseChain;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002329 }
Evan Chengf8bad082012-04-10 01:51:00 +00002330 }
2331 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002332 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002333 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002334 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002335 Copy = *Copy->use_begin();
2336 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002337 return false;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002338 // If the copy has a glue operand, we conservatively assume it isn't safe to
2339 // perform a tail call.
2340 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2341 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002342 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002343 } else {
2344 return false;
2345 }
2346
Evan Cheng419ea282010-12-01 22:59:46 +00002347 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002348 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2349 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002350 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2351 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002352 return false;
2353 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002354 }
2355
Evan Chengf8bad082012-04-10 01:51:00 +00002356 if (!HasRet)
2357 return false;
2358
2359 Chain = TCChain;
2360 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002361}
2362
Evan Cheng0663f232011-03-21 01:19:09 +00002363bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002364 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002365 return false;
2366
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00002367 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng0663f232011-03-21 01:19:09 +00002368 return false;
2369
2370 return !Subtarget->isThumb1Only();
2371}
2372
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002373// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2374// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2375// one of the above mentioned nodes. It has to be wrapped because otherwise
2376// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2377// be used to form addressing mode. These wrapped nodes will be selected
2378// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002379static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002380 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002381 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002382 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002383 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002384 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002385 if (CP->isMachineConstantPoolEntry())
2386 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2387 CP->getAlignment());
2388 else
2389 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2390 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002391 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002392}
2393
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002394unsigned ARMTargetLowering::getJumpTableEncoding() const {
2395 return MachineJumpTableInfo::EK_Inline;
2396}
2397
Dan Gohman21cea8a2010-04-17 15:26:15 +00002398SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2399 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002400 MachineFunction &MF = DAG.getMachineFunction();
2401 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2402 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002403 SDLoc DL(Op);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002404 EVT PtrVT = getPointerTy();
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002405 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002406 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2407 SDValue CPAddr;
2408 if (RelocM == Reloc::Static) {
2409 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2410 } else {
2411 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002412 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002413 ARMConstantPoolValue *CPV =
2414 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2415 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002416 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2417 }
2418 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2419 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002420 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002421 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002422 if (RelocM == Reloc::Static)
2423 return Result;
Evan Cheng408aa562009-11-06 22:24:13 +00002424 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002425 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002426}
2427
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002428// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002429SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002430ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002431 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002432 SDLoc dl(GA);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002433 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002434 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002435 MachineFunction &MF = DAG.getMachineFunction();
2436 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002437 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002438 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002439 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2440 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002441 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002442 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002443 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattner7727d052010-09-21 06:44:06 +00002444 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002445 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002446 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002447
Evan Cheng408aa562009-11-06 22:24:13 +00002448 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002449 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002450
2451 // call __tls_get_addr.
2452 ArgListTy Args;
2453 ArgListEntry Entry;
2454 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002455 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002456 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002457
Dale Johannesen555a3752009-01-30 23:10:59 +00002458 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002459 TargetLowering::CallLoweringInfo CLI(DAG);
2460 CLI.setDebugLoc(dl).setChain(Chain)
2461 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002462 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args),
2463 0);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002464
Justin Holewinskiaa583972012-05-25 16:35:28 +00002465 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002466 return CallResult.first;
2467}
2468
2469// Lower ISD::GlobalTLSAddress using the "initial exec" or
2470// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002471SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002472ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002473 SelectionDAG &DAG,
2474 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002475 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002476 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002477 SDValue Offset;
2478 SDValue Chain = DAG.getEntryNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002479 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002480 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002481 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002482
Hans Wennborgaea41202012-05-04 09:40:39 +00002483 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002484 MachineFunction &MF = DAG.getMachineFunction();
2485 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002486 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002487 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002488 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2489 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002490 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2491 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2492 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002493 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002494 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002495 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002496 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002497 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002498 Chain = Offset.getValue(1);
2499
Evan Cheng408aa562009-11-06 22:24:13 +00002500 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002501 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002502
Evan Chengcdbb70c2009-10-31 03:39:36 +00002503 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002504 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002505 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002506 } else {
2507 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002508 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002509 ARMConstantPoolValue *CPV =
2510 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002511 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002512 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002513 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002514 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002515 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002516 }
2517
2518 // The address of the thread local variable is the add of the thread
2519 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002520 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002521}
2522
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002523SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002524ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002525 // TODO: implement the "local dynamic" model
2526 assert(Subtarget->isTargetELF() &&
2527 "TLS not implemented for non-ELF targets");
2528 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgaea41202012-05-04 09:40:39 +00002529
2530 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2531
2532 switch (model) {
2533 case TLSModel::GeneralDynamic:
2534 case TLSModel::LocalDynamic:
2535 return LowerToTLSGeneralDynamicModel(GA, DAG);
2536 case TLSModel::InitialExec:
2537 case TLSModel::LocalExec:
2538 return LowerToTLSExecModels(GA, DAG, model);
2539 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002540 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002541}
2542
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002543SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002544 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002545 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002546 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002547 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002548 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Rafael Espindola6de96a12009-01-15 20:18:42 +00002549 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002550 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002551 ARMConstantPoolConstant::Create(GV,
2552 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002553 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002554 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson7117a912009-03-20 22:42:55 +00002555 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002556 CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002557 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002558 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002559 SDValue Chain = Result.getValue(1);
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002560 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00002561 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002562 if (!UseGOTOFF)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002563 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002564 MachinePointerInfo::getGOT(),
2565 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002566 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002567 }
2568
2569 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002570 // pair. This is always cheaper.
Eric Christopherc1058df2014-07-04 01:55:26 +00002571 if (Subtarget->useMovt(DAG.getMachineFunction())) {
Evan Cheng68aec142011-01-19 02:16:49 +00002572 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002573 // FIXME: Once remat is capable of dealing with instructions with register
2574 // operands, expand this into two nodes.
2575 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2576 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002577 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002578 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2579 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2580 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2581 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002582 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002583 }
2584}
2585
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002586SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002587 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002588 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002589 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002590 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002591 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002592
Eric Christopherc1058df2014-07-04 01:55:26 +00002593 if (Subtarget->useMovt(DAG.getMachineFunction()))
Evan Cheng68aec142011-01-19 02:16:49 +00002594 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002595
Tim Northover72360d22013-12-02 10:35:41 +00002596 // FIXME: Once remat is capable of dealing with instructions with register
2597 // operands, expand this into multiple nodes
2598 unsigned Wrapper =
2599 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002600
Tim Northover72360d22013-12-02 10:35:41 +00002601 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2602 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002603
Evan Cheng1b389522009-09-03 07:04:02 +00002604 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002605 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2606 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002607 return Result;
2608}
2609
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002610SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2611 SelectionDAG &DAG) const {
2612 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
Eric Christopherc1058df2014-07-04 01:55:26 +00002613 assert(Subtarget->useMovt(DAG.getMachineFunction()) &&
2614 "Windows on ARM expects to use movw/movt");
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002615
2616 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002617 const ARMII::TOF TargetFlags =
2618 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG);
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002619 EVT PtrVT = getPointerTy();
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002620 SDValue Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002621 SDLoc DL(Op);
2622
2623 ++NumMovwMovt;
2624
2625 // FIXME: Once remat is capable of dealing with instructions with register
2626 // operands, expand this into two nodes.
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002627 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2628 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0,
2629 TargetFlags));
2630 if (GV->hasDLLImportStorageClass())
2631 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
2632 MachinePointerInfo::getGOT(), false, false, false, 0);
2633 return Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002634}
2635
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002636SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002637 SelectionDAG &DAG) const {
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002638 assert(Subtarget->isTargetELF() &&
2639 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Cheng408aa562009-11-06 22:24:13 +00002640 MachineFunction &MF = DAG.getMachineFunction();
2641 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002642 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002643 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002644 SDLoc dl(Op);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002645 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingc214cb02011-10-01 08:58:29 +00002646 ARMConstantPoolValue *CPV =
2647 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2648 ARMPCLabelIndex, PCAdj);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002649 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002650 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002651 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002652 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002653 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00002654 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002655 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002656}
2657
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002658SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002659ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002660 SDLoc dl(Op);
Jim Grosbachfaa3abb2010-05-27 23:49:24 +00002661 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002662 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2663 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002664 Op.getOperand(1), Val);
2665}
2666
2667SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002668ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002669 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002670 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2671 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2672}
2673
2674SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002675ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002676 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002677 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002678 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002679 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002680 default: return SDValue(); // Don't custom lower most intrinsics.
Jim Grosbach07393ba2014-06-16 21:55:30 +00002681 case Intrinsic::arm_rbit: {
Yi Kongc655f0c2014-08-20 10:40:20 +00002682 assert(Op.getOperand(1).getValueType() == MVT::i32 &&
Jim Grosbach07393ba2014-06-16 21:55:30 +00002683 "RBIT intrinsic must have i32 type!");
Yi Kongc655f0c2014-08-20 10:40:20 +00002684 return DAG.getNode(ARMISD::RBIT, dl, MVT::i32, Op.getOperand(1));
Jim Grosbach07393ba2014-06-16 21:55:30 +00002685 }
Bob Wilson17f88782009-08-04 00:25:01 +00002686 case Intrinsic::arm_thread_pointer: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002687 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson17f88782009-08-04 00:25:01 +00002688 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2689 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002690 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002691 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002692 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002693 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002694 EVT PtrVT = getPointerTy();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002695 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2696 SDValue CPAddr;
2697 unsigned PCAdj = (RelocM != Reloc::PIC_)
2698 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002699 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002700 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2701 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002702 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002703 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002704 SDValue Result =
Evan Chengcdbb70c2009-10-31 03:39:36 +00002705 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002706 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002707 false, false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002708
2709 if (RelocM == Reloc::PIC_) {
Evan Cheng408aa562009-11-06 22:24:13 +00002710 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002711 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2712 }
2713 return Result;
2714 }
Evan Cheng18381b42011-03-29 23:06:19 +00002715 case Intrinsic::arm_neon_vmulls:
2716 case Intrinsic::arm_neon_vmullu: {
2717 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2718 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002719 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002720 Op.getOperand(1), Op.getOperand(2));
2721 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002722 }
2723}
2724
Eli Friedman30a49e92011-08-03 21:06:02 +00002725static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2726 const ARMSubtarget *Subtarget) {
2727 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002728 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00002729 if (!Subtarget->hasDataBarrier()) {
2730 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2731 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2732 // here.
2733 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00002734 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00002735 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00002736 DAG.getConstant(0, MVT::i32));
2737 }
2738
Tim Northover36b24172013-07-03 09:20:36 +00002739 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
2740 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
Robin Morisseta47cb412014-09-03 21:01:03 +00002741 ARM_MB::MemBOpt Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00002742 if (Subtarget->isMClass()) {
2743 // Only a full system barrier exists in the M-class architectures.
2744 Domain = ARM_MB::SY;
2745 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00002746 // Swift happens to implement ISHST barriers in a way that's compatible with
2747 // Release semantics but weaker than ISH so we'd be fools not to use
2748 // it. Beware: other processors probably don't!
2749 Domain = ARM_MB::ISHST;
2750 }
2751
Joey Gouly926d3f52013-09-05 15:35:24 +00002752 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
2753 DAG.getConstant(Intrinsic::arm_dmb, MVT::i32),
Tim Northover36b24172013-07-03 09:20:36 +00002754 DAG.getConstant(Domain, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002755}
2756
Evan Cheng8740ee32010-11-03 06:34:55 +00002757static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2758 const ARMSubtarget *Subtarget) {
2759 // ARM pre v5TE and Thumb1 does not have preload instructions.
2760 if (!(Subtarget->isThumb2() ||
2761 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2762 // Just preserve the chain.
2763 return Op.getOperand(0);
2764
Andrew Trickef9de2a2013-05-25 02:42:55 +00002765 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00002766 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2767 if (!isRead &&
2768 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2769 // ARMv7 with MP extension has PLDW.
2770 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00002771
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002772 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2773 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00002774 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00002775 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002776 isData = ~isData & 1;
2777 }
Evan Cheng8740ee32010-11-03 06:34:55 +00002778
2779 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng21acf9f2010-11-04 05:19:35 +00002780 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2781 DAG.getConstant(isData, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00002782}
2783
Dan Gohman31ae5862010-04-17 14:41:14 +00002784static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2785 MachineFunction &MF = DAG.getMachineFunction();
2786 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2787
Evan Cheng10043e22007-01-19 07:51:42 +00002788 // vastart just stores the address of the VarArgsFrameIndex slot into the
2789 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002790 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002791 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00002792 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002793 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00002794 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2795 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002796}
2797
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002798SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00002799ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2800 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002801 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00002802 MachineFunction &MF = DAG.getMachineFunction();
2803 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2804
Craig Topper760b1342012-02-22 05:59:10 +00002805 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00002806 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002807 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002808 else
Craig Topperc7242e02012-04-20 07:30:17 +00002809 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002810
2811 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002812 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002813 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002814
2815 SDValue ArgValue2;
2816 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002817 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00002818 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00002819
2820 // Create load node to retrieve arguments from the stack.
2821 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Chengcdbb70c2009-10-31 03:39:36 +00002822 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002823 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002824 false, false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00002825 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00002826 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002827 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002828 }
Christian Pirkerb5728192014-05-08 14:06:24 +00002829 if (!Subtarget->isLittle())
2830 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002831 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00002832}
2833
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002834void
2835ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002836 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002837 unsigned ArgSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002838 unsigned &ArgRegsSize,
2839 unsigned &ArgRegsSaveSize)
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002840 const {
2841 unsigned NumGPRs;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002842 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2843 unsigned RBegin, REnd;
2844 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2845 NumGPRs = REnd - RBegin;
2846 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002847 unsigned int firstUnalloced;
2848 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2849 sizeof(GPRArgRegs) /
2850 sizeof(GPRArgRegs[0]));
2851 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2852 }
2853
Eric Christopherd9134482014-08-04 21:25:23 +00002854 unsigned Align = MF.getTarget()
2855 .getSubtargetImpl()
2856 ->getFrameLowering()
2857 ->getStackAlignment();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002858 ArgRegsSize = NumGPRs * 4;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002859
2860 // If parameter is split between stack and GPRs...
Mark Seabornbe266aa2014-02-16 18:59:48 +00002861 if (NumGPRs && Align > 4 &&
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002862 (ArgRegsSize < ArgSize ||
2863 InRegsParamRecordIdx >= CCInfo.getInRegsParamsCount())) {
Mark Seabornbe266aa2014-02-16 18:59:48 +00002864 // Add padding for part of param recovered from GPRs. For example,
2865 // if Align == 8, its last byte must be at address K*8 - 1.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002866 // We need to do it, since remained (stack) part of parameter has
2867 // stack alignment, and we need to "attach" "GPRs head" without gaps
2868 // to it:
2869 // Stack:
2870 // |---- 8 bytes block ----| |---- 8 bytes block ----| |---- 8 bytes...
2871 // [ [padding] [GPRs head] ] [ Tail passed via stack ....
2872 //
2873 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2874 unsigned Padding =
Mark Seabornbe266aa2014-02-16 18:59:48 +00002875 OffsetToAlignment(ArgRegsSize + AFI->getArgRegsSaveSize(), Align);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002876 ArgRegsSaveSize = ArgRegsSize + Padding;
2877 } else
2878 // We don't need to extend regs save size for byval parameters if they
2879 // are passed via GPRs only.
2880 ArgRegsSaveSize = ArgRegsSize;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002881}
2882
2883// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00002884// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002885// byval). Either way, we allocate stack slots adjacent to the data
2886// provided by our caller, and store the unallocated registers there.
2887// If this is a variadic function, the va_list pointer will begin with
2888// these values; otherwise, this reassembles a (byval) structure that
2889// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002890// Return: The frame index registers were stored into.
2891int
2892ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002893 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002894 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002895 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002896 unsigned OffsetFromOrigArg,
2897 unsigned ArgOffset,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002898 unsigned ArgSize,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002899 bool ForceMutable,
2900 unsigned ByValStoreOffset,
2901 unsigned TotalArgRegsSaveSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002902
2903 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00002904 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002905 // Setup first unallocated register as first byval register;
2906 // eat all remained registers
2907 // (these two actions are performed by HandleByVal method).
2908 // Then, here, we initialize stack frame with
2909 // "store-reg" instructions.
2910 // Case #2. Var-args function, that doesn't contain byval parameters.
2911 // The same: eat all remained unallocated registers,
2912 // initialize stack frame.
2913
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002914 MachineFunction &MF = DAG.getMachineFunction();
2915 MachineFrameInfo *MFI = MF.getFrameInfo();
2916 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002917 unsigned firstRegToSaveIndex, lastRegToSaveIndex;
2918 unsigned RBegin, REnd;
2919 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2920 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2921 firstRegToSaveIndex = RBegin - ARM::R0;
2922 lastRegToSaveIndex = REnd - ARM::R0;
2923 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002924 firstRegToSaveIndex = CCInfo.getFirstUnallocated
Craig Topper58713212013-07-15 04:27:47 +00002925 (GPRArgRegs, array_lengthof(GPRArgRegs));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002926 lastRegToSaveIndex = 4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002927 }
2928
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002929 unsigned ArgRegsSize, ArgRegsSaveSize;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002930 computeRegArea(CCInfo, MF, InRegsParamRecordIdx, ArgSize,
2931 ArgRegsSize, ArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002932
2933 // Store any by-val regs to their spots on the stack so that they may be
2934 // loaded by deferencing the result of formal parameter pointer or va_next.
2935 // Note: once stack area for byval/varargs registers
2936 // was initialized, it can't be initialized again.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002937 if (ArgRegsSaveSize) {
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002938 unsigned Padding = ArgRegsSaveSize - ArgRegsSize;
2939
2940 if (Padding) {
2941 assert(AFI->getStoredByValParamsPadding() == 0 &&
2942 "The only parameter may be padded.");
2943 AFI->setStoredByValParamsPadding(Padding);
2944 }
2945
Oliver Stannardd55e1152014-03-05 15:25:27 +00002946 int FrameIndex = MFI->CreateFixedObject(ArgRegsSaveSize,
2947 Padding +
2948 ByValStoreOffset -
2949 (int64_t)TotalArgRegsSaveSize,
2950 false);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002951 SDValue FIN = DAG.getFrameIndex(FrameIndex, getPointerTy());
Oliver Stannardd55e1152014-03-05 15:25:27 +00002952 if (Padding) {
2953 MFI->CreateFixedObject(Padding,
2954 ArgOffset + ByValStoreOffset -
2955 (int64_t)ArgRegsSaveSize,
2956 false);
2957 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002958
2959 SmallVector<SDValue, 4> MemOps;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002960 for (unsigned i = 0; firstRegToSaveIndex < lastRegToSaveIndex;
2961 ++firstRegToSaveIndex, ++i) {
Craig Topper760b1342012-02-22 05:59:10 +00002962 const TargetRegisterClass *RC;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002963 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002964 RC = &ARM::tGPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002965 else
Craig Topperc7242e02012-04-20 07:30:17 +00002966 RC = &ARM::GPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002967
2968 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2969 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2970 SDValue Store =
2971 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002972 MachinePointerInfo(OrigArg, OffsetFromOrigArg + 4*i),
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002973 false, false, 0);
2974 MemOps.push_back(Store);
2975 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2976 DAG.getConstant(4, getPointerTy()));
2977 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002978
2979 AFI->setArgRegsSaveSize(ArgRegsSaveSize + AFI->getArgRegsSaveSize());
2980
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002981 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002982 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002983 return FrameIndex;
Oliver Stannardd55e1152014-03-05 15:25:27 +00002984 } else {
2985 if (ArgSize == 0) {
2986 // We cannot allocate a zero-byte object for the first variadic argument,
2987 // so just make up a size.
2988 ArgSize = 4;
2989 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002990 // This will point to the next argument passed via stack.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002991 return MFI->CreateFixedObject(
Oliver Stannardd55e1152014-03-05 15:25:27 +00002992 ArgSize, ArgOffset, !ForceMutable);
2993 }
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002994}
2995
2996// Setup stack frame, the va_list pointer will start from.
2997void
2998ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002999 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003000 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003001 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003002 bool ForceMutable) const {
3003 MachineFunction &MF = DAG.getMachineFunction();
3004 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3005
3006 // Try to store any remaining integer argument regs
3007 // to their spots on the stack so that they may be loaded by deferencing
3008 // the result of va_next.
3009 // If there is no regs to be stored, just point address after last
3010 // argument passed via stack.
3011 int FrameIndex =
Craig Topper062a2ba2014-04-25 05:30:21 +00003012 StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
3013 CCInfo.getInRegsParamsCount(), 0, ArgOffset, 0, ForceMutable,
3014 0, TotalArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003015
3016 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003017}
3018
Bob Wilson2e076c42009-06-22 23:27:02 +00003019SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003020ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003021 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003022 const SmallVectorImpl<ISD::InputArg>
3023 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003024 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003025 SmallVectorImpl<SDValue> &InVals)
3026 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00003027 MachineFunction &MF = DAG.getMachineFunction();
3028 MachineFrameInfo *MFI = MF.getFrameInfo();
3029
Bob Wilsona4c22902009-04-17 19:07:39 +00003030 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3031
3032 // Assign locations to all of the incoming arguments.
3033 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00003034 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
3035 *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003036 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003037 CCAssignFnForNode(CallConv, /* Return*/ false,
3038 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00003039
Bob Wilsona4c22902009-04-17 19:07:39 +00003040 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003041 int lastInsIndex = -1;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003042 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00003043 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
3044 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003045
3046 // Initially ArgRegsSaveSize is zero.
3047 // Then we increase this value each time we meet byval parameter.
3048 // We also increase this value in case of varargs function.
3049 AFI->setArgRegsSaveSize(0);
3050
Oliver Stannardd55e1152014-03-05 15:25:27 +00003051 unsigned ByValStoreOffset = 0;
3052 unsigned TotalArgRegsSaveSize = 0;
3053 unsigned ArgRegsSaveSizeMaxAlign = 4;
3054
3055 // Calculate the amount of stack space that we need to allocate to store
3056 // byval and variadic arguments that are passed in registers.
3057 // We need to know this before we allocate the first byval or variadic
3058 // argument, as they will be allocated a stack slot below the CFA (Canonical
3059 // Frame Address, the stack pointer at entry to the function).
3060 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3061 CCValAssign &VA = ArgLocs[i];
3062 if (VA.isMemLoc()) {
3063 int index = VA.getValNo();
3064 if (index != lastInsIndex) {
3065 ISD::ArgFlagsTy Flags = Ins[index].Flags;
3066 if (Flags.isByVal()) {
3067 unsigned ExtraArgRegsSize;
3068 unsigned ExtraArgRegsSaveSize;
3069 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsProceed(),
3070 Flags.getByValSize(),
3071 ExtraArgRegsSize, ExtraArgRegsSaveSize);
3072
3073 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
3074 if (Flags.getByValAlign() > ArgRegsSaveSizeMaxAlign)
3075 ArgRegsSaveSizeMaxAlign = Flags.getByValAlign();
3076 CCInfo.nextInRegsParam();
3077 }
3078 lastInsIndex = index;
3079 }
3080 }
3081 }
3082 CCInfo.rewindByValRegsInfo();
3083 lastInsIndex = -1;
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003084 if (isVarArg && MFI->hasVAStart()) {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003085 unsigned ExtraArgRegsSize;
3086 unsigned ExtraArgRegsSaveSize;
3087 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsCount(), 0,
3088 ExtraArgRegsSize, ExtraArgRegsSaveSize);
3089 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
3090 }
3091 // If the arg regs save area contains N-byte aligned values, the
3092 // bottom of it must be at least N-byte aligned.
3093 TotalArgRegsSaveSize = RoundUpToAlignment(TotalArgRegsSaveSize, ArgRegsSaveSizeMaxAlign);
3094 TotalArgRegsSaveSize = std::min(TotalArgRegsSaveSize, 16U);
3095
Bob Wilsona4c22902009-04-17 19:07:39 +00003096 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3097 CCValAssign &VA = ArgLocs[i];
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00003098 std::advance(CurOrigArg, Ins[VA.getValNo()].OrigArgIndex - CurArgIdx);
3099 CurArgIdx = Ins[VA.getValNo()].OrigArgIndex;
Bob Wilsonea09d4a2009-04-17 20:35:10 +00003100 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00003101 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003102 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00003103
Bob Wilsona4c22902009-04-17 19:07:39 +00003104 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003105 // f64 and vector types are split up into multiple registers or
3106 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00003107 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003108 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003109 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00003110 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00003111 SDValue ArgValue2;
3112 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00003113 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson699bdf72010-04-13 22:03:22 +00003114 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3115 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003116 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003117 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00003118 } else {
3119 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3120 Chain, DAG, dl);
3121 }
Owen Anderson9f944592009-08-11 20:47:22 +00003122 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3123 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00003124 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson9f944592009-08-11 20:47:22 +00003125 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00003126 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
3127 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003128 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00003129
Bob Wilson2e076c42009-06-22 23:27:02 +00003130 } else {
Craig Topper760b1342012-02-22 05:59:10 +00003131 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003132
Owen Anderson9f944592009-08-11 20:47:22 +00003133 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003134 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003135 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003136 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003137 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003138 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003139 else if (RegVT == MVT::i32)
Craig Topperc7242e02012-04-20 07:30:17 +00003140 RC = AFI->isThumb1OnlyFunction() ?
3141 (const TargetRegisterClass*)&ARM::tGPRRegClass :
3142 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003143 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003144 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003145
3146 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003147 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003148 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003149 }
3150
3151 // If this is an 8 or 16-bit value, it is really passed promoted
3152 // to 32 bits. Insert an assert[sz]ext to capture this, then
3153 // truncate to the right size.
3154 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003155 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003156 case CCValAssign::Full: break;
3157 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003158 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003159 break;
3160 case CCValAssign::SExt:
3161 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3162 DAG.getValueType(VA.getValVT()));
3163 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3164 break;
3165 case CCValAssign::ZExt:
3166 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3167 DAG.getValueType(VA.getValVT()));
3168 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3169 break;
3170 }
3171
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003172 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003173
3174 } else { // VA.isRegLoc()
3175
3176 // sanity check
3177 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003178 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003179
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003180 int index = ArgLocs[i].getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003181
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003182 // Some Ins[] entries become multiple ArgLoc[] entries.
3183 // Process them only once.
3184 if (index != lastInsIndex)
3185 {
3186 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003187 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003188 // This can be changed with more analysis.
3189 // In case of tail call optimization mark all arguments mutable.
3190 // Since they could be overwritten by lowering of arguments in case of
3191 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003192 if (Flags.isByVal()) {
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003193 unsigned CurByValIndex = CCInfo.getInRegsParamsProceed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003194
3195 ByValStoreOffset = RoundUpToAlignment(ByValStoreOffset, Flags.getByValAlign());
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003196 int FrameIndex = StoreByValRegs(
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003197 CCInfo, DAG, dl, Chain, CurOrigArg,
3198 CurByValIndex,
3199 Ins[VA.getValNo()].PartOffset,
3200 VA.getLocMemOffset(),
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003201 Flags.getByValSize(),
Oliver Stannardd55e1152014-03-05 15:25:27 +00003202 true /*force mutable frames*/,
3203 ByValStoreOffset,
3204 TotalArgRegsSaveSize);
3205 ByValStoreOffset += Flags.getByValSize();
3206 ByValStoreOffset = std::min(ByValStoreOffset, 16U);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003207 InVals.push_back(DAG.getFrameIndex(FrameIndex, getPointerTy()));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003208 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003209 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003210 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003211 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003212 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003213
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003214 // Create load nodes to retrieve arguments from the stack.
3215 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3216 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
3217 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003218 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003219 }
3220 lastInsIndex = index;
3221 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003222 }
3223 }
3224
3225 // varargs
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003226 if (isVarArg && MFI->hasVAStart())
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003227 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003228 CCInfo.getNextStackOffset(),
3229 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003230
Oliver Stannardb14c6252014-04-02 16:10:33 +00003231 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3232
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003233 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003234}
3235
3236/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003237static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003238 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003239 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003240 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003241 // Maybe this has already been legalized into the constant pool?
3242 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003243 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003244 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003245 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003246 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003247 }
Renato Golin6fb9c2e2014-10-23 15:31:50 +00003248 } else if (Op->getOpcode() == ISD::BITCAST &&
3249 Op->getValueType(0) == MVT::f64) {
3250 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64)
3251 // created by LowerConstantFP().
3252 SDValue BitcastOp = Op->getOperand(0);
3253 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM) {
3254 SDValue MoveOp = BitcastOp->getOperand(0);
3255 if (MoveOp->getOpcode() == ISD::TargetConstant &&
3256 cast<ConstantSDNode>(MoveOp)->getZExtValue() == 0) {
3257 return true;
3258 }
3259 }
Evan Cheng10043e22007-01-19 07:51:42 +00003260 }
3261 return false;
3262}
3263
Evan Cheng10043e22007-01-19 07:51:42 +00003264/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3265/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003266SDValue
3267ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003268 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003269 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003270 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003271 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003272 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003273 // Constant does not fit, try adjusting it by one?
3274 switch (CC) {
3275 default: break;
3276 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003277 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003278 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003279 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003280 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003281 }
3282 break;
3283 case ISD::SETULT:
3284 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003285 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003286 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003287 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003288 }
3289 break;
3290 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003291 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003292 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003293 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003294 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003295 }
3296 break;
3297 case ISD::SETULE:
3298 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003299 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003300 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003301 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003302 }
3303 break;
3304 }
3305 }
3306 }
3307
3308 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003309 ARMISD::NodeType CompareType;
3310 switch (CondCode) {
3311 default:
3312 CompareType = ARMISD::CMP;
3313 break;
3314 case ARMCC::EQ:
3315 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003316 // Uses only Z Flag
3317 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003318 break;
3319 }
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003320 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003321 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003322}
3323
3324/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003325SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003326ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003327 SDLoc dl) const {
Oliver Stannard51b1d462014-08-21 12:50:31 +00003328 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003329 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003330 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003331 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003332 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003333 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3334 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003335}
3336
Bob Wilson45acbd02011-03-08 01:17:20 +00003337/// duplicateCmp - Glue values can have only one use, so this function
3338/// duplicates a comparison node.
3339SDValue
3340ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3341 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003342 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003343 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3344 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3345
3346 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3347 Cmp = Cmp.getOperand(0);
3348 Opc = Cmp.getOpcode();
3349 if (Opc == ARMISD::CMPFP)
3350 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3351 else {
3352 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3353 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3354 }
3355 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3356}
3357
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003358std::pair<SDValue, SDValue>
3359ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3360 SDValue &ARMcc) const {
3361 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3362
3363 SDValue Value, OverflowCmp;
3364 SDValue LHS = Op.getOperand(0);
3365 SDValue RHS = Op.getOperand(1);
3366
3367
3368 // FIXME: We are currently always generating CMPs because we don't support
3369 // generating CMN through the backend. This is not as good as the natural
3370 // CMP case because it causes a register dependency and cannot be folded
3371 // later.
3372
3373 switch (Op.getOpcode()) {
3374 default:
3375 llvm_unreachable("Unknown overflow instruction!");
3376 case ISD::SADDO:
3377 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32);
3378 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS);
3379 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS);
3380 break;
3381 case ISD::UADDO:
3382 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32);
3383 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS);
3384 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS);
3385 break;
3386 case ISD::SSUBO:
3387 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32);
3388 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS);
3389 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS);
3390 break;
3391 case ISD::USUBO:
3392 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32);
3393 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS);
3394 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS);
3395 break;
3396 } // switch (...)
3397
3398 return std::make_pair(Value, OverflowCmp);
3399}
3400
3401
3402SDValue
3403ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3404 // Let legalize expand this if it isn't a legal type yet.
3405 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3406 return SDValue();
3407
3408 SDValue Value, OverflowCmp;
3409 SDValue ARMcc;
3410 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3411 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3412 // We use 0 and 1 as false and true values.
3413 SDValue TVal = DAG.getConstant(1, MVT::i32);
3414 SDValue FVal = DAG.getConstant(0, MVT::i32);
3415 EVT VT = Op.getValueType();
3416
3417 SDValue Overflow = DAG.getNode(ARMISD::CMOV, SDLoc(Op), VT, TVal, FVal,
3418 ARMcc, CCR, OverflowCmp);
3419
3420 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
3421 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), VTs, Value, Overflow);
3422}
3423
3424
Bill Wendling6a981312010-08-11 08:43:16 +00003425SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3426 SDValue Cond = Op.getOperand(0);
3427 SDValue SelectTrue = Op.getOperand(1);
3428 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003429 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003430 unsigned Opc = Cond.getOpcode();
3431
3432 if (Cond.getResNo() == 1 &&
3433 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3434 Opc == ISD::USUBO)) {
3435 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3436 return SDValue();
3437
3438 SDValue Value, OverflowCmp;
3439 SDValue ARMcc;
3440 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3441 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3442 EVT VT = Op.getValueType();
3443
Oliver Stannard51b1d462014-08-21 12:50:31 +00003444 return getCMOV(SDLoc(Op), VT, SelectTrue, SelectFalse, ARMcc, CCR,
3445 OverflowCmp, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003446 }
Bill Wendling6a981312010-08-11 08:43:16 +00003447
3448 // Convert:
3449 //
3450 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3451 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3452 //
3453 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3454 const ConstantSDNode *CMOVTrue =
3455 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3456 const ConstantSDNode *CMOVFalse =
3457 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3458
3459 if (CMOVTrue && CMOVFalse) {
3460 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3461 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3462
3463 SDValue True;
3464 SDValue False;
3465 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3466 True = SelectTrue;
3467 False = SelectFalse;
3468 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3469 True = SelectFalse;
3470 False = SelectTrue;
3471 }
3472
3473 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003474 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003475 SDValue ARMcc = Cond.getOperand(2);
3476 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003477 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003478 assert(True.getValueType() == VT);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003479 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00003480 }
3481 }
3482 }
3483
Dan Gohmand4a77c42012-02-24 00:09:36 +00003484 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3485 // undefined bits before doing a full-word comparison with zero.
3486 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
3487 DAG.getConstant(1, Cond.getValueType()));
3488
Bill Wendling6a981312010-08-11 08:43:16 +00003489 return DAG.getSelectCC(dl, Cond,
3490 DAG.getConstant(0, Cond.getValueType()),
3491 SelectTrue, SelectFalse, ISD::SETNE);
3492}
3493
Joey Gouly881eab52013-08-22 15:29:11 +00003494static ISD::CondCode getInverseCCForVSEL(ISD::CondCode CC) {
3495 if (CC == ISD::SETNE)
3496 return ISD::SETEQ;
Weiming Zhao63871d22013-12-18 22:25:17 +00003497 return ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003498}
3499
3500static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3501 bool &swpCmpOps, bool &swpVselOps) {
3502 // Start by selecting the GE condition code for opcodes that return true for
3503 // 'equality'
3504 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3505 CC == ISD::SETULE)
3506 CondCode = ARMCC::GE;
3507
3508 // and GT for opcodes that return false for 'equality'.
3509 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3510 CC == ISD::SETULT)
3511 CondCode = ARMCC::GT;
3512
3513 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3514 // to swap the compare operands.
3515 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3516 CC == ISD::SETULT)
3517 swpCmpOps = true;
3518
3519 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3520 // If we have an unordered opcode, we need to swap the operands to the VSEL
3521 // instruction (effectively negating the condition).
3522 //
3523 // This also has the effect of swapping which one of 'less' or 'greater'
3524 // returns true, so we also swap the compare operands. It also switches
3525 // whether we return true for 'equality', so we compensate by picking the
3526 // opposite condition code to our original choice.
3527 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3528 CC == ISD::SETUGT) {
3529 swpCmpOps = !swpCmpOps;
3530 swpVselOps = !swpVselOps;
3531 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3532 }
3533
3534 // 'ordered' is 'anything but unordered', so use the VS condition code and
3535 // swap the VSEL operands.
3536 if (CC == ISD::SETO) {
3537 CondCode = ARMCC::VS;
3538 swpVselOps = true;
3539 }
3540
3541 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3542 // code and swap the VSEL operands.
3543 if (CC == ISD::SETUNE) {
3544 CondCode = ARMCC::EQ;
3545 swpVselOps = true;
3546 }
3547}
3548
Oliver Stannard51b1d462014-08-21 12:50:31 +00003549SDValue ARMTargetLowering::getCMOV(SDLoc dl, EVT VT, SDValue FalseVal,
3550 SDValue TrueVal, SDValue ARMcc, SDValue CCR,
3551 SDValue Cmp, SelectionDAG &DAG) const {
3552 if (Subtarget->isFPOnlySP() && VT == MVT::f64) {
3553 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3554 DAG.getVTList(MVT::i32, MVT::i32), FalseVal);
3555 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3556 DAG.getVTList(MVT::i32, MVT::i32), TrueVal);
3557
3558 SDValue TrueLow = TrueVal.getValue(0);
3559 SDValue TrueHigh = TrueVal.getValue(1);
3560 SDValue FalseLow = FalseVal.getValue(0);
3561 SDValue FalseHigh = FalseVal.getValue(1);
3562
3563 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow,
3564 ARMcc, CCR, Cmp);
3565 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh,
3566 ARMcc, CCR, duplicateCmp(Cmp, DAG));
3567
3568 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High);
3569 } else {
3570 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3571 Cmp);
3572 }
3573}
3574
Dan Gohman21cea8a2010-04-17 15:26:15 +00003575SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003576 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003577 SDValue LHS = Op.getOperand(0);
3578 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003579 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003580 SDValue TrueVal = Op.getOperand(2);
3581 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003582 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003583
Oliver Stannard51b1d462014-08-21 12:50:31 +00003584 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3585 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3586 dl);
3587
3588 // If softenSetCCOperands only returned one value, we should compare it to
3589 // zero.
3590 if (!RHS.getNode()) {
3591 RHS = DAG.getConstant(0, LHS.getValueType());
3592 CC = ISD::SETNE;
3593 }
3594 }
3595
Owen Anderson9f944592009-08-11 20:47:22 +00003596 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003597 // Try to generate VSEL on ARMv8.
3598 // The VSEL instruction can't use all the usual ARM condition
3599 // codes: it only has two bits to select the condition code, so it's
3600 // constrained to use only GE, GT, VS and EQ.
3601 //
3602 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3603 // swap the operands of the previous compare instruction (effectively
3604 // inverting the compare condition, swapping 'less' and 'greater') and
3605 // sometimes need to swap the operands to the VSEL (which inverts the
3606 // condition in the sense of firing whenever the previous condition didn't)
Joey Goulyccd04892013-09-13 13:46:57 +00003607 if (getSubtarget()->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
Joey Gouly881eab52013-08-22 15:29:11 +00003608 TrueVal.getValueType() == MVT::f64)) {
3609 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3610 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3611 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
3612 CC = getInverseCCForVSEL(CC);
3613 std::swap(TrueVal, FalseVal);
3614 }
3615 }
3616
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003617 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003618 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003619 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003620 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003621 }
3622
3623 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003624 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003625
Joey Gouly881eab52013-08-22 15:29:11 +00003626 // Try to generate VSEL on ARMv8.
Joey Goulyccd04892013-09-13 13:46:57 +00003627 if (getSubtarget()->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
Joey Gouly881eab52013-08-22 15:29:11 +00003628 TrueVal.getValueType() == MVT::f64)) {
Joey Goulye3dd6842013-08-23 12:01:13 +00003629 // We can select VMAXNM/VMINNM from a compare followed by a select with the
3630 // same operands, as follows:
3631 // c = fcmp [ogt, olt, ugt, ult] a, b
3632 // select c, a, b
3633 // We only do this in unsafe-fp-math, because signed zeros and NaNs are
3634 // handled differently than the original code sequence.
Oliver Stannard79efe412014-10-27 09:23:02 +00003635 if (getTargetMachine().Options.UnsafeFPMath) {
3636 if (LHS == TrueVal && RHS == FalseVal) {
3637 if (CC == ISD::SETOGT || CC == ISD::SETUGT)
3638 return DAG.getNode(ARMISD::VMAXNM, dl, VT, TrueVal, FalseVal);
3639 if (CC == ISD::SETOLT || CC == ISD::SETULT)
3640 return DAG.getNode(ARMISD::VMINNM, dl, VT, TrueVal, FalseVal);
3641 } else if (LHS == FalseVal && RHS == TrueVal) {
3642 if (CC == ISD::SETOLT || CC == ISD::SETULT)
3643 return DAG.getNode(ARMISD::VMAXNM, dl, VT, TrueVal, FalseVal);
3644 if (CC == ISD::SETOGT || CC == ISD::SETUGT)
3645 return DAG.getNode(ARMISD::VMINNM, dl, VT, TrueVal, FalseVal);
3646 }
Joey Goulye3dd6842013-08-23 12:01:13 +00003647 }
3648
Joey Gouly881eab52013-08-22 15:29:11 +00003649 bool swpCmpOps = false;
3650 bool swpVselOps = false;
3651 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3652
3653 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3654 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3655 if (swpCmpOps)
3656 std::swap(LHS, RHS);
3657 if (swpVselOps)
3658 std::swap(TrueVal, FalseVal);
3659 }
3660 }
3661
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003662 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3663 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003664 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003665 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003666 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003667 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003668 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003669 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003670 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003671 }
3672 return Result;
3673}
3674
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003675/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3676/// to morph to an integer compare sequence.
3677static bool canChangeToInt(SDValue Op, bool &SeenZero,
3678 const ARMSubtarget *Subtarget) {
3679 SDNode *N = Op.getNode();
3680 if (!N->hasOneUse())
3681 // Otherwise it requires moving the value from fp to integer registers.
3682 return false;
3683 if (!N->getNumValues())
3684 return false;
3685 EVT VT = Op.getValueType();
3686 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3687 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3688 // vmrs are very slow, e.g. cortex-a8.
3689 return false;
3690
3691 if (isFloatingPointZero(Op)) {
3692 SeenZero = true;
3693 return true;
3694 }
3695 return ISD::isNormalLoad(N);
3696}
3697
3698static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3699 if (isFloatingPointZero(Op))
3700 return DAG.getConstant(0, MVT::i32);
3701
3702 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003703 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003704 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003705 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003706 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003707
3708 llvm_unreachable("Unknown VFP cmp argument!");
3709}
3710
3711static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3712 SDValue &RetVal1, SDValue &RetVal2) {
3713 if (isFloatingPointZero(Op)) {
3714 RetVal1 = DAG.getConstant(0, MVT::i32);
3715 RetVal2 = DAG.getConstant(0, MVT::i32);
3716 return;
3717 }
3718
3719 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3720 SDValue Ptr = Ld->getBasePtr();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003721 RetVal1 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003722 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003723 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003724 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003725 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003726
3727 EVT PtrType = Ptr.getValueType();
3728 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003729 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003730 PtrType, Ptr, DAG.getConstant(4, PtrType));
Andrew Trickef9de2a2013-05-25 02:42:55 +00003731 RetVal2 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003732 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003733 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003734 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003735 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003736 return;
3737 }
3738
3739 llvm_unreachable("Unknown VFP cmp argument!");
3740}
3741
3742/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3743/// f32 and even f64 comparisons to integer ones.
3744SDValue
3745ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3746 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003747 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003748 SDValue LHS = Op.getOperand(2);
3749 SDValue RHS = Op.getOperand(3);
3750 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003751 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003752
Evan Chengd12af5d2012-03-01 23:27:13 +00003753 bool LHSSeenZero = false;
3754 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3755 bool RHSSeenZero = false;
3756 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3757 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003758 // If unsafe fp math optimization is enabled and there are no other uses of
3759 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003760 // to an integer comparison.
3761 if (CC == ISD::SETOEQ)
3762 CC = ISD::SETEQ;
3763 else if (CC == ISD::SETUNE)
3764 CC = ISD::SETNE;
3765
Evan Chengd12af5d2012-03-01 23:27:13 +00003766 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003767 SDValue ARMcc;
3768 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003769 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3770 bitcastf32Toi32(LHS, DAG), Mask);
3771 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3772 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003773 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3774 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3775 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3776 Chain, Dest, ARMcc, CCR, Cmp);
3777 }
3778
3779 SDValue LHS1, LHS2;
3780 SDValue RHS1, RHS2;
3781 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3782 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003783 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3784 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003785 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3786 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003787 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003788 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003789 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003790 }
3791
3792 return SDValue();
3793}
3794
3795SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3796 SDValue Chain = Op.getOperand(0);
3797 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3798 SDValue LHS = Op.getOperand(2);
3799 SDValue RHS = Op.getOperand(3);
3800 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003801 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003802
Oliver Stannard51b1d462014-08-21 12:50:31 +00003803 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3804 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3805 dl);
3806
3807 // If softenSetCCOperands only returned one value, we should compare it to
3808 // zero.
3809 if (!RHS.getNode()) {
3810 RHS = DAG.getConstant(0, LHS.getValueType());
3811 CC = ISD::SETNE;
3812 }
3813 }
3814
Owen Anderson9f944592009-08-11 20:47:22 +00003815 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003816 SDValue ARMcc;
3817 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003818 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003819 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003820 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003821 }
3822
Owen Anderson9f944592009-08-11 20:47:22 +00003823 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003824
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003825 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003826 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3827 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3828 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3829 if (Result.getNode())
3830 return Result;
3831 }
3832
Evan Cheng10043e22007-01-19 07:51:42 +00003833 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003834 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003835
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003836 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3837 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003838 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003839 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003840 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003841 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003842 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003843 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3844 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003845 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003846 }
3847 return Res;
3848}
3849
Dan Gohman21cea8a2010-04-17 15:26:15 +00003850SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003851 SDValue Chain = Op.getOperand(0);
3852 SDValue Table = Op.getOperand(1);
3853 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003854 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003855
Owen Anderson53aa7a92009-08-10 22:56:29 +00003856 EVT PTy = getPointerTy();
Evan Cheng10043e22007-01-19 07:51:42 +00003857 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3858 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3f17aee2009-07-14 18:44:34 +00003859 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003860 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson9f944592009-08-11 20:47:22 +00003861 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chengc8bed032009-07-28 20:53:24 +00003862 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3863 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003864 if (Subtarget->isThumb2()) {
3865 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3866 // which does another jump to the destination. This also makes it easier
3867 // to translate it to TBB / TBH later.
3868 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00003869 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Chengc6d70ae2009-07-29 02:18:14 +00003870 Addr, Op.getOperand(2), JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003871 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00003872 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003873 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattner7727d052010-09-21 06:44:06 +00003874 MachinePointerInfo::getJumpTable(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003875 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003876 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00003877 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson9f944592009-08-11 20:47:22 +00003878 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003879 } else {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003880 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003881 MachinePointerInfo::getJumpTable(),
3882 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003883 Chain = Addr.getValue(1);
Owen Anderson9f944592009-08-11 20:47:22 +00003884 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003885 }
Evan Cheng10043e22007-01-19 07:51:42 +00003886}
3887
Eli Friedman2d4055b2011-11-09 23:36:02 +00003888static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00003889 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003890 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003891
James Molloy547d4c02012-02-20 09:24:05 +00003892 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3893 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3894 return Op;
3895 return DAG.UnrollVectorOp(Op.getNode());
3896 }
3897
3898 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3899 "Invalid type for custom lowering!");
3900 if (VT != MVT::v4i16)
3901 return DAG.UnrollVectorOp(Op.getNode());
3902
3903 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3904 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003905}
3906
Oliver Stannard51b1d462014-08-21 12:50:31 +00003907SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const {
Eli Friedman2d4055b2011-11-09 23:36:02 +00003908 EVT VT = Op.getValueType();
3909 if (VT.isVector())
3910 return LowerVectorFP_TO_INT(Op, DAG);
3911
Oliver Stannard51b1d462014-08-21 12:50:31 +00003912 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) {
3913 RTLIB::Libcall LC;
3914 if (Op.getOpcode() == ISD::FP_TO_SINT)
3915 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(),
3916 Op.getValueType());
3917 else
3918 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(),
3919 Op.getValueType());
3920 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1,
3921 /*isSigned*/ false, SDLoc(Op)).first;
3922 }
3923
Andrew Trickef9de2a2013-05-25 02:42:55 +00003924 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003925 unsigned Opc;
3926
3927 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003928 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003929 case ISD::FP_TO_SINT:
3930 Opc = ARMISD::FTOSI;
3931 break;
3932 case ISD::FP_TO_UINT:
3933 Opc = ARMISD::FTOUI;
3934 break;
3935 }
3936 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peck527da1b2010-11-23 03:31:01 +00003937 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003938}
3939
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003940static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3941 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003942 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003943
Eli Friedman2d4055b2011-11-09 23:36:02 +00003944 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3945 if (VT.getVectorElementType() == MVT::f32)
3946 return Op;
3947 return DAG.UnrollVectorOp(Op.getNode());
3948 }
3949
Duncan Sandsa41634e2011-08-12 14:54:45 +00003950 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3951 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003952 if (VT != MVT::v4f32)
3953 return DAG.UnrollVectorOp(Op.getNode());
3954
3955 unsigned CastOpc;
3956 unsigned Opc;
3957 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003958 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003959 case ISD::SINT_TO_FP:
3960 CastOpc = ISD::SIGN_EXTEND;
3961 Opc = ISD::SINT_TO_FP;
3962 break;
3963 case ISD::UINT_TO_FP:
3964 CastOpc = ISD::ZERO_EXTEND;
3965 Opc = ISD::UINT_TO_FP;
3966 break;
3967 }
3968
3969 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3970 return DAG.getNode(Opc, dl, VT, Op);
3971}
3972
Oliver Stannard51b1d462014-08-21 12:50:31 +00003973SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const {
Bob Wilsone4191e72010-03-19 22:51:32 +00003974 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003975 if (VT.isVector())
3976 return LowerVectorINT_TO_FP(Op, DAG);
3977
Oliver Stannard51b1d462014-08-21 12:50:31 +00003978 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) {
3979 RTLIB::Libcall LC;
3980 if (Op.getOpcode() == ISD::SINT_TO_FP)
3981 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(),
3982 Op.getValueType());
3983 else
3984 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(),
3985 Op.getValueType());
3986 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1,
3987 /*isSigned*/ false, SDLoc(Op)).first;
3988 }
3989
Andrew Trickef9de2a2013-05-25 02:42:55 +00003990 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003991 unsigned Opc;
3992
3993 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003994 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003995 case ISD::SINT_TO_FP:
3996 Opc = ARMISD::SITOF;
3997 break;
3998 case ISD::UINT_TO_FP:
3999 Opc = ARMISD::UITOF;
4000 break;
4001 }
4002
Wesley Peck527da1b2010-11-23 03:31:01 +00004003 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilsone4191e72010-03-19 22:51:32 +00004004 return DAG.getNode(Opc, dl, VT, Op);
4005}
4006
Evan Cheng25f93642010-07-08 02:08:50 +00004007SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00004008 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004009 SDValue Tmp0 = Op.getOperand(0);
4010 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004011 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004012 EVT VT = Op.getValueType();
4013 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00004014 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
4015 Tmp0.getOpcode() == ARMISD::VMOVDRR;
4016 bool UseNEON = !InGPR && Subtarget->hasNEON();
4017
4018 if (UseNEON) {
4019 // Use VBSL to copy the sign bit.
4020 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
4021 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
4022 DAG.getTargetConstant(EncodedVal, MVT::i32));
4023 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
4024 if (VT == MVT::f64)
4025 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4026 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
4027 DAG.getConstant(32, MVT::i32));
4028 else /*if (VT == MVT::f32)*/
4029 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
4030 if (SrcVT == MVT::f32) {
4031 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
4032 if (VT == MVT::f64)
4033 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4034 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
4035 DAG.getConstant(32, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00004036 } else if (VT == MVT::f32)
4037 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
4038 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
4039 DAG.getConstant(32, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004040 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
4041 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
4042
4043 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
4044 MVT::i32);
4045 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
4046 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
4047 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00004048
Evan Chengd6b641e2011-02-23 02:24:55 +00004049 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
4050 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
4051 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00004052 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00004053 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
4054 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
4055 DAG.getConstant(0, MVT::i32));
4056 } else {
4057 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
4058 }
4059
4060 return Res;
4061 }
Evan Cheng2da1c952011-02-11 02:28:55 +00004062
4063 // Bitcast operand 1 to i32.
4064 if (SrcVT == MVT::f64)
4065 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004066 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00004067 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
4068
Evan Chengd6b641e2011-02-23 02:24:55 +00004069 // Or in the signbit with integer operations.
4070 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
4071 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
4072 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
4073 if (VT == MVT::f32) {
4074 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
4075 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
4076 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4077 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00004078 }
4079
Evan Chengd6b641e2011-02-23 02:24:55 +00004080 // f64: Or the high part with signbit and then combine two parts.
4081 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004082 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00004083 SDValue Lo = Tmp0.getValue(0);
4084 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
4085 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
4086 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00004087}
4088
Evan Cheng168ced92010-05-22 01:47:14 +00004089SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
4090 MachineFunction &MF = DAG.getMachineFunction();
4091 MachineFrameInfo *MFI = MF.getFrameInfo();
4092 MFI->setReturnAddressIsTaken(true);
4093
Bill Wendling908bf812014-01-06 00:43:20 +00004094 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004095 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004096
Evan Cheng168ced92010-05-22 01:47:14 +00004097 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004098 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00004099 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4100 if (Depth) {
4101 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
4102 SDValue Offset = DAG.getConstant(4, MVT::i32);
4103 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
4104 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004105 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00004106 }
4107
4108 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00004109 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00004110 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
4111}
4112
Dan Gohman21cea8a2010-04-17 15:26:15 +00004113SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004114 const ARMBaseRegisterInfo &ARI =
4115 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
4116 MachineFunction &MF = DAG.getMachineFunction();
4117 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004118 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00004119
Owen Anderson53aa7a92009-08-10 22:56:29 +00004120 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004121 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004122 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004123 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004124 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
4125 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00004126 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
4127 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004128 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004129 return FrameAddr;
4130}
4131
Renato Golinc7aea402014-05-06 16:51:25 +00004132// FIXME? Maybe this could be a TableGen attribute on some registers and
4133// this table could be generated automatically from RegInfo.
Hal Finkelf0e086a2014-05-11 19:29:07 +00004134unsigned ARMTargetLowering::getRegisterByName(const char* RegName,
4135 EVT VT) const {
Renato Golinc7aea402014-05-06 16:51:25 +00004136 unsigned Reg = StringSwitch<unsigned>(RegName)
4137 .Case("sp", ARM::SP)
4138 .Default(0);
4139 if (Reg)
4140 return Reg;
4141 report_fatal_error("Invalid register name global variable");
4142}
4143
Wesley Peck527da1b2010-11-23 03:31:01 +00004144/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00004145/// expand a bit convert where either the source or destination type is i64 to
4146/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
4147/// operand type is illegal (e.g., v2f32 for a target that doesn't support
4148/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004149static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00004150 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004151 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004152 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00004153
Bob Wilson59b70ea2010-04-17 05:30:19 +00004154 // This function is only supposed to be called for i64 types, either as the
4155 // source or destination of the bit convert.
4156 EVT SrcVT = Op.getValueType();
4157 EVT DstVT = N->getValueType(0);
4158 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00004159 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00004160
Bob Wilson59b70ea2010-04-17 05:30:19 +00004161 // Turn i64->f64 into VMOVDRR.
4162 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson9f944592009-08-11 20:47:22 +00004163 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
4164 DAG.getConstant(0, MVT::i32));
4165 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
4166 DAG.getConstant(1, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00004167 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00004168 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00004169 }
Bob Wilson7117a912009-03-20 22:42:55 +00004170
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00004171 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00004172 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00004173 SDValue Cvt;
Christian Pirker6692e7c2014-05-14 16:59:44 +00004174 if (TLI.isBigEndian() && SrcVT.isVector() &&
4175 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00004176 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4177 DAG.getVTList(MVT::i32, MVT::i32),
4178 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
4179 else
4180 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4181 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00004182 // Merge the pieces into a single i64 value.
4183 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
4184 }
Bob Wilson7117a912009-03-20 22:42:55 +00004185
Bob Wilson59b70ea2010-04-17 05:30:19 +00004186 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00004187}
4188
Bob Wilson2e076c42009-06-22 23:27:02 +00004189/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00004190/// Zero vectors are used to represent vector negation and in those cases
4191/// will be implemented with the NEON VNEG instruction. However, VNEG does
4192/// not support i64 elements, so sometimes the zero vectors will need to be
4193/// explicitly constructed. Regardless, use a canonical VMOV to create the
4194/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004195static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004196 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00004197 // The canonical modified immediate encoding of a zero vector is....0!
4198 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
4199 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
4200 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00004201 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00004202}
4203
Jim Grosbach624fcb22009-10-31 21:00:56 +00004204/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
4205/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004206SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
4207 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00004208 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4209 EVT VT = Op.getValueType();
4210 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004211 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004212 SDValue ShOpLo = Op.getOperand(0);
4213 SDValue ShOpHi = Op.getOperand(1);
4214 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004215 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004216 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00004217
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004218 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
4219
Jim Grosbach624fcb22009-10-31 21:00:56 +00004220 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
4221 DAG.getConstant(VTBits, MVT::i32), ShAmt);
4222 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
4223 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
4224 DAG.getConstant(VTBits, MVT::i32));
4225 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
4226 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004227 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004228
4229 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4230 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004231 ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004232 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004233 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004234 CCR, Cmp);
4235
4236 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004237 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004238}
4239
Jim Grosbach5d994042009-10-31 19:38:01 +00004240/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4241/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004242SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4243 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004244 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4245 EVT VT = Op.getValueType();
4246 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004247 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004248 SDValue ShOpLo = Op.getOperand(0);
4249 SDValue ShOpHi = Op.getOperand(1);
4250 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004251 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004252
4253 assert(Op.getOpcode() == ISD::SHL_PARTS);
4254 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
4255 DAG.getConstant(VTBits, MVT::i32), ShAmt);
4256 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4257 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
4258 DAG.getConstant(VTBits, MVT::i32));
4259 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4260 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4261
4262 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4263 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4264 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004265 ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004266 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004267 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004268 CCR, Cmp);
4269
4270 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004271 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004272}
4273
Jim Grosbach535d3b42010-09-08 03:54:02 +00004274SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004275 SelectionDAG &DAG) const {
4276 // The rounding mode is in bits 23:22 of the FPSCR.
4277 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4278 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4279 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004280 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004281 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
4282 DAG.getConstant(Intrinsic::arm_get_fpscr,
4283 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004284 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemanb69b1822010-08-03 21:31:55 +00004285 DAG.getConstant(1U << 22, MVT::i32));
4286 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
4287 DAG.getConstant(22, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004288 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemanb69b1822010-08-03 21:31:55 +00004289 DAG.getConstant(3, MVT::i32));
4290}
4291
Jim Grosbach8546ec92010-01-18 19:58:49 +00004292static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4293 const ARMSubtarget *ST) {
4294 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004295 SDLoc dl(N);
Jim Grosbach8546ec92010-01-18 19:58:49 +00004296
4297 if (!ST->hasV6T2Ops())
4298 return SDValue();
4299
4300 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
4301 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4302}
4303
Evan Chengb4eae132012-12-04 22:41:50 +00004304/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4305/// for each 16-bit element from operand, repeated. The basic idea is to
4306/// leverage vcnt to get the 8-bit counts, gather and add the results.
4307///
4308/// Trace for v4i16:
4309/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4310/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4311/// 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 +00004312/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004313/// [b0 b1 b2 b3 b4 b5 b6 b7]
4314/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4315/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4316/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4317static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4318 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004319 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004320
4321 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4322 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4323 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4324 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4325 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4326 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4327}
4328
4329/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4330/// bit-count for each 16-bit element from the operand. We need slightly
4331/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4332/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004333///
Evan Chengb4eae132012-12-04 22:41:50 +00004334/// Trace for v4i16:
4335/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4336/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4337/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4338/// v4i16:Extracted = [k0 k1 k2 k3 ]
4339static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4340 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004341 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004342
4343 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4344 if (VT.is64BitVector()) {
4345 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4346 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
4347 DAG.getIntPtrConstant(0));
4348 } else {
4349 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
4350 BitCounts, DAG.getIntPtrConstant(0));
4351 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4352 }
4353}
4354
4355/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4356/// bit-count for each 32-bit element from the operand. The idea here is
4357/// to split the vector into 16-bit elements, leverage the 16-bit count
4358/// routine, and then combine the results.
4359///
4360/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4361/// input = [v0 v1 ] (vi: 32-bit elements)
4362/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4363/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004364/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004365/// [k0 k1 k2 k3 ]
4366/// N1 =+[k1 k0 k3 k2 ]
4367/// [k0 k2 k1 k3 ]
4368/// N2 =+[k1 k3 k0 k2 ]
4369/// [k0 k2 k1 k3 ]
4370/// Extended =+[k1 k3 k0 k2 ]
4371/// [k0 k2 ]
4372/// Extracted=+[k1 k3 ]
4373///
4374static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4375 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004376 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004377
4378 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4379
4380 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4381 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4382 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4383 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4384 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4385
4386 if (VT.is64BitVector()) {
4387 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4388 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
4389 DAG.getIntPtrConstant(0));
4390 } else {
4391 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
4392 DAG.getIntPtrConstant(0));
4393 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4394 }
4395}
4396
4397static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4398 const ARMSubtarget *ST) {
4399 EVT VT = N->getValueType(0);
4400
4401 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004402 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4403 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004404 "Unexpected type for custom ctpop lowering");
4405
4406 if (VT.getVectorElementType() == MVT::i32)
4407 return lowerCTPOP32BitElements(N, DAG);
4408 else
4409 return lowerCTPOP16BitElements(N, DAG);
4410}
4411
Bob Wilson2e076c42009-06-22 23:27:02 +00004412static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4413 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004414 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004415 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004416
Bob Wilson7d471332010-11-18 21:16:28 +00004417 if (!VT.isVector())
4418 return SDValue();
4419
Bob Wilson2e076c42009-06-22 23:27:02 +00004420 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004421 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004422
Bob Wilson7d471332010-11-18 21:16:28 +00004423 // Left shifts translate directly to the vshiftu intrinsic.
4424 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004425 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilson7d471332010-11-18 21:16:28 +00004426 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
4427 N->getOperand(0), N->getOperand(1));
4428
4429 assert((N->getOpcode() == ISD::SRA ||
4430 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4431
4432 // NEON uses the same intrinsics for both left and right shifts. For
4433 // right shifts, the shift amounts are negative, so negate the vector of
4434 // shift amounts.
4435 EVT ShiftVT = N->getOperand(1).getValueType();
4436 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4437 getZeroVector(ShiftVT, DAG, dl),
4438 N->getOperand(1));
4439 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4440 Intrinsic::arm_neon_vshifts :
4441 Intrinsic::arm_neon_vshiftu);
4442 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4443 DAG.getConstant(vshiftInt, MVT::i32),
4444 N->getOperand(0), NegatedCount);
4445}
4446
4447static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4448 const ARMSubtarget *ST) {
4449 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004450 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004451
Eli Friedman682d8c12009-08-22 03:13:10 +00004452 // We can get here for a node like i32 = ISD::SHL i32, i64
4453 if (VT != MVT::i64)
4454 return SDValue();
4455
4456 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004457 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004458
Chris Lattnerf81d5882007-11-24 07:07:01 +00004459 // We only lower SRA, SRL of 1 here, all others use generic lowering.
4460 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmaneffb8942008-09-12 16:56:44 +00004461 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands6ed40142008-12-01 11:39:25 +00004462 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004463
Chris Lattnerf81d5882007-11-24 07:07:01 +00004464 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004465 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004466
Chris Lattnerf81d5882007-11-24 07:07:01 +00004467 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004468 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004469 DAG.getConstant(0, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004470 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004471 DAG.getConstant(1, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004472
Chris Lattnerf81d5882007-11-24 07:07:01 +00004473 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4474 // captures the result into a carry flag.
4475 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004476 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004477
Chris Lattnerf81d5882007-11-24 07:07:01 +00004478 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004479 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004480
Chris Lattnerf81d5882007-11-24 07:07:01 +00004481 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004482 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004483}
4484
Bob Wilson2e076c42009-06-22 23:27:02 +00004485static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4486 SDValue TmpOp0, TmpOp1;
4487 bool Invert = false;
4488 bool Swap = false;
4489 unsigned Opc = 0;
4490
4491 SDValue Op0 = Op.getOperand(0);
4492 SDValue Op1 = Op.getOperand(1);
4493 SDValue CC = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004494 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004495 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004496 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004497
Oliver Stannard51b1d462014-08-21 12:50:31 +00004498 if (Op1.getValueType().isFloatingPoint()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004499 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004500 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004501 case ISD::SETUNE:
4502 case ISD::SETNE: Invert = true; // Fallthrough
4503 case ISD::SETOEQ:
4504 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4505 case ISD::SETOLT:
4506 case ISD::SETLT: Swap = true; // Fallthrough
4507 case ISD::SETOGT:
4508 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4509 case ISD::SETOLE:
4510 case ISD::SETLE: Swap = true; // Fallthrough
4511 case ISD::SETOGE:
4512 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4513 case ISD::SETUGE: Swap = true; // Fallthrough
4514 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4515 case ISD::SETUGT: Swap = true; // Fallthrough
4516 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4517 case ISD::SETUEQ: Invert = true; // Fallthrough
4518 case ISD::SETONE:
4519 // Expand this to (OLT | OGT).
4520 TmpOp0 = Op0;
4521 TmpOp1 = Op1;
4522 Opc = ISD::OR;
4523 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
4524 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
4525 break;
4526 case ISD::SETUO: Invert = true; // Fallthrough
4527 case ISD::SETO:
4528 // Expand this to (OLT | OGE).
4529 TmpOp0 = Op0;
4530 TmpOp1 = Op1;
4531 Opc = ISD::OR;
4532 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
4533 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
4534 break;
4535 }
4536 } else {
4537 // Integer comparisons.
4538 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004539 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004540 case ISD::SETNE: Invert = true;
4541 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4542 case ISD::SETLT: Swap = true;
4543 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4544 case ISD::SETLE: Swap = true;
4545 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4546 case ISD::SETULT: Swap = true;
4547 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4548 case ISD::SETULE: Swap = true;
4549 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4550 }
4551
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004552 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004553 if (Opc == ARMISD::VCEQ) {
4554
4555 SDValue AndOp;
4556 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4557 AndOp = Op0;
4558 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4559 AndOp = Op1;
4560
4561 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004562 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004563 AndOp = AndOp.getOperand(0);
4564
4565 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4566 Opc = ARMISD::VTST;
Wesley Peck527da1b2010-11-23 03:31:01 +00004567 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
4568 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004569 Invert = !Invert;
4570 }
4571 }
4572 }
4573
4574 if (Swap)
4575 std::swap(Op0, Op1);
4576
Owen Andersonc7baee32010-11-08 23:21:22 +00004577 // If one of the operands is a constant vector zero, attempt to fold the
4578 // comparison to a specialized compare-against-zero form.
4579 SDValue SingleOp;
4580 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4581 SingleOp = Op0;
4582 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4583 if (Opc == ARMISD::VCGE)
4584 Opc = ARMISD::VCLEZ;
4585 else if (Opc == ARMISD::VCGT)
4586 Opc = ARMISD::VCLTZ;
4587 SingleOp = Op1;
4588 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004589
Owen Andersonc7baee32010-11-08 23:21:22 +00004590 SDValue Result;
4591 if (SingleOp.getNode()) {
4592 switch (Opc) {
4593 case ARMISD::VCEQ:
4594 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
4595 case ARMISD::VCGE:
4596 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
4597 case ARMISD::VCLEZ:
4598 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
4599 case ARMISD::VCGT:
4600 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
4601 case ARMISD::VCLTZ:
4602 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
4603 default:
4604 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
4605 }
4606 } else {
4607 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
4608 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004609
4610 if (Invert)
4611 Result = DAG.getNOT(dl, Result, VT);
4612
4613 return Result;
4614}
4615
Bob Wilson5b2b5042010-06-14 22:19:57 +00004616/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4617/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004618/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004619static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4620 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Andersona4076922010-11-05 21:57:54 +00004621 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004622 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004623
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004624 // SplatBitSize is set to the smallest size that splats the vector, so a
4625 // zero vector will always have SplatBitSize == 8. However, NEON modified
4626 // immediate instructions others than VMOV do not support the 8-bit encoding
4627 // of a zero vector, and the default encoding of zero is supposed to be the
4628 // 32-bit version.
4629 if (SplatBits == 0)
4630 SplatBitSize = 32;
4631
Bob Wilson2e076c42009-06-22 23:27:02 +00004632 switch (SplatBitSize) {
4633 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004634 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004635 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004636 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004637 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004638 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004639 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004640 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004641 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004642
4643 case 16:
4644 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004645 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004646 if ((SplatBits & ~0xff) == 0) {
4647 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004648 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004649 Imm = SplatBits;
4650 break;
4651 }
4652 if ((SplatBits & ~0xff00) == 0) {
4653 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004654 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004655 Imm = SplatBits >> 8;
4656 break;
4657 }
4658 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004659
4660 case 32:
4661 // NEON's 32-bit VMOV supports splat values where:
4662 // * only one byte is nonzero, or
4663 // * the least significant byte is 0xff and the second byte is nonzero, or
4664 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004665 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004666 if ((SplatBits & ~0xff) == 0) {
4667 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004668 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004669 Imm = SplatBits;
4670 break;
4671 }
4672 if ((SplatBits & ~0xff00) == 0) {
4673 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004674 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004675 Imm = SplatBits >> 8;
4676 break;
4677 }
4678 if ((SplatBits & ~0xff0000) == 0) {
4679 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004680 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004681 Imm = SplatBits >> 16;
4682 break;
4683 }
4684 if ((SplatBits & ~0xff000000) == 0) {
4685 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004686 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004687 Imm = SplatBits >> 24;
4688 break;
4689 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004690
Owen Andersona4076922010-11-05 21:57:54 +00004691 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4692 if (type == OtherModImm) return SDValue();
4693
Bob Wilson2e076c42009-06-22 23:27:02 +00004694 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004695 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4696 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004697 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004698 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004699 break;
4700 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004701
4702 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004703 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4704 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004705 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004706 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004707 break;
4708 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004709
4710 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4711 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4712 // VMOV.I32. A (very) minor optimization would be to replicate the value
4713 // and fall through here to test for a valid 64-bit splat. But, then the
4714 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004715 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004716
4717 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004718 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004719 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004720 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00004721 uint64_t BitMask = 0xff;
4722 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004723 unsigned ImmMask = 1;
4724 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00004725 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00004726 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004727 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00004728 Imm |= ImmMask;
4729 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004730 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004731 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004732 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004733 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00004734 }
Christian Pirker6f81e752014-06-23 18:05:53 +00004735
4736 if (DAG.getTargetLoweringInfo().isBigEndian())
4737 // swap higher and lower 32 bit word
4738 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4);
4739
Bob Wilson6eae5202010-06-11 21:34:50 +00004740 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004741 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00004742 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00004743 break;
4744 }
4745
Bob Wilson6eae5202010-06-11 21:34:50 +00004746 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00004747 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00004748 }
4749
Bob Wilsona3f19012010-07-13 21:16:48 +00004750 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
4751 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00004752}
4753
Lang Hames591cdaf2012-03-29 21:56:11 +00004754SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4755 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004756 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00004757 return SDValue();
4758
Tim Northoverf79c3a52013-08-20 08:57:11 +00004759 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00004760 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004761
Oliver Stannard51b1d462014-08-21 12:50:31 +00004762 // Use the default (constant pool) lowering for double constants when we have
4763 // an SP-only FPU
4764 if (IsDouble && Subtarget->isFPOnlySP())
4765 return SDValue();
4766
Lang Hames591cdaf2012-03-29 21:56:11 +00004767 // Try splatting with a VMOV.f32...
4768 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00004769 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
4770
Lang Hames591cdaf2012-03-29 21:56:11 +00004771 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004772 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
4773 // We have code in place to select a valid ConstantFP already, no need to
4774 // do any mangling.
4775 return Op;
4776 }
4777
4778 // It's a float and we are trying to use NEON operations where
4779 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004780 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004781 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
4782 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4783 NewVal);
4784 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
4785 DAG.getConstant(0, MVT::i32));
4786 }
4787
Tim Northoverf79c3a52013-08-20 08:57:11 +00004788 // The rest of our options are NEON only, make sure that's allowed before
4789 // proceeding..
4790 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
4791 return SDValue();
4792
Lang Hames591cdaf2012-03-29 21:56:11 +00004793 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00004794 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
4795
4796 // It wouldn't really be worth bothering for doubles except for one very
4797 // important value, which does happen to match: 0.0. So make sure we don't do
4798 // anything stupid.
4799 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
4800 return SDValue();
4801
4802 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
4803 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4804 false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004805 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004806 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004807 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4808 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004809 if (IsDouble)
4810 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4811
4812 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004813 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4814 VecConstant);
4815 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4816 DAG.getConstant(0, MVT::i32));
4817 }
4818
4819 // Finally, try a VMVN.i32
Tim Northoverf79c3a52013-08-20 08:57:11 +00004820 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4821 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004822 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004823 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004824 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004825
4826 if (IsDouble)
4827 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4828
4829 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004830 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4831 VecConstant);
4832 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4833 DAG.getConstant(0, MVT::i32));
4834 }
4835
4836 return SDValue();
4837}
4838
Quentin Colombet8e1fe842012-11-02 21:32:17 +00004839// check if an VEXT instruction can handle the shuffle mask when the
4840// vector sources of the shuffle are the same.
4841static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4842 unsigned NumElts = VT.getVectorNumElements();
4843
4844 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4845 if (M[0] < 0)
4846 return false;
4847
4848 Imm = M[0];
4849
4850 // If this is a VEXT shuffle, the immediate value is the index of the first
4851 // element. The other shuffle indices must be the successive elements after
4852 // the first one.
4853 unsigned ExpectedElt = Imm;
4854 for (unsigned i = 1; i < NumElts; ++i) {
4855 // Increment the expected index. If it wraps around, just follow it
4856 // back to index zero and keep going.
4857 ++ExpectedElt;
4858 if (ExpectedElt == NumElts)
4859 ExpectedElt = 0;
4860
4861 if (M[i] < 0) continue; // ignore UNDEF indices
4862 if (ExpectedElt != static_cast<unsigned>(M[i]))
4863 return false;
4864 }
4865
4866 return true;
4867}
4868
Lang Hames591cdaf2012-03-29 21:56:11 +00004869
Benjamin Kramer339ced42012-01-15 13:16:05 +00004870static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004871 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004872 unsigned NumElts = VT.getVectorNumElements();
4873 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00004874
4875 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4876 if (M[0] < 0)
4877 return false;
4878
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004879 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00004880
4881 // If this is a VEXT shuffle, the immediate value is the index of the first
4882 // element. The other shuffle indices must be the successive elements after
4883 // the first one.
4884 unsigned ExpectedElt = Imm;
4885 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004886 // Increment the expected index. If it wraps around, it may still be
4887 // a VEXT but the source vectors must be swapped.
4888 ExpectedElt += 1;
4889 if (ExpectedElt == NumElts * 2) {
4890 ExpectedElt = 0;
4891 ReverseVEXT = true;
4892 }
4893
Bob Wilson411dfad2010-08-17 05:54:34 +00004894 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004895 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00004896 return false;
4897 }
4898
4899 // Adjust the index value if the source operands will be swapped.
4900 if (ReverseVEXT)
4901 Imm -= NumElts;
4902
Bob Wilson32cd8552009-08-19 17:03:43 +00004903 return true;
4904}
4905
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004906/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4907/// instruction with the specified blocksize. (The order of the elements
4908/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00004909static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004910 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4911 "Only possible block sizes for VREV are: 16, 32, 64");
4912
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004913 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00004914 if (EltSz == 64)
4915 return false;
4916
4917 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004918 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00004919 // If the first shuffle index is UNDEF, be optimistic.
4920 if (M[0] < 0)
4921 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004922
4923 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4924 return false;
4925
4926 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004927 if (M[i] < 0) continue; // ignore UNDEF indices
4928 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004929 return false;
4930 }
4931
4932 return true;
4933}
4934
Benjamin Kramer339ced42012-01-15 13:16:05 +00004935static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00004936 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
4937 // range, then 0 is placed into the resulting vector. So pretty much any mask
4938 // of 8 elements can work here.
4939 return VT == MVT::v8i8 && M.size() == 8;
4940}
4941
Benjamin Kramer339ced42012-01-15 13:16:05 +00004942static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004943 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4944 if (EltSz == 64)
4945 return false;
4946
Bob Wilsona7062312009-08-21 20:54:19 +00004947 unsigned NumElts = VT.getVectorNumElements();
4948 WhichResult = (M[0] == 0 ? 0 : 1);
4949 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004950 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4951 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsona7062312009-08-21 20:54:19 +00004952 return false;
4953 }
4954 return true;
4955}
4956
Bob Wilson0bbd3072009-12-03 06:40:55 +00004957/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
4958/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4959/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004960static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004961 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4962 if (EltSz == 64)
4963 return false;
4964
4965 unsigned NumElts = VT.getVectorNumElements();
4966 WhichResult = (M[0] == 0 ? 0 : 1);
4967 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004968 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4969 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004970 return false;
4971 }
4972 return true;
4973}
4974
Benjamin Kramer339ced42012-01-15 13:16:05 +00004975static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004976 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4977 if (EltSz == 64)
4978 return false;
4979
Bob Wilsona7062312009-08-21 20:54:19 +00004980 unsigned NumElts = VT.getVectorNumElements();
4981 WhichResult = (M[0] == 0 ? 0 : 1);
4982 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004983 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsona7062312009-08-21 20:54:19 +00004984 if ((unsigned) M[i] != 2 * i + WhichResult)
4985 return false;
4986 }
4987
4988 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004989 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004990 return false;
4991
4992 return true;
4993}
4994
Bob Wilson0bbd3072009-12-03 06:40:55 +00004995/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4996/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4997/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00004998static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004999 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5000 if (EltSz == 64)
5001 return false;
5002
5003 unsigned Half = VT.getVectorNumElements() / 2;
5004 WhichResult = (M[0] == 0 ? 0 : 1);
5005 for (unsigned j = 0; j != 2; ++j) {
5006 unsigned Idx = WhichResult;
5007 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005008 int MIdx = M[i + j * Half];
5009 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson0bbd3072009-12-03 06:40:55 +00005010 return false;
5011 Idx += 2;
5012 }
5013 }
5014
5015 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5016 if (VT.is64BitVector() && EltSz == 32)
5017 return false;
5018
5019 return true;
5020}
5021
Benjamin Kramer339ced42012-01-15 13:16:05 +00005022static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005023 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5024 if (EltSz == 64)
5025 return false;
5026
Bob Wilsona7062312009-08-21 20:54:19 +00005027 unsigned NumElts = VT.getVectorNumElements();
5028 WhichResult = (M[0] == 0 ? 0 : 1);
5029 unsigned Idx = WhichResult * NumElts / 2;
5030 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005031 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
5032 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsona7062312009-08-21 20:54:19 +00005033 return false;
5034 Idx += 1;
5035 }
5036
5037 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005038 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005039 return false;
5040
5041 return true;
5042}
5043
Bob Wilson0bbd3072009-12-03 06:40:55 +00005044/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
5045/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5046/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005047static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005048 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5049 if (EltSz == 64)
5050 return false;
5051
5052 unsigned NumElts = VT.getVectorNumElements();
5053 WhichResult = (M[0] == 0 ? 0 : 1);
5054 unsigned Idx = WhichResult * NumElts / 2;
5055 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005056 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
5057 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson0bbd3072009-12-03 06:40:55 +00005058 return false;
5059 Idx += 1;
5060 }
5061
5062 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5063 if (VT.is64BitVector() && EltSz == 32)
5064 return false;
5065
5066 return true;
5067}
5068
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005069/// \return true if this is a reverse operation on an vector.
5070static bool isReverseMask(ArrayRef<int> M, EVT VT) {
5071 unsigned NumElts = VT.getVectorNumElements();
5072 // Make sure the mask has the right size.
5073 if (NumElts != M.size())
5074 return false;
5075
5076 // Look for <15, ..., 3, -1, 1, 0>.
5077 for (unsigned i = 0; i != NumElts; ++i)
5078 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
5079 return false;
5080
5081 return true;
5082}
5083
Dale Johannesen2bff5052010-07-29 20:10:08 +00005084// If N is an integer constant that can be moved into a register in one
5085// instruction, return an SDValue of such a constant (will become a MOV
5086// instruction). Otherwise return null.
5087static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005088 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00005089 uint64_t Val;
5090 if (!isa<ConstantSDNode>(N))
5091 return SDValue();
5092 Val = cast<ConstantSDNode>(N)->getZExtValue();
5093
5094 if (ST->isThumb1Only()) {
5095 if (Val <= 255 || ~Val <= 255)
5096 return DAG.getConstant(Val, MVT::i32);
5097 } else {
5098 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
5099 return DAG.getConstant(Val, MVT::i32);
5100 }
5101 return SDValue();
5102}
5103
Bob Wilson2e076c42009-06-22 23:27:02 +00005104// If this is a case we can't handle, return null and let the default
5105// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005106SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
5107 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00005108 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005109 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005110 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00005111
5112 APInt SplatBits, SplatUndef;
5113 unsigned SplatBitSize;
5114 bool HasAnyUndefs;
5115 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005116 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00005117 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00005118 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00005119 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00005120 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00005121 DAG, VmovVT, VT.is128BitVector(),
5122 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00005123 if (Val.getNode()) {
5124 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005125 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00005126 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00005127
5128 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00005129 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00005130 Val = isNEONModifiedImm(NegatedImm,
5131 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00005132 DAG, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005133 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005134 if (Val.getNode()) {
5135 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005136 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005137 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005138
5139 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00005140 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00005141 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005142 if (ImmVal != -1) {
5143 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
5144 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
5145 }
5146 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005147 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00005148 }
5149
Bob Wilson91fdf682010-05-22 00:23:12 +00005150 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00005151 //
5152 // As an optimisation, even if more than one value is used it may be more
5153 // profitable to splat with one value then change some lanes.
5154 //
5155 // Heuristically we decide to do this if the vector has a "dominant" value,
5156 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00005157 unsigned NumElts = VT.getVectorNumElements();
5158 bool isOnlyLowElement = true;
5159 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005160 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00005161 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005162
5163 // Map of the number of times a particular SDValue appears in the
5164 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00005165 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00005166 SDValue Value;
5167 for (unsigned i = 0; i < NumElts; ++i) {
5168 SDValue V = Op.getOperand(i);
5169 if (V.getOpcode() == ISD::UNDEF)
5170 continue;
5171 if (i > 0)
5172 isOnlyLowElement = false;
5173 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
5174 isConstant = false;
5175
James Molloy49bdbce2012-09-06 09:55:02 +00005176 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00005177 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00005178
James Molloy49bdbce2012-09-06 09:55:02 +00005179 // Is this value dominant? (takes up more than half of the lanes)
5180 if (++Count > (NumElts / 2)) {
5181 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00005182 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00005183 }
Bob Wilson91fdf682010-05-22 00:23:12 +00005184 }
James Molloy49bdbce2012-09-06 09:55:02 +00005185 if (ValueCounts.size() != 1)
5186 usesOnlyOneValue = false;
5187 if (!Value.getNode() && ValueCounts.size() > 0)
5188 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00005189
James Molloy49bdbce2012-09-06 09:55:02 +00005190 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00005191 return DAG.getUNDEF(VT);
5192
Quentin Colombet0f2fe742013-07-23 22:34:47 +00005193 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
5194 // Keep going if we are hitting this case.
5195 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00005196 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
5197
Dale Johannesen2bff5052010-07-29 20:10:08 +00005198 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5199
Dale Johannesen710a2d92010-10-19 20:00:17 +00005200 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
5201 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00005202 if (hasDominantValue && EltSize <= 32) {
5203 if (!isConstant) {
5204 SDValue N;
5205
5206 // If we are VDUPing a value that comes directly from a vector, that will
5207 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005208 // just use VDUPLANE. We can only do this if the lane being extracted
5209 // is at a constant index, as the VDUP from lane instructions only have
5210 // constant-index forms.
5211 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5212 isa<ConstantSDNode>(Value->getOperand(1))) {
Silviu Barangab1409702012-10-15 09:41:32 +00005213 // We need to create a new undef vector to use for the VDUPLANE if the
5214 // size of the vector from which we get the value is different than the
5215 // size of the vector that we need to create. We will insert the element
5216 // such that the register coalescer will remove unnecessary copies.
5217 if (VT != Value->getOperand(0).getValueType()) {
5218 ConstantSDNode *constIndex;
5219 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
5220 assert(constIndex && "The index is not a constant!");
5221 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
5222 VT.getVectorNumElements();
5223 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
5224 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
5225 Value, DAG.getConstant(index, MVT::i32)),
5226 DAG.getConstant(index, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005227 } else
Silviu Barangab1409702012-10-15 09:41:32 +00005228 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00005229 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005230 } else
James Molloy49bdbce2012-09-06 09:55:02 +00005231 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
5232
5233 if (!usesOnlyOneValue) {
5234 // The dominant value was splatted as 'N', but we now have to insert
5235 // all differing elements.
5236 for (unsigned I = 0; I < NumElts; ++I) {
5237 if (Op.getOperand(I) == Value)
5238 continue;
5239 SmallVector<SDValue, 3> Ops;
5240 Ops.push_back(N);
5241 Ops.push_back(Op.getOperand(I));
5242 Ops.push_back(DAG.getConstant(I, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00005243 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00005244 }
5245 }
5246 return N;
5247 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00005248 if (VT.getVectorElementType().isFloatingPoint()) {
5249 SmallVector<SDValue, 8> Ops;
5250 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005251 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005252 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005253 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00005254 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005255 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5256 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005257 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005258 }
James Molloy49bdbce2012-09-06 09:55:02 +00005259 if (usesOnlyOneValue) {
5260 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5261 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005262 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005263 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005264 }
5265
5266 // If all elements are constants and the case above didn't get hit, fall back
5267 // to the default expansion, which will generate a load from the constant
5268 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005269 if (isConstant)
5270 return SDValue();
5271
Bob Wilson6f2b8962011-01-07 21:37:30 +00005272 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5273 if (NumElts >= 4) {
5274 SDValue shuffle = ReconstructShuffle(Op, DAG);
5275 if (shuffle != SDValue())
5276 return shuffle;
5277 }
5278
Bob Wilson91fdf682010-05-22 00:23:12 +00005279 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005280 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5281 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005282 if (EltSize >= 32) {
5283 // Do the expansion with floating-point types, since that is what the VFP
5284 // registers are defined to use, and since i64 is not legal.
5285 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5286 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005287 SmallVector<SDValue, 8> Ops;
5288 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005289 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005290 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005291 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005292 }
5293
Jim Grosbach24e102a2013-07-08 18:18:52 +00005294 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5295 // know the default expansion would otherwise fall back on something even
5296 // worse. For a vector with one or two non-undef values, that's
5297 // scalar_to_vector for the elements followed by a shuffle (provided the
5298 // shuffle is valid for the target) and materialization element by element
5299 // on the stack followed by a load for everything else.
5300 if (!isConstant && !usesOnlyOneValue) {
5301 SDValue Vec = DAG.getUNDEF(VT);
5302 for (unsigned i = 0 ; i < NumElts; ++i) {
5303 SDValue V = Op.getOperand(i);
5304 if (V.getOpcode() == ISD::UNDEF)
5305 continue;
5306 SDValue LaneIdx = DAG.getConstant(i, MVT::i32);
5307 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5308 }
5309 return Vec;
5310 }
5311
Bob Wilson2e076c42009-06-22 23:27:02 +00005312 return SDValue();
5313}
5314
Bob Wilson6f2b8962011-01-07 21:37:30 +00005315// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005316// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005317SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5318 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005319 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005320 EVT VT = Op.getValueType();
5321 unsigned NumElts = VT.getVectorNumElements();
5322
5323 SmallVector<SDValue, 2> SourceVecs;
5324 SmallVector<unsigned, 2> MinElts;
5325 SmallVector<unsigned, 2> MaxElts;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005326
Bob Wilson6f2b8962011-01-07 21:37:30 +00005327 for (unsigned i = 0; i < NumElts; ++i) {
5328 SDValue V = Op.getOperand(i);
5329 if (V.getOpcode() == ISD::UNDEF)
5330 continue;
5331 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5332 // A shuffle can only come from building a vector from various
5333 // elements of other vectors.
5334 return SDValue();
Eli Friedman74d1da52011-10-14 23:58:49 +00005335 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
5336 VT.getVectorElementType()) {
5337 // This code doesn't know how to handle shuffles where the vector
5338 // element types do not match (this happens because type legalization
5339 // promotes the return type of EXTRACT_VECTOR_ELT).
5340 // FIXME: It might be appropriate to extend this code to handle
5341 // mismatched types.
5342 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005343 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005344
Bob Wilson6f2b8962011-01-07 21:37:30 +00005345 // Record this extraction against the appropriate vector if possible...
5346 SDValue SourceVec = V.getOperand(0);
Jim Grosbach6df755c2012-07-25 17:02:47 +00005347 // If the element number isn't a constant, we can't effectively
5348 // analyze what's going on.
5349 if (!isa<ConstantSDNode>(V.getOperand(1)))
5350 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005351 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5352 bool FoundSource = false;
5353 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
5354 if (SourceVecs[j] == SourceVec) {
5355 if (MinElts[j] > EltNo)
5356 MinElts[j] = EltNo;
5357 if (MaxElts[j] < EltNo)
5358 MaxElts[j] = EltNo;
5359 FoundSource = true;
5360 break;
5361 }
5362 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005363
Bob Wilson6f2b8962011-01-07 21:37:30 +00005364 // Or record a new source if not...
5365 if (!FoundSource) {
5366 SourceVecs.push_back(SourceVec);
5367 MinElts.push_back(EltNo);
5368 MaxElts.push_back(EltNo);
5369 }
5370 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005371
Bob Wilson6f2b8962011-01-07 21:37:30 +00005372 // Currently only do something sane when at most two source vectors
5373 // involved.
5374 if (SourceVecs.size() > 2)
5375 return SDValue();
5376
5377 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
5378 int VEXTOffsets[2] = {0, 0};
Andrew Trick5eb0a302011-01-19 02:26:13 +00005379
Bob Wilson6f2b8962011-01-07 21:37:30 +00005380 // This loop extracts the usage patterns of the source vectors
5381 // and prepares appropriate SDValues for a shuffle if possible.
5382 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
5383 if (SourceVecs[i].getValueType() == VT) {
5384 // No VEXT necessary
5385 ShuffleSrcs[i] = SourceVecs[i];
5386 VEXTOffsets[i] = 0;
5387 continue;
5388 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
5389 // It probably isn't worth padding out a smaller vector just to
5390 // break it down again in a shuffle.
5391 return SDValue();
5392 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005393
Bob Wilson6f2b8962011-01-07 21:37:30 +00005394 // Since only 64-bit and 128-bit vectors are legal on ARM and
5395 // we've eliminated the other cases...
Bob Wilson3fa9c062011-01-07 23:40:46 +00005396 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
5397 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick5eb0a302011-01-19 02:26:13 +00005398
Bob Wilson6f2b8962011-01-07 21:37:30 +00005399 if (MaxElts[i] - MinElts[i] >= NumElts) {
5400 // Span too large for a VEXT to cope
5401 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005402 }
5403
Bob Wilson6f2b8962011-01-07 21:37:30 +00005404 if (MinElts[i] >= NumElts) {
5405 // The extraction can just take the second half
5406 VEXTOffsets[i] = NumElts;
Eric Christopher2af95512011-01-14 23:50:53 +00005407 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5408 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005409 DAG.getIntPtrConstant(NumElts));
5410 } else if (MaxElts[i] < NumElts) {
5411 // The extraction can just take the first half
5412 VEXTOffsets[i] = 0;
Eric Christopher2af95512011-01-14 23:50:53 +00005413 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5414 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005415 DAG.getIntPtrConstant(0));
5416 } else {
5417 // An actual VEXT is needed
5418 VEXTOffsets[i] = MinElts[i];
Eric Christopher2af95512011-01-14 23:50:53 +00005419 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5420 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005421 DAG.getIntPtrConstant(0));
Eric Christopher2af95512011-01-14 23:50:53 +00005422 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5423 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005424 DAG.getIntPtrConstant(NumElts));
5425 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
5426 DAG.getConstant(VEXTOffsets[i], MVT::i32));
5427 }
5428 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005429
Bob Wilson6f2b8962011-01-07 21:37:30 +00005430 SmallVector<int, 8> Mask;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005431
Bob Wilson6f2b8962011-01-07 21:37:30 +00005432 for (unsigned i = 0; i < NumElts; ++i) {
5433 SDValue Entry = Op.getOperand(i);
5434 if (Entry.getOpcode() == ISD::UNDEF) {
5435 Mask.push_back(-1);
5436 continue;
5437 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005438
Bob Wilson6f2b8962011-01-07 21:37:30 +00005439 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher2af95512011-01-14 23:50:53 +00005440 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
5441 .getOperand(1))->getSExtValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005442 if (ExtractVec == SourceVecs[0]) {
5443 Mask.push_back(ExtractElt - VEXTOffsets[0]);
5444 } else {
5445 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
5446 }
5447 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005448
Bob Wilson6f2b8962011-01-07 21:37:30 +00005449 // Final check before we try to produce nonsense...
5450 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher2af95512011-01-14 23:50:53 +00005451 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
5452 &Mask[0]);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005453
Bob Wilson6f2b8962011-01-07 21:37:30 +00005454 return SDValue();
5455}
5456
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005457/// isShuffleMaskLegal - Targets can use this to indicate that they only
5458/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5459/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5460/// are assumed to be legal.
5461bool
5462ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5463 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005464 if (VT.getVectorNumElements() == 4 &&
5465 (VT.is128BitVector() || VT.is64BitVector())) {
5466 unsigned PFIndexes[4];
5467 for (unsigned i = 0; i != 4; ++i) {
5468 if (M[i] < 0)
5469 PFIndexes[i] = 8;
5470 else
5471 PFIndexes[i] = M[i];
5472 }
5473
5474 // Compute the index in the perfect shuffle table.
5475 unsigned PFTableIndex =
5476 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5477 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5478 unsigned Cost = (PFEntry >> 30);
5479
5480 if (Cost <= 4)
5481 return true;
5482 }
5483
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005484 bool ReverseVEXT;
Bob Wilsona7062312009-08-21 20:54:19 +00005485 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005486
Bob Wilson846bd792010-06-07 23:53:38 +00005487 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5488 return (EltSize >= 32 ||
5489 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005490 isVREVMask(M, VT, 64) ||
5491 isVREVMask(M, VT, 32) ||
5492 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005493 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005494 isVTBLMask(M, VT) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005495 isVTRNMask(M, VT, WhichResult) ||
5496 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson0bbd3072009-12-03 06:40:55 +00005497 isVZIPMask(M, VT, WhichResult) ||
5498 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
5499 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005500 isVZIP_v_undef_Mask(M, VT, WhichResult) ||
5501 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005502}
5503
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005504/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5505/// the specified operations to build the shuffle.
5506static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5507 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005508 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005509 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5510 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5511 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5512
5513 enum {
5514 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5515 OP_VREV,
5516 OP_VDUP0,
5517 OP_VDUP1,
5518 OP_VDUP2,
5519 OP_VDUP3,
5520 OP_VEXT1,
5521 OP_VEXT2,
5522 OP_VEXT3,
5523 OP_VUZPL, // VUZP, left result
5524 OP_VUZPR, // VUZP, right result
5525 OP_VZIPL, // VZIP, left result
5526 OP_VZIPR, // VZIP, right result
5527 OP_VTRNL, // VTRN, left result
5528 OP_VTRNR // VTRN, right result
5529 };
5530
5531 if (OpNum == OP_COPY) {
5532 if (LHSID == (1*9+2)*9+3) return LHS;
5533 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5534 return RHS;
5535 }
5536
5537 SDValue OpLHS, OpRHS;
5538 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5539 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5540 EVT VT = OpLHS.getValueType();
5541
5542 switch (OpNum) {
5543 default: llvm_unreachable("Unknown shuffle opcode!");
5544 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00005545 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00005546 if (VT.getVectorElementType() == MVT::i32 ||
5547 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00005548 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
5549 // vrev <4 x i16> -> VREV32
5550 if (VT.getVectorElementType() == MVT::i16)
5551 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
5552 // vrev <4 x i8> -> VREV16
5553 assert(VT.getVectorElementType() == MVT::i8);
5554 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005555 case OP_VDUP0:
5556 case OP_VDUP1:
5557 case OP_VDUP2:
5558 case OP_VDUP3:
5559 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005560 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005561 case OP_VEXT1:
5562 case OP_VEXT2:
5563 case OP_VEXT3:
5564 return DAG.getNode(ARMISD::VEXT, dl, VT,
5565 OpLHS, OpRHS,
5566 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
5567 case OP_VUZPL:
5568 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005569 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005570 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
5571 case OP_VZIPL:
5572 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005573 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005574 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
5575 case OP_VTRNL:
5576 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005577 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5578 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005579 }
5580}
5581
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005582static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005583 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005584 SelectionDAG &DAG) {
5585 // Check to see if we can use the VTBL instruction.
5586 SDValue V1 = Op.getOperand(0);
5587 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005588 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005589
5590 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00005591 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005592 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
5593 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
5594
5595 if (V2.getNode()->getOpcode() == ISD::UNDEF)
5596 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Craig Topper48d114b2014-04-26 18:35:24 +00005597 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00005598
Owen Anderson77aa2662011-04-05 21:48:57 +00005599 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Craig Topper48d114b2014-04-26 18:35:24 +00005600 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005601}
5602
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005603static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
5604 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005605 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005606 SDValue OpLHS = Op.getOperand(0);
5607 EVT VT = OpLHS.getValueType();
5608
5609 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
5610 "Expect an v8i16/v16i8 type");
5611 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
5612 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
5613 // extract the first 8 bytes into the top double word and the last 8 bytes
5614 // into the bottom double word. The v8i16 case is similar.
5615 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
5616 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
5617 DAG.getConstant(ExtractNum, MVT::i32));
5618}
5619
Bob Wilson2e076c42009-06-22 23:27:02 +00005620static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005621 SDValue V1 = Op.getOperand(0);
5622 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005623 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00005624 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005625 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00005626
Bob Wilsonc6800b52009-08-13 02:13:04 +00005627 // Convert shuffles that are directly supported on NEON to target-specific
5628 // DAG nodes, instead of keeping them as shuffles and matching them again
5629 // during code selection. This is more efficient and avoids the possibility
5630 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00005631 // FIXME: floating-point vectors should be canonicalized to integer vectors
5632 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005633 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005634
Bob Wilson846bd792010-06-07 23:53:38 +00005635 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5636 if (EltSize <= 32) {
5637 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
5638 int Lane = SVN->getSplatIndex();
5639 // If this is undef splat, generate it via "just" vdup, if possible.
5640 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00005641
Dan Gohman198b7ff2011-11-03 21:49:52 +00005642 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00005643 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5644 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5645 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005646 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
5647 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
5648 // reaches it).
5649 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
5650 !isa<ConstantSDNode>(V1.getOperand(0))) {
5651 bool IsScalarToVector = true;
5652 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
5653 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
5654 IsScalarToVector = false;
5655 break;
5656 }
5657 if (IsScalarToVector)
5658 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5659 }
Bob Wilson846bd792010-06-07 23:53:38 +00005660 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
5661 DAG.getConstant(Lane, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00005662 }
Bob Wilson846bd792010-06-07 23:53:38 +00005663
5664 bool ReverseVEXT;
5665 unsigned Imm;
5666 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
5667 if (ReverseVEXT)
5668 std::swap(V1, V2);
5669 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
5670 DAG.getConstant(Imm, MVT::i32));
5671 }
5672
5673 if (isVREVMask(ShuffleMask, VT, 64))
5674 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
5675 if (isVREVMask(ShuffleMask, VT, 32))
5676 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
5677 if (isVREVMask(ShuffleMask, VT, 16))
5678 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
5679
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005680 if (V2->getOpcode() == ISD::UNDEF &&
5681 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
5682 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
5683 DAG.getConstant(Imm, MVT::i32));
5684 }
5685
Bob Wilson846bd792010-06-07 23:53:38 +00005686 // Check for Neon shuffles that modify both input vectors in place.
5687 // If both results are used, i.e., if there are two shuffles with the same
5688 // source operands and with masks corresponding to both results of one of
5689 // these operations, DAG memoization will ensure that a single node is
5690 // used for both shuffles.
5691 unsigned WhichResult;
5692 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5693 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5694 V1, V2).getValue(WhichResult);
5695 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5696 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5697 V1, V2).getValue(WhichResult);
5698 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5699 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5700 V1, V2).getValue(WhichResult);
5701
5702 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5703 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5704 V1, V1).getValue(WhichResult);
5705 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5706 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5707 V1, V1).getValue(WhichResult);
5708 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5709 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5710 V1, V1).getValue(WhichResult);
Bob Wilsoncce31f62009-08-14 05:08:32 +00005711 }
Bob Wilson32cd8552009-08-19 17:03:43 +00005712
Bob Wilsona7062312009-08-21 20:54:19 +00005713 // If the shuffle is not directly supported and it has 4 elements, use
5714 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00005715 unsigned NumElts = VT.getVectorNumElements();
5716 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005717 unsigned PFIndexes[4];
5718 for (unsigned i = 0; i != 4; ++i) {
5719 if (ShuffleMask[i] < 0)
5720 PFIndexes[i] = 8;
5721 else
5722 PFIndexes[i] = ShuffleMask[i];
5723 }
5724
5725 // Compute the index in the perfect shuffle table.
5726 unsigned PFTableIndex =
5727 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005728 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5729 unsigned Cost = (PFEntry >> 30);
5730
5731 if (Cost <= 4)
5732 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
5733 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00005734
Bob Wilsond8a9a042010-06-04 00:04:02 +00005735 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00005736 if (EltSize >= 32) {
5737 // Do the expansion with floating-point types, since that is what the VFP
5738 // registers are defined to use, and since i64 is not legal.
5739 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5740 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00005741 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
5742 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005743 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00005744 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00005745 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00005746 Ops.push_back(DAG.getUNDEF(EltVT));
5747 else
5748 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
5749 ShuffleMask[i] < (int)NumElts ? V1 : V2,
5750 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
5751 MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00005752 }
Craig Topper48d114b2014-04-26 18:35:24 +00005753 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005754 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00005755 }
5756
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005757 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
5758 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
5759
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005760 if (VT == MVT::v8i8) {
5761 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
5762 if (NewOp.getNode())
5763 return NewOp;
5764 }
5765
Bob Wilson6f34e272009-08-14 05:16:33 +00005766 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005767}
5768
Eli Friedmana5e244c2011-10-24 23:08:52 +00005769static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
5770 // INSERT_VECTOR_ELT is legal only for immediate indexes.
5771 SDValue Lane = Op.getOperand(2);
5772 if (!isa<ConstantSDNode>(Lane))
5773 return SDValue();
5774
5775 return Op;
5776}
5777
Bob Wilson2e076c42009-06-22 23:27:02 +00005778static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00005779 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00005780 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00005781 if (!isa<ConstantSDNode>(Lane))
5782 return SDValue();
5783
5784 SDValue Vec = Op.getOperand(0);
5785 if (Op.getValueType() == MVT::i32 &&
5786 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005787 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00005788 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
5789 }
5790
5791 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00005792}
5793
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005794static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5795 // The only time a CONCAT_VECTORS operation can have legal types is when
5796 // two 64-bit vectors are concatenated to a 128-bit vector.
5797 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
5798 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005799 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005800 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005801 SDValue Op0 = Op.getOperand(0);
5802 SDValue Op1 = Op.getOperand(1);
5803 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005804 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005805 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005806 DAG.getIntPtrConstant(0));
5807 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005808 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005809 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005810 DAG.getIntPtrConstant(1));
Wesley Peck527da1b2010-11-23 03:31:01 +00005811 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005812}
5813
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005814/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
5815/// element has been zero/sign-extended, depending on the isSigned parameter,
5816/// from an integer type half its size.
5817static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
5818 bool isSigned) {
5819 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
5820 EVT VT = N->getValueType(0);
5821 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
5822 SDNode *BVN = N->getOperand(0).getNode();
5823 if (BVN->getValueType(0) != MVT::v4i32 ||
5824 BVN->getOpcode() != ISD::BUILD_VECTOR)
5825 return false;
5826 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5827 unsigned HiElt = 1 - LoElt;
5828 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
5829 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
5830 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
5831 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
5832 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
5833 return false;
5834 if (isSigned) {
5835 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
5836 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
5837 return true;
5838 } else {
5839 if (Hi0->isNullValue() && Hi1->isNullValue())
5840 return true;
5841 }
5842 return false;
5843 }
5844
5845 if (N->getOpcode() != ISD::BUILD_VECTOR)
5846 return false;
5847
5848 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5849 SDNode *Elt = N->getOperand(i).getNode();
5850 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
5851 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5852 unsigned HalfSize = EltSize / 2;
5853 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005854 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005855 return false;
5856 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005857 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005858 return false;
5859 }
5860 continue;
5861 }
5862 return false;
5863 }
5864
5865 return true;
5866}
5867
5868/// isSignExtended - Check if a node is a vector value that is sign-extended
5869/// or a constant BUILD_VECTOR with sign-extended elements.
5870static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
5871 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
5872 return true;
5873 if (isExtendedBUILD_VECTOR(N, DAG, true))
5874 return true;
5875 return false;
5876}
5877
5878/// isZeroExtended - Check if a node is a vector value that is zero-extended
5879/// or a constant BUILD_VECTOR with zero-extended elements.
5880static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
5881 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
5882 return true;
5883 if (isExtendedBUILD_VECTOR(N, DAG, false))
5884 return true;
5885 return false;
5886}
5887
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005888static EVT getExtensionTo64Bits(const EVT &OrigVT) {
5889 if (OrigVT.getSizeInBits() >= 64)
5890 return OrigVT;
5891
5892 assert(OrigVT.isSimple() && "Expecting a simple value type");
5893
5894 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
5895 switch (OrigSimpleTy) {
5896 default: llvm_unreachable("Unexpected Vector Type");
5897 case MVT::v2i8:
5898 case MVT::v2i16:
5899 return MVT::v2i32;
5900 case MVT::v4i8:
5901 return MVT::v4i16;
5902 }
5903}
5904
Sebastian Popa204f722012-11-30 19:08:04 +00005905/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
5906/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
5907/// We insert the required extension here to get the vector to fill a D register.
5908static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
5909 const EVT &OrigTy,
5910 const EVT &ExtTy,
5911 unsigned ExtOpcode) {
5912 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
5913 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
5914 // 64-bits we need to insert a new extension so that it will be 64-bits.
5915 assert(ExtTy.is128BitVector() && "Unexpected extension size");
5916 if (OrigTy.getSizeInBits() >= 64)
5917 return N;
5918
5919 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005920 EVT NewVT = getExtensionTo64Bits(OrigTy);
5921
Andrew Trickef9de2a2013-05-25 02:42:55 +00005922 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00005923}
5924
5925/// SkipLoadExtensionForVMULL - return a load of the original vector size that
5926/// does not do any sign/zero extension. If the original vector is less
5927/// than 64 bits, an appropriate extension will be added after the load to
5928/// reach a total size of 64 bits. We have to add the extension separately
5929/// because ARM does not have a sign/zero extending load for vectors.
5930static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005931 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
5932
5933 // The load already has the right type.
5934 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00005935 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00005936 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
5937 LD->isNonTemporal(), LD->isInvariant(),
5938 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005939
5940 // We need to create a zextload/sextload. We cannot just create a load
5941 // followed by a zext/zext node because LowerMUL is also run during normal
5942 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005943 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005944 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00005945 LD->getMemoryVT(), LD->isVolatile(), LD->isInvariant(),
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005946 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00005947}
5948
5949/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
5950/// extending load, or BUILD_VECTOR with extended elements, return the
5951/// unextended value. The unextended vector should be 64 bits so that it can
5952/// be used as an operand to a VMULL instruction. If the original vector size
5953/// before extension is less than 64 bits we add a an extension to resize
5954/// the vector to 64 bits.
5955static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00005956 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00005957 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
5958 N->getOperand(0)->getValueType(0),
5959 N->getValueType(0),
5960 N->getOpcode());
5961
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005962 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00005963 return SkipLoadExtensionForVMULL(LD, DAG);
5964
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005965 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
5966 // have been legalized as a BITCAST from v4i32.
5967 if (N->getOpcode() == ISD::BITCAST) {
5968 SDNode *BVN = N->getOperand(0).getNode();
5969 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
5970 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
5971 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00005972 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005973 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
5974 }
5975 // Construct a new BUILD_VECTOR with elements truncated to half the size.
5976 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
5977 EVT VT = N->getValueType(0);
5978 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
5979 unsigned NumElts = VT.getVectorNumElements();
5980 MVT TruncVT = MVT::getIntegerVT(EltSize);
5981 SmallVector<SDValue, 8> Ops;
5982 for (unsigned i = 0; i != NumElts; ++i) {
5983 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
5984 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00005985 // Element types smaller than 32 bits are not legal, so use i32 elements.
5986 // The values are implicitly truncated so sext vs. zext doesn't matter.
5987 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005988 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005989 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Craig Topper48d114b2014-04-26 18:35:24 +00005990 MVT::getVectorVT(TruncVT, NumElts), Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00005991}
5992
Evan Chenge2086e72011-03-29 01:56:09 +00005993static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
5994 unsigned Opcode = N->getOpcode();
5995 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5996 SDNode *N0 = N->getOperand(0).getNode();
5997 SDNode *N1 = N->getOperand(1).getNode();
5998 return N0->hasOneUse() && N1->hasOneUse() &&
5999 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
6000 }
6001 return false;
6002}
6003
6004static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
6005 unsigned Opcode = N->getOpcode();
6006 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6007 SDNode *N0 = N->getOperand(0).getNode();
6008 SDNode *N1 = N->getOperand(1).getNode();
6009 return N0->hasOneUse() && N1->hasOneUse() &&
6010 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
6011 }
6012 return false;
6013}
6014
Bob Wilson38ab35a2010-09-01 23:50:19 +00006015static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
6016 // Multiplications are only custom-lowered for 128-bit vectors so that
6017 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
6018 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00006019 assert(VT.is128BitVector() && VT.isInteger() &&
6020 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00006021 SDNode *N0 = Op.getOperand(0).getNode();
6022 SDNode *N1 = Op.getOperand(1).getNode();
6023 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00006024 bool isMLA = false;
6025 bool isN0SExt = isSignExtended(N0, DAG);
6026 bool isN1SExt = isSignExtended(N1, DAG);
6027 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00006028 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00006029 else {
6030 bool isN0ZExt = isZeroExtended(N0, DAG);
6031 bool isN1ZExt = isZeroExtended(N1, DAG);
6032 if (isN0ZExt && isN1ZExt)
6033 NewOpc = ARMISD::VMULLu;
6034 else if (isN1SExt || isN1ZExt) {
6035 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
6036 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
6037 if (isN1SExt && isAddSubSExt(N0, DAG)) {
6038 NewOpc = ARMISD::VMULLs;
6039 isMLA = true;
6040 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
6041 NewOpc = ARMISD::VMULLu;
6042 isMLA = true;
6043 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
6044 std::swap(N0, N1);
6045 NewOpc = ARMISD::VMULLu;
6046 isMLA = true;
6047 }
6048 }
6049
6050 if (!NewOpc) {
6051 if (VT == MVT::v2i64)
6052 // Fall through to expand this. It is not legal.
6053 return SDValue();
6054 else
6055 // Other vector multiplications are legal.
6056 return Op;
6057 }
6058 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006059
6060 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006061 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00006062 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00006063 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006064 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00006065 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006066 assert(Op0.getValueType().is64BitVector() &&
6067 Op1.getValueType().is64BitVector() &&
6068 "unexpected types for extended operands to VMULL");
6069 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
6070 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006071
Evan Chenge2086e72011-03-29 01:56:09 +00006072 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
6073 // isel lowering to take advantage of no-stall back to back vmul + vmla.
6074 // vmull q0, d4, d6
6075 // vmlal q0, d5, d6
6076 // is faster than
6077 // vaddl q0, d4, d5
6078 // vmovl q1, d6
6079 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00006080 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
6081 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006082 EVT Op1VT = Op1.getValueType();
6083 return DAG.getNode(N0->getOpcode(), DL, VT,
6084 DAG.getNode(NewOpc, DL, VT,
6085 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
6086 DAG.getNode(NewOpc, DL, VT,
6087 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00006088}
6089
Owen Anderson77aa2662011-04-05 21:48:57 +00006090static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006091LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00006092 // Convert to float
6093 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
6094 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
6095 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
6096 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
6097 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
6098 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
6099 // Get reciprocal estimate.
6100 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00006101 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006102 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
6103 // Because char has a smaller range than uchar, we can actually get away
6104 // without any newton steps. This requires that we use a weird bias
6105 // of 0xb000, however (again, this has been exhaustively tested).
6106 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
6107 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
6108 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
6109 Y = DAG.getConstant(0xb000, MVT::i32);
6110 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
6111 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
6112 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
6113 // Convert back to short.
6114 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
6115 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
6116 return X;
6117}
6118
Owen Anderson77aa2662011-04-05 21:48:57 +00006119static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006120LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00006121 SDValue N2;
6122 // Convert to float.
6123 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
6124 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
6125 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
6126 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
6127 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
6128 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006129
Nate Begemanfa62d502011-02-11 20:53:29 +00006130 // Use reciprocal estimate and one refinement step.
6131 // float4 recip = vrecpeq_f32(yf);
6132 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006133 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006134 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006135 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006136 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
6137 N1, N2);
6138 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6139 // Because short has a smaller range than ushort, we can actually get away
6140 // with only a single newton step. This requires that we use a weird bias
6141 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006142 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00006143 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6144 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006145 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006146 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6147 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6148 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6149 // Convert back to integer and return.
6150 // return vmovn_s32(vcvt_s32_f32(result));
6151 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6152 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6153 return N0;
6154}
6155
6156static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
6157 EVT VT = Op.getValueType();
6158 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6159 "unexpected type for custom-lowering ISD::SDIV");
6160
Andrew Trickef9de2a2013-05-25 02:42:55 +00006161 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006162 SDValue N0 = Op.getOperand(0);
6163 SDValue N1 = Op.getOperand(1);
6164 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006165
Nate Begemanfa62d502011-02-11 20:53:29 +00006166 if (VT == MVT::v8i8) {
6167 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
6168 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006169
Nate Begemanfa62d502011-02-11 20:53:29 +00006170 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6171 DAG.getIntPtrConstant(4));
6172 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00006173 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00006174 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6175 DAG.getIntPtrConstant(0));
6176 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
6177 DAG.getIntPtrConstant(0));
6178
6179 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
6180 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
6181
6182 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6183 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006184
Nate Begemanfa62d502011-02-11 20:53:29 +00006185 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
6186 return N0;
6187 }
6188 return LowerSDIV_v4i16(N0, N1, dl, DAG);
6189}
6190
6191static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
6192 EVT VT = Op.getValueType();
6193 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6194 "unexpected type for custom-lowering ISD::UDIV");
6195
Andrew Trickef9de2a2013-05-25 02:42:55 +00006196 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006197 SDValue N0 = Op.getOperand(0);
6198 SDValue N1 = Op.getOperand(1);
6199 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006200
Nate Begemanfa62d502011-02-11 20:53:29 +00006201 if (VT == MVT::v8i8) {
6202 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
6203 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006204
Nate Begemanfa62d502011-02-11 20:53:29 +00006205 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6206 DAG.getIntPtrConstant(4));
6207 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00006208 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00006209 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6210 DAG.getIntPtrConstant(0));
6211 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
6212 DAG.getIntPtrConstant(0));
Owen Anderson77aa2662011-04-05 21:48:57 +00006213
Nate Begemanfa62d502011-02-11 20:53:29 +00006214 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
6215 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00006216
Nate Begemanfa62d502011-02-11 20:53:29 +00006217 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6218 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006219
6220 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begemanfa62d502011-02-11 20:53:29 +00006221 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
6222 N0);
6223 return N0;
6224 }
Owen Anderson77aa2662011-04-05 21:48:57 +00006225
Nate Begemanfa62d502011-02-11 20:53:29 +00006226 // v4i16 sdiv ... Convert to float.
6227 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
6228 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
6229 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
6230 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
6231 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006232 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00006233
6234 // Use reciprocal estimate and two refinement steps.
6235 // float4 recip = vrecpeq_f32(yf);
6236 // recip *= vrecpsq_f32(yf, recip);
6237 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006238 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006239 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006240 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006241 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006242 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006243 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00006244 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006245 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006246 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006247 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6248 // Simply multiplying by the reciprocal estimate can leave us a few ulps
6249 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6250 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006251 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006252 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6253 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
6254 N1 = DAG.getConstant(2, MVT::i32);
6255 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6256 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6257 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6258 // Convert back to integer and return.
6259 // return vmovn_u32(vcvt_s32_f32(result));
6260 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6261 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6262 return N0;
6263}
6264
Evan Chenge8916542011-08-30 01:34:54 +00006265static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6266 EVT VT = Op.getNode()->getValueType(0);
6267 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6268
6269 unsigned Opc;
6270 bool ExtraOp = false;
6271 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006272 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006273 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6274 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6275 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6276 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6277 }
6278
6279 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006280 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006281 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006282 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006283 Op.getOperand(1), Op.getOperand(2));
6284}
6285
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006286SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6287 assert(Subtarget->isTargetDarwin());
6288
6289 // For iOS, we want to call an alternative entry point: __sincos_stret,
6290 // return values are passed via sret.
6291 SDLoc dl(Op);
6292 SDValue Arg = Op.getOperand(0);
6293 EVT ArgVT = Arg.getValueType();
6294 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
6295
6296 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
6297 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6298
6299 // Pair of floats / doubles used to pass the result.
6300 StructType *RetTy = StructType::get(ArgTy, ArgTy, NULL);
6301
6302 // Create stack object for sret.
6303 const uint64_t ByteSize = TLI.getDataLayout()->getTypeAllocSize(RetTy);
6304 const unsigned StackAlign = TLI.getDataLayout()->getPrefTypeAlignment(RetTy);
6305 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
6306 SDValue SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
6307
6308 ArgListTy Args;
6309 ArgListEntry Entry;
6310
6311 Entry.Node = SRet;
6312 Entry.Ty = RetTy->getPointerTo();
6313 Entry.isSExt = false;
6314 Entry.isZExt = false;
6315 Entry.isSRet = true;
6316 Args.push_back(Entry);
6317
6318 Entry.Node = Arg;
6319 Entry.Ty = ArgTy;
6320 Entry.isSExt = false;
6321 Entry.isZExt = false;
6322 Args.push_back(Entry);
6323
6324 const char *LibcallName = (ArgVT == MVT::f64)
6325 ? "__sincos_stret" : "__sincosf_stret";
6326 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
6327
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006328 TargetLowering::CallLoweringInfo CLI(DAG);
6329 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
6330 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), Callee,
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00006331 std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006332 .setDiscardResult();
6333
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006334 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6335
6336 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6337 MachinePointerInfo(), false, false, false, 0);
6338
6339 // Address of cos field.
6340 SDValue Add = DAG.getNode(ISD::ADD, dl, getPointerTy(), SRet,
6341 DAG.getIntPtrConstant(ArgVT.getStoreSize()));
6342 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6343 MachinePointerInfo(), false, false, false, 0);
6344
6345 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6346 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6347 LoadSin.getValue(0), LoadCos.getValue(0));
6348}
6349
Eli Friedman10f9ce22011-09-15 22:26:18 +00006350static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00006351 // Monotonic load/store is legal for all targets
6352 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
6353 return Op;
6354
Alp Tokercb402912014-01-24 17:20:08 +00006355 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00006356 // dmb or equivalent available.
6357 return SDValue();
6358}
6359
Tim Northoverbc933082013-05-23 19:11:20 +00006360static void ReplaceREADCYCLECOUNTER(SDNode *N,
6361 SmallVectorImpl<SDValue> &Results,
6362 SelectionDAG &DAG,
6363 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006364 SDLoc DL(N);
Tim Northoverbc933082013-05-23 19:11:20 +00006365 SDValue Cycles32, OutChain;
6366
6367 if (Subtarget->hasPerfMon()) {
6368 // Under Power Management extensions, the cycle-count is:
6369 // mrc p15, #0, <Rt>, c9, c13, #0
6370 SDValue Ops[] = { N->getOperand(0), // Chain
6371 DAG.getConstant(Intrinsic::arm_mrc, MVT::i32),
6372 DAG.getConstant(15, MVT::i32),
6373 DAG.getConstant(0, MVT::i32),
6374 DAG.getConstant(9, MVT::i32),
6375 DAG.getConstant(13, MVT::i32),
6376 DAG.getConstant(0, MVT::i32)
6377 };
6378
6379 Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00006380 DAG.getVTList(MVT::i32, MVT::Other), Ops);
Tim Northoverbc933082013-05-23 19:11:20 +00006381 OutChain = Cycles32.getValue(1);
6382 } else {
6383 // Intrinsic is defined to return 0 on unsupported platforms. Technically
6384 // there are older ARM CPUs that have implementation-specific ways of
6385 // obtaining this information (FIXME!).
6386 Cycles32 = DAG.getConstant(0, MVT::i32);
6387 OutChain = DAG.getEntryNode();
6388 }
6389
6390
6391 SDValue Cycles64 = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64,
6392 Cycles32, DAG.getConstant(0, MVT::i32));
6393 Results.push_back(Cycles64);
6394 Results.push_back(OutChain);
6395}
6396
Dan Gohman21cea8a2010-04-17 15:26:15 +00006397SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006398 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006399 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Cheng10043e22007-01-19 07:51:42 +00006400 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006401 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006402 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00006403 switch (Subtarget->getTargetTriple().getObjectFormat()) {
6404 default: llvm_unreachable("unknown object format");
6405 case Triple::COFF:
6406 return LowerGlobalAddressWindows(Op, DAG);
6407 case Triple::ELF:
6408 return LowerGlobalAddressELF(Op, DAG);
6409 case Triple::MachO:
6410 return LowerGlobalAddressDarwin(Op, DAG);
6411 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006412 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006413 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006414 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6415 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006416 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006417 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006418 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006419 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006420 case ISD::SINT_TO_FP:
6421 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6422 case ISD::FP_TO_SINT:
6423 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006424 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006425 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006426 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006427 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006428 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006429 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006430 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6431 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006432 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006433 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006434 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006435 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng15b80e42009-11-12 07:13:11 +00006436 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00006437 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00006438 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach8546ec92010-01-18 19:58:49 +00006439 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00006440 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00006441 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00006442 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006443 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00006444 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00006445 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006446 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006447 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00006448 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006449 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begemanfa62d502011-02-11 20:53:29 +00006450 case ISD::SDIV: return LowerSDIV(Op, DAG);
6451 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00006452 case ISD::ADDC:
6453 case ISD::ADDE:
6454 case ISD::SUBC:
6455 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00006456 case ISD::SADDO:
6457 case ISD::UADDO:
6458 case ISD::SSUBO:
6459 case ISD::USUBO:
6460 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00006461 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00006462 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006463 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00006464 case ISD::SDIVREM:
6465 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00006466 case ISD::DYNAMIC_STACKALLOC:
6467 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
6468 return LowerDYNAMIC_STACKALLOC(Op, DAG);
6469 llvm_unreachable("Don't know how to custom lower this!");
Oliver Stannard51b1d462014-08-21 12:50:31 +00006470 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG);
6471 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006472 }
Evan Cheng10043e22007-01-19 07:51:42 +00006473}
6474
Duncan Sands6ed40142008-12-01 11:39:25 +00006475/// ReplaceNodeResults - Replace the results of node with an illegal result
6476/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00006477void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
6478 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006479 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00006480 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006481 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006482 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00006483 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peck527da1b2010-11-23 03:31:01 +00006484 case ISD::BITCAST:
6485 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006486 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006487 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00006488 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00006489 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006490 break;
Tim Northoverbc933082013-05-23 19:11:20 +00006491 case ISD::READCYCLECOUNTER:
6492 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
6493 return;
Duncan Sands6ed40142008-12-01 11:39:25 +00006494 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00006495 if (Res.getNode())
6496 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00006497}
Chris Lattnerf81d5882007-11-24 07:07:01 +00006498
Evan Cheng10043e22007-01-19 07:51:42 +00006499//===----------------------------------------------------------------------===//
6500// ARM Scheduler Hooks
6501//===----------------------------------------------------------------------===//
6502
Bill Wendling030b58e2011-10-06 22:18:16 +00006503/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6504/// registers the function context.
6505void ARMTargetLowering::
6506SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6507 MachineBasicBlock *DispatchBB, int FI) const {
Eric Christopherd9134482014-08-04 21:25:23 +00006508 const TargetInstrInfo *TII =
6509 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Bill Wendling374ee192011-10-03 21:25:38 +00006510 DebugLoc dl = MI->getDebugLoc();
6511 MachineFunction *MF = MBB->getParent();
6512 MachineRegisterInfo *MRI = &MF->getRegInfo();
6513 MachineConstantPool *MCP = MF->getConstantPool();
6514 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6515 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00006516
Bill Wendling374ee192011-10-03 21:25:38 +00006517 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006518 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00006519
Bill Wendling374ee192011-10-03 21:25:38 +00006520 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006521 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00006522 ARMConstantPoolValue *CPV =
6523 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6524 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6525
Craig Topperc7242e02012-04-20 07:30:17 +00006526 const TargetRegisterClass *TRC = isThumb ?
6527 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6528 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00006529
Bill Wendling030b58e2011-10-06 22:18:16 +00006530 // Grab constant pool and fixed stack memory operands.
6531 MachineMemOperand *CPMMO =
6532 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6533 MachineMemOperand::MOLoad, 4, 4);
6534
6535 MachineMemOperand *FIMMOSt =
6536 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6537 MachineMemOperand::MOStore, 4, 4);
6538
6539 // Load the address of the dispatch MBB into the jump buffer.
6540 if (isThumb2) {
6541 // Incoming value: jbuf
6542 // ldr.n r5, LCPI1_1
6543 // orr r5, r5, #1
6544 // add r5, pc
6545 // str r5, [$jbuf, #+4] ; &jbuf[1]
6546 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6547 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6548 .addConstantPoolIndex(CPI)
6549 .addMemOperand(CPMMO));
6550 // Set the low bit because of thumb mode.
6551 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6552 AddDefaultCC(
6553 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6554 .addReg(NewVReg1, RegState::Kill)
6555 .addImm(0x01)));
6556 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6557 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6558 .addReg(NewVReg2, RegState::Kill)
6559 .addImm(PCLabelId);
6560 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6561 .addReg(NewVReg3, RegState::Kill)
6562 .addFrameIndex(FI)
6563 .addImm(36) // &jbuf[1] :: pc
6564 .addMemOperand(FIMMOSt));
6565 } else if (isThumb) {
6566 // Incoming value: jbuf
6567 // ldr.n r1, LCPI1_4
6568 // add r1, pc
6569 // mov r2, #1
6570 // orrs r1, r2
6571 // add r2, $jbuf, #+4 ; &jbuf[1]
6572 // str r1, [r2]
6573 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6574 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6575 .addConstantPoolIndex(CPI)
6576 .addMemOperand(CPMMO));
6577 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6578 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6579 .addReg(NewVReg1, RegState::Kill)
6580 .addImm(PCLabelId);
6581 // Set the low bit because of thumb mode.
6582 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6583 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6584 .addReg(ARM::CPSR, RegState::Define)
6585 .addImm(1));
6586 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6587 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6588 .addReg(ARM::CPSR, RegState::Define)
6589 .addReg(NewVReg2, RegState::Kill)
6590 .addReg(NewVReg3, RegState::Kill));
6591 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Tim Northover23075cc2014-10-20 21:28:41 +00006592 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5)
6593 .addFrameIndex(FI)
6594 .addImm(36); // &jbuf[1] :: pc
Bill Wendling030b58e2011-10-06 22:18:16 +00006595 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6596 .addReg(NewVReg4, RegState::Kill)
6597 .addReg(NewVReg5, RegState::Kill)
6598 .addImm(0)
6599 .addMemOperand(FIMMOSt));
6600 } else {
6601 // Incoming value: jbuf
6602 // ldr r1, LCPI1_1
6603 // add r1, pc, r1
6604 // str r1, [$jbuf, #+4] ; &jbuf[1]
6605 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6606 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6607 .addConstantPoolIndex(CPI)
6608 .addImm(0)
6609 .addMemOperand(CPMMO));
6610 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6611 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6612 .addReg(NewVReg1, RegState::Kill)
6613 .addImm(PCLabelId));
6614 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6615 .addReg(NewVReg2, RegState::Kill)
6616 .addFrameIndex(FI)
6617 .addImm(36) // &jbuf[1] :: pc
6618 .addMemOperand(FIMMOSt));
6619 }
6620}
6621
6622MachineBasicBlock *ARMTargetLowering::
6623EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
Eric Christopherd9134482014-08-04 21:25:23 +00006624 const TargetInstrInfo *TII =
6625 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00006626 DebugLoc dl = MI->getDebugLoc();
6627 MachineFunction *MF = MBB->getParent();
6628 MachineRegisterInfo *MRI = &MF->getRegInfo();
6629 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6630 MachineFrameInfo *MFI = MF->getFrameInfo();
6631 int FI = MFI->getFunctionContextIndex();
6632
Craig Topperc7242e02012-04-20 07:30:17 +00006633 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
6634 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen691ae332012-05-20 06:38:47 +00006635 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00006636
Bill Wendling362c1b02011-10-06 21:29:56 +00006637 // Get a mapping of the call site numbers to all of the landing pads they're
6638 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00006639 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6640 unsigned MaxCSNum = 0;
6641 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00006642 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6643 ++BB) {
Bill Wendling202803e2011-10-05 00:02:33 +00006644 if (!BB->isLandingPad()) continue;
6645
6646 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6647 // pad.
6648 for (MachineBasicBlock::iterator
6649 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6650 if (!II->isEHLabel()) continue;
6651
6652 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006653 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00006654
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006655 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6656 for (SmallVectorImpl<unsigned>::iterator
6657 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6658 CSI != CSE; ++CSI) {
6659 CallSiteNumToLPad[*CSI].push_back(BB);
6660 MaxCSNum = std::max(MaxCSNum, *CSI);
6661 }
Bill Wendling202803e2011-10-05 00:02:33 +00006662 break;
6663 }
6664 }
6665
6666 // Get an ordered list of the machine basic blocks for the jump table.
6667 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling883ec972011-10-07 23:18:02 +00006668 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00006669 LPadList.reserve(CallSiteNumToLPad.size());
6670 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6671 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6672 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006673 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00006674 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00006675 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6676 }
Bill Wendling202803e2011-10-05 00:02:33 +00006677 }
6678
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006679 assert(!LPadList.empty() &&
6680 "No landing pad destinations for the dispatch jump table!");
6681
Bill Wendling362c1b02011-10-06 21:29:56 +00006682 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00006683 MachineJumpTableInfo *JTI =
6684 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6685 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
6686 unsigned UId = AFI->createJumpTableUId();
Chad Rosier96603432013-03-01 18:30:38 +00006687 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00006688
Bill Wendling362c1b02011-10-06 21:29:56 +00006689 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00006690
6691 // Shove the dispatch's address into the return slot in the function context.
6692 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6693 DispatchBB->setIsLandingPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00006694
Bill Wendling324be982011-10-05 00:39:32 +00006695 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006696 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00006697 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006698 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00006699 else
6700 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
6701
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006702 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00006703 DispatchBB->addSuccessor(TrapBB);
6704
6705 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6706 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00006707
Bill Wendling510fbcd2011-10-17 21:32:56 +00006708 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00006709 MF->insert(MF->end(), DispatchBB);
6710 MF->insert(MF->end(), DispContBB);
6711 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00006712
Bill Wendling030b58e2011-10-06 22:18:16 +00006713 // Insert code into the entry block that creates and registers the function
6714 // context.
6715 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6716
Bill Wendling030b58e2011-10-06 22:18:16 +00006717 MachineMemOperand *FIMMOLd =
Bill Wendling362c1b02011-10-06 21:29:56 +00006718 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendlingb3d46782011-10-06 23:37:36 +00006719 MachineMemOperand::MOLoad |
6720 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00006721
Chad Rosier1ec8e402012-11-06 23:05:24 +00006722 MachineInstrBuilder MIB;
6723 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6724
6725 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6726 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6727
6728 // Add a register mask with no preserved registers. This results in all
6729 // registers being marked as clobbered.
6730 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00006731
Bill Wendling85833f72011-10-18 22:49:07 +00006732 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00006733 if (Subtarget->isThumb2()) {
6734 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6735 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6736 .addFrameIndex(FI)
6737 .addImm(4)
6738 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006739
Bill Wendling85833f72011-10-18 22:49:07 +00006740 if (NumLPads < 256) {
6741 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6742 .addReg(NewVReg1)
6743 .addImm(LPadList.size()));
6744 } else {
6745 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6746 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006747 .addImm(NumLPads & 0xFFFF));
6748
6749 unsigned VReg2 = VReg1;
6750 if ((NumLPads & 0xFFFF0000) != 0) {
6751 VReg2 = MRI->createVirtualRegister(TRC);
6752 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6753 .addReg(VReg1)
6754 .addImm(NumLPads >> 16));
6755 }
6756
Bill Wendling85833f72011-10-18 22:49:07 +00006757 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6758 .addReg(NewVReg1)
6759 .addReg(VReg2));
6760 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006761
Bill Wendling5626c662011-10-06 22:53:00 +00006762 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6763 .addMBB(TrapBB)
6764 .addImm(ARMCC::HI)
6765 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00006766
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006767 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6768 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006769 .addJumpTableIndex(MJTI)
6770 .addImm(UId));
Bill Wendling202803e2011-10-05 00:02:33 +00006771
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006772 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006773 AddDefaultCC(
6774 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006775 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6776 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00006777 .addReg(NewVReg1)
6778 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6779
6780 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006781 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00006782 .addReg(NewVReg1)
Bill Wendling5626c662011-10-06 22:53:00 +00006783 .addJumpTableIndex(MJTI)
6784 .addImm(UId);
6785 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00006786 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6787 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6788 .addFrameIndex(FI)
6789 .addImm(1)
6790 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00006791
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006792 if (NumLPads < 256) {
6793 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6794 .addReg(NewVReg1)
6795 .addImm(NumLPads));
6796 } else {
6797 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00006798 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6799 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6800
6801 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006802 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006803 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006804 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006805 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006806
6807 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6808 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6809 .addReg(VReg1, RegState::Define)
6810 .addConstantPoolIndex(Idx));
6811 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6812 .addReg(NewVReg1)
6813 .addReg(VReg1));
6814 }
6815
Bill Wendlingb3d46782011-10-06 23:37:36 +00006816 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6817 .addMBB(TrapBB)
6818 .addImm(ARMCC::HI)
6819 .addReg(ARM::CPSR);
6820
6821 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6822 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6823 .addReg(ARM::CPSR, RegState::Define)
6824 .addReg(NewVReg1)
6825 .addImm(2));
6826
6827 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00006828 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendlingb3d46782011-10-06 23:37:36 +00006829 .addJumpTableIndex(MJTI)
6830 .addImm(UId));
6831
6832 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6833 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6834 .addReg(ARM::CPSR, RegState::Define)
6835 .addReg(NewVReg2, RegState::Kill)
6836 .addReg(NewVReg3));
6837
6838 MachineMemOperand *JTMMOLd =
6839 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6840 MachineMemOperand::MOLoad, 4, 4);
6841
6842 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6843 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6844 .addReg(NewVReg4, RegState::Kill)
6845 .addImm(0)
6846 .addMemOperand(JTMMOLd));
6847
Chad Rosier96603432013-03-01 18:30:38 +00006848 unsigned NewVReg6 = NewVReg5;
6849 if (RelocM == Reloc::PIC_) {
6850 NewVReg6 = MRI->createVirtualRegister(TRC);
6851 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6852 .addReg(ARM::CPSR, RegState::Define)
6853 .addReg(NewVReg5, RegState::Kill)
6854 .addReg(NewVReg3));
6855 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00006856
6857 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6858 .addReg(NewVReg6, RegState::Kill)
6859 .addJumpTableIndex(MJTI)
6860 .addImm(UId);
Bill Wendling5626c662011-10-06 22:53:00 +00006861 } else {
6862 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6863 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6864 .addFrameIndex(FI)
6865 .addImm(4)
6866 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00006867
Bill Wendling4969dcd2011-10-18 22:52:20 +00006868 if (NumLPads < 256) {
6869 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6870 .addReg(NewVReg1)
6871 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00006872 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00006873 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6874 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006875 .addImm(NumLPads & 0xFFFF));
6876
6877 unsigned VReg2 = VReg1;
6878 if ((NumLPads & 0xFFFF0000) != 0) {
6879 VReg2 = MRI->createVirtualRegister(TRC);
6880 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6881 .addReg(VReg1)
6882 .addImm(NumLPads >> 16));
6883 }
6884
Bill Wendling4969dcd2011-10-18 22:52:20 +00006885 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6886 .addReg(NewVReg1)
6887 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00006888 } else {
6889 MachineConstantPool *ConstantPool = MF->getConstantPool();
6890 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6891 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6892
6893 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006894 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006895 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006896 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006897 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6898
6899 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6900 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6901 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00006902 .addConstantPoolIndex(Idx)
6903 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00006904 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6905 .addReg(NewVReg1)
6906 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00006907 }
6908
Bill Wendling5626c662011-10-06 22:53:00 +00006909 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6910 .addMBB(TrapBB)
6911 .addImm(ARMCC::HI)
6912 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00006913
Bill Wendling973c8172011-10-18 22:11:18 +00006914 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006915 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00006916 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006917 .addReg(NewVReg1)
6918 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00006919 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6920 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006921 .addJumpTableIndex(MJTI)
6922 .addImm(UId));
6923
6924 MachineMemOperand *JTMMOLd =
6925 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6926 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00006927 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006928 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00006929 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6930 .addReg(NewVReg3, RegState::Kill)
6931 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006932 .addImm(0)
6933 .addMemOperand(JTMMOLd));
6934
Chad Rosier96603432013-03-01 18:30:38 +00006935 if (RelocM == Reloc::PIC_) {
6936 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
6937 .addReg(NewVReg5, RegState::Kill)
6938 .addReg(NewVReg4)
6939 .addJumpTableIndex(MJTI)
6940 .addImm(UId);
6941 } else {
6942 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
6943 .addReg(NewVReg5, RegState::Kill)
6944 .addJumpTableIndex(MJTI)
6945 .addImm(UId);
6946 }
Bill Wendling5626c662011-10-06 22:53:00 +00006947 }
Bill Wendling202803e2011-10-05 00:02:33 +00006948
Bill Wendling324be982011-10-05 00:39:32 +00006949 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006950 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00006951 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006952 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6953 MachineBasicBlock *CurMBB = *I;
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006954 if (SeenMBBs.insert(CurMBB))
Bill Wendling883ec972011-10-07 23:18:02 +00006955 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006956 }
6957
Bill Wendling26d27802011-10-17 05:25:09 +00006958 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00006959 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00006960 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Craig Topper46276792014-08-24 23:23:06 +00006961 for (MachineBasicBlock *BB : InvokeBBs) {
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006962
6963 // Remove the landing pad successor from the invoke block and replace it
6964 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00006965 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6966 BB->succ_end());
6967 while (!Successors.empty()) {
6968 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling883ec972011-10-07 23:18:02 +00006969 if (SMBB->isLandingPad()) {
6970 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00006971 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006972 }
6973 }
6974
6975 BB->addSuccessor(DispatchBB);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006976
6977 // Find the invoke call and mark all of the callee-saved registers as
6978 // 'implicit defined' so that they're spilled. This prevents code from
6979 // moving instructions to before the EH block, where they will never be
6980 // executed.
6981 for (MachineBasicBlock::reverse_iterator
6982 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00006983 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006984
6985 DenseMap<unsigned, bool> DefRegs;
6986 for (MachineInstr::mop_iterator
6987 OI = II->operands_begin(), OE = II->operands_end();
6988 OI != OE; ++OI) {
6989 if (!OI->isReg()) continue;
6990 DefRegs[OI->getReg()] = true;
6991 }
6992
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00006993 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006994
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006995 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00006996 unsigned Reg = SavedRegs[i];
6997 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00006998 !ARM::tGPRRegClass.contains(Reg) &&
6999 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007000 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007001 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007002 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007003 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007004 continue;
7005 if (!DefRegs[Reg])
7006 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007007 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007008
7009 break;
7010 }
Bill Wendling883ec972011-10-07 23:18:02 +00007011 }
Bill Wendling324be982011-10-05 00:39:32 +00007012
Bill Wendling617075f2011-10-18 18:30:49 +00007013 // Mark all former landing pads as non-landing pads. The dispatch is the only
7014 // landing pad now.
7015 for (SmallVectorImpl<MachineBasicBlock*>::iterator
7016 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
7017 (*I)->setIsLandingPad(false);
7018
Bill Wendling324be982011-10-05 00:39:32 +00007019 // The instruction is gone now.
7020 MI->eraseFromParent();
7021
Bill Wendling374ee192011-10-03 21:25:38 +00007022 return MBB;
7023}
7024
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007025static
7026MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
7027 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
7028 E = MBB->succ_end(); I != E; ++I)
7029 if (*I != Succ)
7030 return *I;
7031 llvm_unreachable("Expecting a BB with two successors!");
7032}
7033
Manman Renb504f492013-10-29 22:27:32 +00007034/// Return the load opcode for a given load size. If load size >= 8,
7035/// neon opcode will be returned.
7036static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
7037 if (LdSize >= 8)
7038 return LdSize == 16 ? ARM::VLD1q32wb_fixed
7039 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
7040 if (IsThumb1)
7041 return LdSize == 4 ? ARM::tLDRi
7042 : LdSize == 2 ? ARM::tLDRHi
7043 : LdSize == 1 ? ARM::tLDRBi : 0;
7044 if (IsThumb2)
7045 return LdSize == 4 ? ARM::t2LDR_POST
7046 : LdSize == 2 ? ARM::t2LDRH_POST
7047 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
7048 return LdSize == 4 ? ARM::LDR_POST_IMM
7049 : LdSize == 2 ? ARM::LDRH_POST
7050 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
7051}
7052
7053/// Return the store opcode for a given store size. If store size >= 8,
7054/// neon opcode will be returned.
7055static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
7056 if (StSize >= 8)
7057 return StSize == 16 ? ARM::VST1q32wb_fixed
7058 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
7059 if (IsThumb1)
7060 return StSize == 4 ? ARM::tSTRi
7061 : StSize == 2 ? ARM::tSTRHi
7062 : StSize == 1 ? ARM::tSTRBi : 0;
7063 if (IsThumb2)
7064 return StSize == 4 ? ARM::t2STR_POST
7065 : StSize == 2 ? ARM::t2STRH_POST
7066 : StSize == 1 ? ARM::t2STRB_POST : 0;
7067 return StSize == 4 ? ARM::STR_POST_IMM
7068 : StSize == 2 ? ARM::STRH_POST
7069 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
7070}
7071
7072/// Emit a post-increment load operation with given size. The instructions
7073/// will be added to BB at Pos.
7074static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
7075 const TargetInstrInfo *TII, DebugLoc dl,
7076 unsigned LdSize, unsigned Data, unsigned AddrIn,
7077 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7078 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
7079 assert(LdOpc != 0 && "Should have a load opcode");
7080 if (LdSize >= 8) {
7081 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7082 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7083 .addImm(0));
7084 } else if (IsThumb1) {
7085 // load + update AddrIn
7086 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7087 .addReg(AddrIn).addImm(0));
7088 MachineInstrBuilder MIB =
7089 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7090 MIB = AddDefaultT1CC(MIB);
7091 MIB.addReg(AddrIn).addImm(LdSize);
7092 AddDefaultPred(MIB);
7093 } else if (IsThumb2) {
7094 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7095 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7096 .addImm(LdSize));
7097 } else { // arm
7098 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7099 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7100 .addReg(0).addImm(LdSize));
7101 }
7102}
7103
7104/// Emit a post-increment store operation with given size. The instructions
7105/// will be added to BB at Pos.
7106static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
7107 const TargetInstrInfo *TII, DebugLoc dl,
7108 unsigned StSize, unsigned Data, unsigned AddrIn,
7109 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7110 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
7111 assert(StOpc != 0 && "Should have a store opcode");
7112 if (StSize >= 8) {
7113 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7114 .addReg(AddrIn).addImm(0).addReg(Data));
7115 } else if (IsThumb1) {
7116 // store + update AddrIn
7117 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
7118 .addReg(AddrIn).addImm(0));
7119 MachineInstrBuilder MIB =
7120 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7121 MIB = AddDefaultT1CC(MIB);
7122 MIB.addReg(AddrIn).addImm(StSize);
7123 AddDefaultPred(MIB);
7124 } else if (IsThumb2) {
7125 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7126 .addReg(Data).addReg(AddrIn).addImm(StSize));
7127 } else { // arm
7128 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7129 .addReg(Data).addReg(AddrIn).addReg(0)
7130 .addImm(StSize));
7131 }
7132}
7133
David Peixottoc32e24a2013-10-17 19:49:22 +00007134MachineBasicBlock *
7135ARMTargetLowering::EmitStructByval(MachineInstr *MI,
7136 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00007137 // This pseudo instruction has 3 operands: dst, src, size
7138 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
7139 // Otherwise, we will generate unrolled scalar copies.
Eric Christopherd9134482014-08-04 21:25:23 +00007140 const TargetInstrInfo *TII =
7141 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Manman Rene8735522012-06-01 19:33:18 +00007142 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7143 MachineFunction::iterator It = BB;
7144 ++It;
7145
7146 unsigned dest = MI->getOperand(0).getReg();
7147 unsigned src = MI->getOperand(1).getReg();
7148 unsigned SizeVal = MI->getOperand(2).getImm();
7149 unsigned Align = MI->getOperand(3).getImm();
7150 DebugLoc dl = MI->getDebugLoc();
7151
Manman Rene8735522012-06-01 19:33:18 +00007152 MachineFunction *MF = BB->getParent();
7153 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00007154 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00007155 const TargetRegisterClass *TRC = nullptr;
7156 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007157
7158 bool IsThumb1 = Subtarget->isThumb1Only();
7159 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00007160
7161 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00007162 UnitSize = 1;
7163 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00007164 UnitSize = 2;
7165 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00007166 // Check whether we can use NEON instructions.
Bill Wendling698e84f2012-12-30 10:32:01 +00007167 if (!MF->getFunction()->getAttributes().
7168 hasAttribute(AttributeSet::FunctionIndex,
7169 Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00007170 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00007171 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00007172 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00007173 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00007174 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00007175 }
7176 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00007177 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00007178 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00007179 }
Manman Ren6e1fd462012-06-18 22:23:48 +00007180
David Peixottob0653e532013-10-24 16:39:36 +00007181 // Select the correct opcode and register class for unit size load/store
7182 bool IsNeon = UnitSize >= 8;
7183 TRC = (IsThumb1 || IsThumb2) ? (const TargetRegisterClass *)&ARM::tGPRRegClass
7184 : (const TargetRegisterClass *)&ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00007185 if (IsNeon)
David Peixottob0653e532013-10-24 16:39:36 +00007186 VecTRC = UnitSize == 16
7187 ? (const TargetRegisterClass *)&ARM::DPairRegClass
7188 : UnitSize == 8
7189 ? (const TargetRegisterClass *)&ARM::DPRRegClass
Craig Topper062a2ba2014-04-25 05:30:21 +00007190 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007191
Manman Rene8735522012-06-01 19:33:18 +00007192 unsigned BytesLeft = SizeVal % UnitSize;
7193 unsigned LoopSize = SizeVal - BytesLeft;
7194
7195 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
7196 // Use LDR and STR to copy.
7197 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
7198 // [destOut] = STR_POST(scratch, destIn, UnitSize)
7199 unsigned srcIn = src;
7200 unsigned destIn = dest;
7201 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00007202 unsigned srcOut = MRI.createVirtualRegister(TRC);
7203 unsigned destOut = MRI.createVirtualRegister(TRC);
7204 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007205 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
7206 IsThumb1, IsThumb2);
7207 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
7208 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007209 srcIn = srcOut;
7210 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007211 }
7212
7213 // Handle the leftover bytes with LDRB and STRB.
7214 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
7215 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00007216 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007217 unsigned srcOut = MRI.createVirtualRegister(TRC);
7218 unsigned destOut = MRI.createVirtualRegister(TRC);
7219 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007220 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
7221 IsThumb1, IsThumb2);
7222 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
7223 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007224 srcIn = srcOut;
7225 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007226 }
7227 MI->eraseFromParent(); // The instruction is gone now.
7228 return BB;
7229 }
7230
7231 // Expand the pseudo op to a loop.
7232 // thisMBB:
7233 // ...
7234 // movw varEnd, # --> with thumb2
7235 // movt varEnd, #
7236 // ldrcp varEnd, idx --> without thumb2
7237 // fallthrough --> loopMBB
7238 // loopMBB:
7239 // PHI varPhi, varEnd, varLoop
7240 // PHI srcPhi, src, srcLoop
7241 // PHI destPhi, dst, destLoop
7242 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7243 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
7244 // subs varLoop, varPhi, #UnitSize
7245 // bne loopMBB
7246 // fallthrough --> exitMBB
7247 // exitMBB:
7248 // epilogue to handle left-over bytes
7249 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7250 // [destOut] = STRB_POST(scratch, destLoop, 1)
7251 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7252 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7253 MF->insert(It, loopMBB);
7254 MF->insert(It, exitMBB);
7255
7256 // Transfer the remainder of BB and its successor edges to exitMBB.
7257 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007258 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007259 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7260
7261 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007262 unsigned varEnd = MRI.createVirtualRegister(TRC);
7263 if (IsThumb2) {
7264 unsigned Vtmp = varEnd;
7265 if ((LoopSize & 0xFFFF0000) != 0)
7266 Vtmp = MRI.createVirtualRegister(TRC);
7267 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), Vtmp)
7268 .addImm(LoopSize & 0xFFFF));
7269
7270 if ((LoopSize & 0xFFFF0000) != 0)
7271 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
7272 .addReg(Vtmp).addImm(LoopSize >> 16));
7273 } else {
7274 MachineConstantPool *ConstantPool = MF->getConstantPool();
7275 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7276 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7277
7278 // MachineConstantPool wants an explicit alignment.
7279 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
7280 if (Align == 0)
7281 Align = getDataLayout()->getTypeAllocSize(C->getType());
7282 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7283
7284 if (IsThumb1)
7285 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7286 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7287 else
7288 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7289 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7290 }
Manman Rene8735522012-06-01 19:33:18 +00007291 BB->addSuccessor(loopMBB);
7292
7293 // Generate the loop body:
7294 // varPhi = PHI(varLoop, varEnd)
7295 // srcPhi = PHI(srcLoop, src)
7296 // destPhi = PHI(destLoop, dst)
7297 MachineBasicBlock *entryBB = BB;
7298 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007299 unsigned varLoop = MRI.createVirtualRegister(TRC);
7300 unsigned varPhi = MRI.createVirtualRegister(TRC);
7301 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7302 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7303 unsigned destLoop = MRI.createVirtualRegister(TRC);
7304 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007305
7306 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7307 .addReg(varLoop).addMBB(loopMBB)
7308 .addReg(varEnd).addMBB(entryBB);
7309 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7310 .addReg(srcLoop).addMBB(loopMBB)
7311 .addReg(src).addMBB(entryBB);
7312 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7313 .addReg(destLoop).addMBB(loopMBB)
7314 .addReg(dest).addMBB(entryBB);
7315
7316 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7317 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007318 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007319 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7320 IsThumb1, IsThumb2);
7321 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7322 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007323
7324 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007325 if (IsThumb1) {
7326 MachineInstrBuilder MIB =
7327 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7328 MIB = AddDefaultT1CC(MIB);
7329 MIB.addReg(varPhi).addImm(UnitSize);
7330 AddDefaultPred(MIB);
7331 } else {
7332 MachineInstrBuilder MIB =
7333 BuildMI(*BB, BB->end(), dl,
7334 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7335 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7336 MIB->getOperand(5).setReg(ARM::CPSR);
7337 MIB->getOperand(5).setIsDef(true);
7338 }
7339 BuildMI(*BB, BB->end(), dl,
7340 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7341 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007342
7343 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7344 BB->addSuccessor(loopMBB);
7345 BB->addSuccessor(exitMBB);
7346
7347 // Add epilogue to handle BytesLeft.
7348 BB = exitMBB;
7349 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007350
7351 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7352 // [destOut] = STRB_POST(scratch, destLoop, 1)
7353 unsigned srcIn = srcLoop;
7354 unsigned destIn = destLoop;
7355 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007356 unsigned srcOut = MRI.createVirtualRegister(TRC);
7357 unsigned destOut = MRI.createVirtualRegister(TRC);
7358 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007359 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7360 IsThumb1, IsThumb2);
7361 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7362 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007363 srcIn = srcOut;
7364 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007365 }
7366
7367 MI->eraseFromParent(); // The instruction is gone now.
7368 return BB;
7369}
7370
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00007371MachineBasicBlock *
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007372ARMTargetLowering::EmitLowered__chkstk(MachineInstr *MI,
7373 MachineBasicBlock *MBB) const {
7374 const TargetMachine &TM = getTargetMachine();
Eric Christopherd9134482014-08-04 21:25:23 +00007375 const TargetInstrInfo &TII = *TM.getSubtargetImpl()->getInstrInfo();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007376 DebugLoc DL = MI->getDebugLoc();
7377
7378 assert(Subtarget->isTargetWindows() &&
7379 "__chkstk is only supported on Windows");
7380 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
7381
7382 // __chkstk takes the number of words to allocate on the stack in R4, and
7383 // returns the stack adjustment in number of bytes in R4. This will not
7384 // clober any other registers (other than the obvious lr).
7385 //
7386 // Although, technically, IP should be considered a register which may be
7387 // clobbered, the call itself will not touch it. Windows on ARM is a pure
7388 // thumb-2 environment, so there is no interworking required. As a result, we
7389 // do not expect a veneer to be emitted by the linker, clobbering IP.
7390 //
Alp Toker1d099d92014-06-19 19:41:26 +00007391 // Each module receives its own copy of __chkstk, so no import thunk is
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007392 // required, again, ensuring that IP is not clobbered.
7393 //
7394 // Finally, although some linkers may theoretically provide a trampoline for
7395 // out of range calls (which is quite common due to a 32M range limitation of
7396 // branches for Thumb), we can generate the long-call version via
7397 // -mcmodel=large, alleviating the need for the trampoline which may clobber
7398 // IP.
7399
7400 switch (TM.getCodeModel()) {
7401 case CodeModel::Small:
7402 case CodeModel::Medium:
7403 case CodeModel::Default:
7404 case CodeModel::Kernel:
7405 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
7406 .addImm((unsigned)ARMCC::AL).addReg(0)
7407 .addExternalSymbol("__chkstk")
7408 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7409 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7410 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7411 break;
7412 case CodeModel::Large:
7413 case CodeModel::JITDefault: {
7414 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
7415 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
7416
7417 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
7418 .addExternalSymbol("__chkstk");
7419 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
7420 .addImm((unsigned)ARMCC::AL).addReg(0)
7421 .addReg(Reg, RegState::Kill)
7422 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7423 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7424 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7425 break;
7426 }
7427 }
7428
7429 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr),
7430 ARM::SP)
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +00007431 .addReg(ARM::SP).addReg(ARM::R4)));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007432
7433 MI->eraseFromParent();
7434 return MBB;
7435}
7436
7437MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00007438ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00007439 MachineBasicBlock *BB) const {
Eric Christopherd9134482014-08-04 21:25:23 +00007440 const TargetInstrInfo *TII =
7441 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00007442 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00007443 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00007444 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00007445 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00007446 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00007447 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00007448 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00007449 // The Thumb2 pre-indexed stores have the same MI operands, they just
7450 // define them differently in the .td files from the isel patterns, so
7451 // they need pseudos.
7452 case ARM::t2STR_preidx:
7453 MI->setDesc(TII->get(ARM::t2STR_PRE));
7454 return BB;
7455 case ARM::t2STRB_preidx:
7456 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7457 return BB;
7458 case ARM::t2STRH_preidx:
7459 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7460 return BB;
7461
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007462 case ARM::STRi_preidx:
7463 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00007464 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007465 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7466 // Decode the offset.
7467 unsigned Offset = MI->getOperand(4).getImm();
7468 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7469 Offset = ARM_AM::getAM2Offset(Offset);
7470 if (isSub)
7471 Offset = -Offset;
7472
Jim Grosbachf402f692011-08-12 21:02:34 +00007473 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00007474 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007475 .addOperand(MI->getOperand(0)) // Rn_wb
7476 .addOperand(MI->getOperand(1)) // Rt
7477 .addOperand(MI->getOperand(2)) // Rn
7478 .addImm(Offset) // offset (skip GPR==zero_reg)
7479 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00007480 .addOperand(MI->getOperand(6))
7481 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007482 MI->eraseFromParent();
7483 return BB;
7484 }
7485 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00007486 case ARM::STRBr_preidx:
7487 case ARM::STRH_preidx: {
7488 unsigned NewOpc;
7489 switch (MI->getOpcode()) {
7490 default: llvm_unreachable("unexpected opcode!");
7491 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7492 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7493 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7494 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007495 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7496 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7497 MIB.addOperand(MI->getOperand(i));
7498 MI->eraseFromParent();
7499 return BB;
7500 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00007501
Evan Chengbb2af352009-08-12 05:17:19 +00007502 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00007503 // To "insert" a SELECT_CC instruction, we actually have to insert the
7504 // diamond control-flow pattern. The incoming instruction knows the
7505 // destination vreg to set, the condition code register to branch on, the
7506 // true/false values to select between, and a branch opcode to use.
7507 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00007508 MachineFunction::iterator It = BB;
Evan Cheng10043e22007-01-19 07:51:42 +00007509 ++It;
7510
7511 // thisMBB:
7512 // ...
7513 // TrueVal = ...
7514 // cmpTY ccX, r1, r2
7515 // bCC copy1MBB
7516 // fallthrough --> copy0MBB
7517 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00007518 MachineFunction *F = BB->getParent();
7519 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7520 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00007521 F->insert(It, copy0MBB);
7522 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007523
7524 // Transfer the remainder of BB and its successor edges to sinkMBB.
7525 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007526 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007527 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7528
Dan Gohmanf4f04102010-07-06 15:49:48 +00007529 BB->addSuccessor(copy0MBB);
7530 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00007531
Dan Gohman34396292010-07-06 20:24:04 +00007532 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7533 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7534
Evan Cheng10043e22007-01-19 07:51:42 +00007535 // copy0MBB:
7536 // %FalseValue = ...
7537 // # fallthrough to sinkMBB
7538 BB = copy0MBB;
7539
7540 // Update machine-CFG edges
7541 BB->addSuccessor(sinkMBB);
7542
7543 // sinkMBB:
7544 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7545 // ...
7546 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007547 BuildMI(*BB, BB->begin(), dl,
7548 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00007549 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7550 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7551
Dan Gohman34396292010-07-06 20:24:04 +00007552 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00007553 return BB;
7554 }
Evan Chengb972e562009-08-07 00:34:42 +00007555
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007556 case ARM::BCCi64:
7557 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00007558 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007559 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00007560
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007561 // Compare both parts that make up the double comparison separately for
7562 // equality.
7563 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7564
7565 unsigned LHS1 = MI->getOperand(1).getReg();
7566 unsigned LHS2 = MI->getOperand(2).getReg();
7567 if (RHSisZero) {
7568 AddDefaultPred(BuildMI(BB, dl,
7569 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7570 .addReg(LHS1).addImm(0));
7571 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7572 .addReg(LHS2).addImm(0)
7573 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7574 } else {
7575 unsigned RHS1 = MI->getOperand(3).getReg();
7576 unsigned RHS2 = MI->getOperand(4).getReg();
7577 AddDefaultPred(BuildMI(BB, dl,
7578 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7579 .addReg(LHS1).addReg(RHS1));
7580 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7581 .addReg(LHS2).addReg(RHS2)
7582 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7583 }
7584
7585 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7586 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7587 if (MI->getOperand(0).getImm() == ARMCC::NE)
7588 std::swap(destMBB, exitMBB);
7589
7590 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7591 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00007592 if (isThumb2)
7593 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7594 else
7595 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007596
7597 MI->eraseFromParent(); // The pseudo instruction is gone now.
7598 return BB;
7599 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007600
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007601 case ARM::Int_eh_sjlj_setjmp:
7602 case ARM::Int_eh_sjlj_setjmp_nofp:
7603 case ARM::tInt_eh_sjlj_setjmp:
7604 case ARM::t2Int_eh_sjlj_setjmp:
7605 case ARM::t2Int_eh_sjlj_setjmp_nofp:
7606 EmitSjLjDispatchBlock(MI, BB);
7607 return BB;
7608
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007609 case ARM::ABS:
7610 case ARM::t2ABS: {
7611 // To insert an ABS instruction, we have to insert the
7612 // diamond control-flow pattern. The incoming instruction knows the
7613 // source vreg to test against 0, the destination vreg to set,
7614 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00007615 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007616 // It transforms
7617 // V1 = ABS V0
7618 // into
7619 // V2 = MOVS V0
7620 // BCC (branch to SinkBB if V0 >= 0)
7621 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00007622 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007623 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7624 MachineFunction::iterator BBI = BB;
7625 ++BBI;
7626 MachineFunction *Fn = BB->getParent();
7627 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7628 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7629 Fn->insert(BBI, RSBBB);
7630 Fn->insert(BBI, SinkBB);
7631
7632 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7633 unsigned int ABSDstReg = MI->getOperand(0).getReg();
7634 bool isThumb2 = Subtarget->isThumb2();
7635 MachineRegisterInfo &MRI = Fn->getRegInfo();
7636 // In Thumb mode S must not be specified if source register is the SP or
7637 // PC and if destination register is the SP, so restrict register class
Craig Topperc7242e02012-04-20 07:30:17 +00007638 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
7639 (const TargetRegisterClass*)&ARM::rGPRRegClass :
7640 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007641
7642 // Transfer the remainder of BB and its successor edges to sinkMBB.
7643 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007644 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007645 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7646
7647 BB->addSuccessor(RSBBB);
7648 BB->addSuccessor(SinkBB);
7649
7650 // fall through to SinkMBB
7651 RSBBB->addSuccessor(SinkBB);
7652
Manman Rene0763c72012-06-15 21:32:12 +00007653 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00007654 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00007655 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7656 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007657
7658 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00007659 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007660 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7661 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7662
7663 // insert rsbri in RSBBB
7664 // Note: BCC and rsbri will be converted into predicated rsbmi
7665 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00007666 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007667 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Rene0763c72012-06-15 21:32:12 +00007668 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007669 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7670
Andrew Trick3f07c422011-10-18 18:40:53 +00007671 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007672 // reuse ABSDstReg to not change uses of ABS instruction
7673 BuildMI(*SinkBB, SinkBB->begin(), dl,
7674 TII->get(ARM::PHI), ABSDstReg)
7675 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00007676 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007677
7678 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00007679 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007680
7681 // return last added BB
7682 return SinkBB;
7683 }
Manman Rene8735522012-06-01 19:33:18 +00007684 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00007685 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00007686 return EmitStructByval(MI, BB);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007687 case ARM::WIN__CHKSTK:
7688 return EmitLowered__chkstk(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00007689 }
7690}
7691
Evan Chenge6fba772011-08-30 19:09:48 +00007692void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7693 SDNode *Node) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007694 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00007695 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7696 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7697 // operand is still set to noreg. If needed, set the optional operand's
7698 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00007699 //
Andrew Trick88b24502011-10-18 19:18:52 +00007700 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00007701
Andrew Trick924123a2011-09-21 02:20:46 +00007702 // Rename pseudo opcodes.
7703 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7704 if (NewOpc) {
Eric Christopherd9134482014-08-04 21:25:23 +00007705 const ARMBaseInstrInfo *TII = static_cast<const ARMBaseInstrInfo *>(
7706 getTargetMachine().getSubtargetImpl()->getInstrInfo());
Andrew Trick88b24502011-10-18 19:18:52 +00007707 MCID = &TII->get(NewOpc);
7708
7709 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7710 "converted opcode should be the same except for cc_out");
7711
7712 MI->setDesc(*MCID);
7713
7714 // Add the optional cc_out operand
7715 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00007716 }
Andrew Trick88b24502011-10-18 19:18:52 +00007717 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00007718
7719 // Any ARM instruction that sets the 's' bit should specify an optional
7720 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00007721 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007722 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007723 return;
7724 }
Andrew Trick924123a2011-09-21 02:20:46 +00007725 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7726 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007727 bool definesCPSR = false;
7728 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00007729 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00007730 i != e; ++i) {
7731 const MachineOperand &MO = MI->getOperand(i);
7732 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7733 definesCPSR = true;
7734 if (MO.isDead())
7735 deadCPSR = true;
7736 MI->RemoveOperand(i);
7737 break;
Evan Chenge6fba772011-08-30 19:09:48 +00007738 }
7739 }
Andrew Trick8586e622011-09-20 03:17:40 +00007740 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00007741 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007742 return;
7743 }
7744 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00007745 if (deadCPSR) {
7746 assert(!MI->getOperand(ccOutIdx).getReg() &&
7747 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00007748 return;
Andrew Trick924123a2011-09-21 02:20:46 +00007749 }
Andrew Trick8586e622011-09-20 03:17:40 +00007750
Andrew Trick924123a2011-09-21 02:20:46 +00007751 // If this instruction was defined with an optional CPSR def and its dag node
7752 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007753 MachineOperand &MO = MI->getOperand(ccOutIdx);
7754 MO.setReg(ARM::CPSR);
7755 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00007756}
7757
Evan Cheng10043e22007-01-19 07:51:42 +00007758//===----------------------------------------------------------------------===//
7759// ARM Optimization Hooks
7760//===----------------------------------------------------------------------===//
7761
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007762// Helper function that checks if N is a null or all ones constant.
7763static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7764 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7765 if (!C)
7766 return false;
7767 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7768}
7769
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007770// Return true if N is conditionally 0 or all ones.
7771// Detects these expressions where cc is an i1 value:
7772//
7773// (select cc 0, y) [AllOnes=0]
7774// (select cc y, 0) [AllOnes=0]
7775// (zext cc) [AllOnes=0]
7776// (sext cc) [AllOnes=0/1]
7777// (select cc -1, y) [AllOnes=1]
7778// (select cc y, -1) [AllOnes=1]
7779//
7780// Invert is set when N is the null/all ones constant when CC is false.
7781// OtherOp is set to the alternative value of N.
7782static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7783 SDValue &CC, bool &Invert,
7784 SDValue &OtherOp,
7785 SelectionDAG &DAG) {
7786 switch (N->getOpcode()) {
7787 default: return false;
7788 case ISD::SELECT: {
7789 CC = N->getOperand(0);
7790 SDValue N1 = N->getOperand(1);
7791 SDValue N2 = N->getOperand(2);
7792 if (isZeroOrAllOnes(N1, AllOnes)) {
7793 Invert = false;
7794 OtherOp = N2;
7795 return true;
7796 }
7797 if (isZeroOrAllOnes(N2, AllOnes)) {
7798 Invert = true;
7799 OtherOp = N1;
7800 return true;
7801 }
7802 return false;
7803 }
7804 case ISD::ZERO_EXTEND:
7805 // (zext cc) can never be the all ones value.
7806 if (AllOnes)
7807 return false;
7808 // Fall through.
7809 case ISD::SIGN_EXTEND: {
7810 EVT VT = N->getValueType(0);
7811 CC = N->getOperand(0);
7812 if (CC.getValueType() != MVT::i1)
7813 return false;
7814 Invert = !AllOnes;
7815 if (AllOnes)
7816 // When looking for an AllOnes constant, N is an sext, and the 'other'
7817 // value is 0.
7818 OtherOp = DAG.getConstant(0, VT);
7819 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7820 // When looking for a 0 constant, N can be zext or sext.
7821 OtherOp = DAG.getConstant(1, VT);
7822 else
7823 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7824 return true;
7825 }
7826 }
7827}
7828
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007829// Combine a constant select operand into its use:
7830//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007831// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7832// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7833// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7834// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7835// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007836//
7837// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007838// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007839//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007840// Also recognize sext/zext from i1:
7841//
7842// (add (zext cc), x) -> (select cc (add x, 1), x)
7843// (add (sext cc), x) -> (select cc (add x, -1), x)
7844//
7845// These transformations eventually create predicated instructions.
7846//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007847// @param N The node to transform.
7848// @param Slct The N operand that is a select.
7849// @param OtherOp The other N operand (x above).
7850// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007851// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007852// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00007853static
7854SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007855 TargetLowering::DAGCombinerInfo &DCI,
7856 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00007857 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00007858 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007859 SDValue NonConstantVal;
7860 SDValue CCOp;
7861 bool SwapSelectOps;
7862 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7863 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007864 return SDValue();
7865
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007866 // Slct is now know to be the desired identity constant when CC is true.
7867 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007868 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007869 OtherOp, NonConstantVal);
7870 // Unless SwapSelectOps says CC should be false.
7871 if (SwapSelectOps)
7872 std::swap(TrueVal, FalseVal);
7873
Andrew Trickef9de2a2013-05-25 02:42:55 +00007874 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007875 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00007876}
7877
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007878// Attempt combineSelectAndUse on each operand of a commutative operator N.
7879static
7880SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7881 TargetLowering::DAGCombinerInfo &DCI) {
7882 SDValue N0 = N->getOperand(0);
7883 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007884 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007885 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7886 if (Result.getNode())
7887 return Result;
7888 }
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007889 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007890 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7891 if (Result.getNode())
7892 return Result;
7893 }
7894 return SDValue();
7895}
7896
Eric Christopher1b8b94192011-06-29 21:10:36 +00007897// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00007898// (only after legalization).
7899static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7900 TargetLowering::DAGCombinerInfo &DCI,
7901 const ARMSubtarget *Subtarget) {
7902
7903 // Only perform optimization if after legalize, and if NEON is available. We
7904 // also expected both operands to be BUILD_VECTORs.
7905 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7906 || N0.getOpcode() != ISD::BUILD_VECTOR
7907 || N1.getOpcode() != ISD::BUILD_VECTOR)
7908 return SDValue();
7909
7910 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7911 EVT VT = N->getValueType(0);
7912 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7913 return SDValue();
7914
7915 // Check that the vector operands are of the right form.
7916 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7917 // operands, where N is the size of the formed vector.
7918 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7919 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007920
7921 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00007922 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00007923 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00007924 SDValue Vec = N0->getOperand(0)->getOperand(0);
7925 SDNode *V = Vec.getNode();
7926 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00007927
Eric Christopher1b8b94192011-06-29 21:10:36 +00007928 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007929 // check to see if each of their operands are an EXTRACT_VECTOR with
7930 // the same vector and appropriate index.
7931 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7932 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7933 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00007934
Tanya Lattnere9e67052011-06-14 23:48:48 +00007935 SDValue ExtVec0 = N0->getOperand(i);
7936 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007937
Tanya Lattnere9e67052011-06-14 23:48:48 +00007938 // First operand is the vector, verify its the same.
7939 if (V != ExtVec0->getOperand(0).getNode() ||
7940 V != ExtVec1->getOperand(0).getNode())
7941 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00007942
Tanya Lattnere9e67052011-06-14 23:48:48 +00007943 // Second is the constant, verify its correct.
7944 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7945 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00007946
Tanya Lattnere9e67052011-06-14 23:48:48 +00007947 // For the constant, we want to see all the even or all the odd.
7948 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7949 || C1->getZExtValue() != nextIndex+1)
7950 return SDValue();
7951
7952 // Increment index.
7953 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00007954 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00007955 return SDValue();
7956 }
7957
7958 // Create VPADDL node.
7959 SelectionDAG &DAG = DCI.DAG;
7960 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00007961
7962 // Build operand list.
7963 SmallVector<SDValue, 8> Ops;
7964 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7965 TLI.getPointerTy()));
7966
7967 // Input is the vector.
7968 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007969
Tanya Lattnere9e67052011-06-14 23:48:48 +00007970 // Get widened type and narrowed type.
7971 MVT widenType;
7972 unsigned numElem = VT.getVectorNumElements();
Silviu Barangaa3106e62014-04-03 10:44:27 +00007973
7974 EVT inputLaneType = Vec.getValueType().getVectorElementType();
7975 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00007976 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7977 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7978 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7979 default:
Craig Toppere55c5562012-02-07 02:50:20 +00007980 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00007981 }
7982
Craig Topper48d114b2014-04-26 18:35:24 +00007983 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N), widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00007984 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
7985 return DAG.getNode(ExtOp, SDLoc(N), VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00007986}
7987
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007988static SDValue findMUL_LOHI(SDValue V) {
7989 if (V->getOpcode() == ISD::UMUL_LOHI ||
7990 V->getOpcode() == ISD::SMUL_LOHI)
7991 return V;
7992 return SDValue();
7993}
7994
7995static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7996 TargetLowering::DAGCombinerInfo &DCI,
7997 const ARMSubtarget *Subtarget) {
7998
7999 if (Subtarget->isThumb1Only()) return SDValue();
8000
8001 // Only perform the checks after legalize when the pattern is available.
8002 if (DCI.isBeforeLegalize()) return SDValue();
8003
8004 // Look for multiply add opportunities.
8005 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
8006 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
8007 // a glue link from the first add to the second add.
8008 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
8009 // a S/UMLAL instruction.
8010 // loAdd UMUL_LOHI
8011 // \ / :lo \ :hi
8012 // \ / \ [no multiline comment]
8013 // ADDC | hiAdd
8014 // \ :glue / /
8015 // \ / /
8016 // ADDE
8017 //
8018 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
8019 SDValue AddcOp0 = AddcNode->getOperand(0);
8020 SDValue AddcOp1 = AddcNode->getOperand(1);
8021
8022 // Check if the two operands are from the same mul_lohi node.
8023 if (AddcOp0.getNode() == AddcOp1.getNode())
8024 return SDValue();
8025
8026 assert(AddcNode->getNumValues() == 2 &&
8027 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00008028 "Expect ADDC with two result values. First: i32");
8029
8030 // Check that we have a glued ADDC node.
8031 if (AddcNode->getValueType(1) != MVT::Glue)
8032 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008033
8034 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
8035 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
8036 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
8037 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
8038 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
8039 return SDValue();
8040
8041 // Look for the glued ADDE.
8042 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00008043 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008044 return SDValue();
8045
8046 // Make sure it is really an ADDE.
8047 if (AddeNode->getOpcode() != ISD::ADDE)
8048 return SDValue();
8049
8050 assert(AddeNode->getNumOperands() == 3 &&
8051 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
8052 "ADDE node has the wrong inputs");
8053
8054 // Check for the triangle shape.
8055 SDValue AddeOp0 = AddeNode->getOperand(0);
8056 SDValue AddeOp1 = AddeNode->getOperand(1);
8057
8058 // Make sure that the ADDE operands are not coming from the same node.
8059 if (AddeOp0.getNode() == AddeOp1.getNode())
8060 return SDValue();
8061
8062 // Find the MUL_LOHI node walking up ADDE's operands.
8063 bool IsLeftOperandMUL = false;
8064 SDValue MULOp = findMUL_LOHI(AddeOp0);
8065 if (MULOp == SDValue())
8066 MULOp = findMUL_LOHI(AddeOp1);
8067 else
8068 IsLeftOperandMUL = true;
8069 if (MULOp == SDValue())
8070 return SDValue();
8071
8072 // Figure out the right opcode.
8073 unsigned Opc = MULOp->getOpcode();
8074 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
8075
8076 // Figure out the high and low input values to the MLAL node.
8077 SDValue* HiMul = &MULOp;
Craig Topper062a2ba2014-04-25 05:30:21 +00008078 SDValue* HiAdd = nullptr;
8079 SDValue* LoMul = nullptr;
8080 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008081
8082 if (IsLeftOperandMUL)
8083 HiAdd = &AddeOp1;
8084 else
8085 HiAdd = &AddeOp0;
8086
8087
8088 if (AddcOp0->getOpcode() == Opc) {
8089 LoMul = &AddcOp0;
8090 LowAdd = &AddcOp1;
8091 }
8092 if (AddcOp1->getOpcode() == Opc) {
8093 LoMul = &AddcOp1;
8094 LowAdd = &AddcOp0;
8095 }
8096
Craig Topper062a2ba2014-04-25 05:30:21 +00008097 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008098 return SDValue();
8099
8100 if (LoMul->getNode() != HiMul->getNode())
8101 return SDValue();
8102
8103 // Create the merged node.
8104 SelectionDAG &DAG = DCI.DAG;
8105
8106 // Build operand list.
8107 SmallVector<SDValue, 8> Ops;
8108 Ops.push_back(LoMul->getOperand(0));
8109 Ops.push_back(LoMul->getOperand(1));
8110 Ops.push_back(*LowAdd);
8111 Ops.push_back(*HiAdd);
8112
Andrew Trickef9de2a2013-05-25 02:42:55 +00008113 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00008114 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008115
8116 // Replace the ADDs' nodes uses by the MLA node's values.
8117 SDValue HiMLALResult(MLALNode.getNode(), 1);
8118 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
8119
8120 SDValue LoMLALResult(MLALNode.getNode(), 0);
8121 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
8122
8123 // Return original node to notify the driver to stop replacing.
8124 SDValue resNode(AddcNode, 0);
8125 return resNode;
8126}
8127
8128/// PerformADDCCombine - Target-specific dag combine transform from
8129/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
8130static SDValue PerformADDCCombine(SDNode *N,
8131 TargetLowering::DAGCombinerInfo &DCI,
8132 const ARMSubtarget *Subtarget) {
8133
8134 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
8135
8136}
8137
Bob Wilson728eb292010-07-29 20:34:14 +00008138/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
8139/// operands N0 and N1. This is a helper for PerformADDCombine that is
8140/// called with the default operands, and if that fails, with commuted
8141/// operands.
8142static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008143 TargetLowering::DAGCombinerInfo &DCI,
8144 const ARMSubtarget *Subtarget){
8145
8146 // Attempt to create vpaddl for this add.
8147 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
8148 if (Result.getNode())
8149 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008150
Chris Lattner4147f082009-03-12 06:52:53 +00008151 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008152 if (N0.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008153 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
8154 if (Result.getNode()) return Result;
8155 }
Chris Lattner4147f082009-03-12 06:52:53 +00008156 return SDValue();
8157}
8158
Bob Wilson728eb292010-07-29 20:34:14 +00008159/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
8160///
8161static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008162 TargetLowering::DAGCombinerInfo &DCI,
8163 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00008164 SDValue N0 = N->getOperand(0);
8165 SDValue N1 = N->getOperand(1);
8166
8167 // First try with the default operand order.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008168 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008169 if (Result.getNode())
8170 return Result;
8171
8172 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008173 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008174}
8175
Chris Lattner4147f082009-03-12 06:52:53 +00008176/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00008177///
Chris Lattner4147f082009-03-12 06:52:53 +00008178static SDValue PerformSUBCombine(SDNode *N,
8179 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00008180 SDValue N0 = N->getOperand(0);
8181 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00008182
Chris Lattner4147f082009-03-12 06:52:53 +00008183 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008184 if (N1.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008185 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
8186 if (Result.getNode()) return Result;
8187 }
Bob Wilson7117a912009-03-20 22:42:55 +00008188
Chris Lattner4147f082009-03-12 06:52:53 +00008189 return SDValue();
8190}
8191
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008192/// PerformVMULCombine
8193/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
8194/// special multiplier accumulator forwarding.
8195/// vmul d3, d0, d2
8196/// vmla d3, d1, d2
8197/// is faster than
8198/// vadd d3, d0, d1
8199/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00008200// However, for (A + B) * (A + B),
8201// vadd d2, d0, d1
8202// vmul d3, d0, d2
8203// vmla d3, d1, d2
8204// is slower than
8205// vadd d2, d0, d1
8206// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008207static SDValue PerformVMULCombine(SDNode *N,
8208 TargetLowering::DAGCombinerInfo &DCI,
8209 const ARMSubtarget *Subtarget) {
8210 if (!Subtarget->hasVMLxForwarding())
8211 return SDValue();
8212
8213 SelectionDAG &DAG = DCI.DAG;
8214 SDValue N0 = N->getOperand(0);
8215 SDValue N1 = N->getOperand(1);
8216 unsigned Opcode = N0.getOpcode();
8217 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8218 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00008219 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008220 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8221 Opcode != ISD::FADD && Opcode != ISD::FSUB)
8222 return SDValue();
8223 std::swap(N0, N1);
8224 }
8225
Weiming Zhao2052f482013-09-25 23:12:06 +00008226 if (N0 == N1)
8227 return SDValue();
8228
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008229 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008230 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008231 SDValue N00 = N0->getOperand(0);
8232 SDValue N01 = N0->getOperand(1);
8233 return DAG.getNode(Opcode, DL, VT,
8234 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
8235 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
8236}
8237
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008238static SDValue PerformMULCombine(SDNode *N,
8239 TargetLowering::DAGCombinerInfo &DCI,
8240 const ARMSubtarget *Subtarget) {
8241 SelectionDAG &DAG = DCI.DAG;
8242
8243 if (Subtarget->isThumb1Only())
8244 return SDValue();
8245
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008246 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8247 return SDValue();
8248
8249 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008250 if (VT.is64BitVector() || VT.is128BitVector())
8251 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008252 if (VT != MVT::i32)
8253 return SDValue();
8254
8255 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8256 if (!C)
8257 return SDValue();
8258
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008259 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008260 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008261
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008262 ShiftAmt = ShiftAmt & (32 - 1);
8263 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008264 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008265
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008266 SDValue Res;
8267 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008268
8269 if (MulAmt >= 0) {
8270 if (isPowerOf2_32(MulAmt - 1)) {
8271 // (mul x, 2^N + 1) => (add (shl x, N), x)
8272 Res = DAG.getNode(ISD::ADD, DL, VT,
8273 V,
8274 DAG.getNode(ISD::SHL, DL, VT,
8275 V,
8276 DAG.getConstant(Log2_32(MulAmt - 1),
8277 MVT::i32)));
8278 } else if (isPowerOf2_32(MulAmt + 1)) {
8279 // (mul x, 2^N - 1) => (sub (shl x, N), x)
8280 Res = DAG.getNode(ISD::SUB, DL, VT,
8281 DAG.getNode(ISD::SHL, DL, VT,
8282 V,
8283 DAG.getConstant(Log2_32(MulAmt + 1),
8284 MVT::i32)),
8285 V);
8286 } else
8287 return SDValue();
8288 } else {
8289 uint64_t MulAmtAbs = -MulAmt;
8290 if (isPowerOf2_32(MulAmtAbs + 1)) {
8291 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8292 Res = DAG.getNode(ISD::SUB, DL, VT,
8293 V,
8294 DAG.getNode(ISD::SHL, DL, VT,
8295 V,
8296 DAG.getConstant(Log2_32(MulAmtAbs + 1),
8297 MVT::i32)));
8298 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8299 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8300 Res = DAG.getNode(ISD::ADD, DL, VT,
8301 V,
8302 DAG.getNode(ISD::SHL, DL, VT,
8303 V,
8304 DAG.getConstant(Log2_32(MulAmtAbs-1),
8305 MVT::i32)));
8306 Res = DAG.getNode(ISD::SUB, DL, VT,
8307 DAG.getConstant(0, MVT::i32),Res);
8308
8309 } else
8310 return SDValue();
8311 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008312
8313 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008314 Res = DAG.getNode(ISD::SHL, DL, VT,
8315 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008316
8317 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008318 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008319 return SDValue();
8320}
8321
Owen Anderson30c48922010-11-05 19:27:46 +00008322static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00008323 TargetLowering::DAGCombinerInfo &DCI,
8324 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00008325
Owen Anderson30c48922010-11-05 19:27:46 +00008326 // Attempt to use immediate-form VBIC
8327 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008328 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00008329 EVT VT = N->getValueType(0);
8330 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008331
Tanya Lattner266792a2011-04-07 15:24:20 +00008332 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8333 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008334
Owen Anderson30c48922010-11-05 19:27:46 +00008335 APInt SplatBits, SplatUndef;
8336 unsigned SplatBitSize;
8337 bool HasAnyUndefs;
8338 if (BVN &&
8339 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8340 if (SplatBitSize <= 64) {
8341 EVT VbicVT;
8342 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8343 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00008344 DAG, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008345 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00008346 if (Val.getNode()) {
8347 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008348 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00008349 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008350 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00008351 }
8352 }
8353 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008354
Evan Chenge87681c2012-02-23 01:19:06 +00008355 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008356 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8357 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8358 if (Result.getNode())
8359 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008360 }
8361
Owen Anderson30c48922010-11-05 19:27:46 +00008362 return SDValue();
8363}
8364
Jim Grosbach11013ed2010-07-16 23:05:05 +00008365/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8366static SDValue PerformORCombine(SDNode *N,
8367 TargetLowering::DAGCombinerInfo &DCI,
8368 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008369 // Attempt to use immediate-form VORR
8370 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008371 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008372 EVT VT = N->getValueType(0);
8373 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008374
Tanya Lattner266792a2011-04-07 15:24:20 +00008375 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8376 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008377
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008378 APInt SplatBits, SplatUndef;
8379 unsigned SplatBitSize;
8380 bool HasAnyUndefs;
8381 if (BVN && Subtarget->hasNEON() &&
8382 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8383 if (SplatBitSize <= 64) {
8384 EVT VorrVT;
8385 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8386 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00008387 DAG, VorrVT, VT.is128BitVector(),
8388 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008389 if (Val.getNode()) {
8390 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008391 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008392 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008393 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008394 }
8395 }
8396 }
8397
Evan Chenge87681c2012-02-23 01:19:06 +00008398 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008399 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8400 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8401 if (Result.getNode())
8402 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008403 }
8404
Nadav Rotem3a94c542012-08-13 18:52:44 +00008405 // The code below optimizes (or (and X, Y), Z).
8406 // The AND operand needs to have a single user to make these optimizations
8407 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008408 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00008409 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008410 return SDValue();
8411 SDValue N1 = N->getOperand(1);
8412
8413 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8414 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8415 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8416 APInt SplatUndef;
8417 unsigned SplatBitSize;
8418 bool HasAnyUndefs;
8419
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008420 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008421 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008422 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8423 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008424 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008425 HasAnyUndefs) && !HasAnyUndefs) {
8426 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8427 HasAnyUndefs) && !HasAnyUndefs) {
8428 // Ensure that the bit width of the constants are the same and that
8429 // the splat arguments are logical inverses as per the pattern we
8430 // are trying to simplify.
8431 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
8432 SplatBits0 == ~SplatBits1) {
8433 // Canonicalize the vector type to make instruction selection
8434 // simpler.
8435 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8436 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8437 N0->getOperand(1),
8438 N0->getOperand(0),
8439 N1->getOperand(0));
8440 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8441 }
8442 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008443 }
8444 }
8445
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008446 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8447 // reasonable.
8448
Jim Grosbach11013ed2010-07-16 23:05:05 +00008449 // BFI is only available on V6T2+
8450 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8451 return SDValue();
8452
Andrew Trickef9de2a2013-05-25 02:42:55 +00008453 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008454 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008455 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008456 //
8457 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008458 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008459 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008460 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008461 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008462 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008463
Jim Grosbach11013ed2010-07-16 23:05:05 +00008464 if (VT != MVT::i32)
8465 return SDValue();
8466
Evan Cheng2e51bb42010-12-13 20:32:54 +00008467 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008468
Jim Grosbach11013ed2010-07-16 23:05:05 +00008469 // The value and the mask need to be constants so we can verify this is
8470 // actually a bitfield set. If the mask is 0xffff, we can do better
8471 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00008472 SDValue MaskOp = N0.getOperand(1);
8473 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8474 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008475 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008476 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008477 if (Mask == 0xffff)
8478 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008479 SDValue Res;
8480 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008481 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8482 if (N1C) {
8483 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00008484 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008485 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008486
Evan Cheng34345752010-12-11 04:11:38 +00008487 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008488 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00008489
Evan Cheng2e51bb42010-12-13 20:32:54 +00008490 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Cheng34345752010-12-11 04:11:38 +00008491 DAG.getConstant(Val, MVT::i32),
8492 DAG.getConstant(Mask, MVT::i32));
8493
8494 // Do not add new nodes to DAG combiner worklist.
8495 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008496 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00008497 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008498 } else if (N1.getOpcode() == ISD::AND) {
8499 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008500 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8501 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008502 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008503 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008504
Eric Christopherd5530962011-03-26 01:21:03 +00008505 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8506 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008507 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008508 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008509 // The pack halfword instruction works better for masks that fit it,
8510 // so use that when it's available.
8511 if (Subtarget->hasT2ExtractPack() &&
8512 (Mask == 0xffff || Mask == 0xffff0000))
8513 return SDValue();
8514 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008515 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008516 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopherd5530962011-03-26 01:21:03 +00008517 DAG.getConstant(amt, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008518 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008519 DAG.getConstant(Mask, MVT::i32));
8520 // Do not add new nodes to DAG combiner worklist.
8521 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008522 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008523 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008524 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008525 // The pack halfword instruction works better for masks that fit it,
8526 // so use that when it's available.
8527 if (Subtarget->hasT2ExtractPack() &&
8528 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8529 return SDValue();
8530 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008531 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008532 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008533 DAG.getConstant(lsb, MVT::i32));
8534 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopherd5530962011-03-26 01:21:03 +00008535 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008536 // Do not add new nodes to DAG combiner worklist.
8537 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008538 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008539 }
8540 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008541
Evan Cheng2e51bb42010-12-13 20:32:54 +00008542 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8543 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8544 ARM::isBitFieldInvertedMask(~Mask)) {
8545 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8546 // where lsb(mask) == #shamt and masked bits of B are known zero.
8547 SDValue ShAmt = N00.getOperand(1);
8548 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008549 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008550 if (ShAmtC != LSB)
8551 return SDValue();
8552
8553 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
8554 DAG.getConstant(~Mask, MVT::i32));
8555
8556 // Do not add new nodes to DAG combiner worklist.
8557 DCI.CombineTo(N, Res, false);
8558 }
8559
Jim Grosbach11013ed2010-07-16 23:05:05 +00008560 return SDValue();
8561}
8562
Evan Chenge87681c2012-02-23 01:19:06 +00008563static SDValue PerformXORCombine(SDNode *N,
8564 TargetLowering::DAGCombinerInfo &DCI,
8565 const ARMSubtarget *Subtarget) {
8566 EVT VT = N->getValueType(0);
8567 SelectionDAG &DAG = DCI.DAG;
8568
8569 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8570 return SDValue();
8571
8572 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008573 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8574 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8575 if (Result.getNode())
8576 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008577 }
8578
8579 return SDValue();
8580}
8581
Evan Cheng6d02d902011-06-15 01:12:31 +00008582/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8583/// the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00008584static SDValue PerformBFICombine(SDNode *N,
8585 TargetLowering::DAGCombinerInfo &DCI) {
8586 SDValue N1 = N->getOperand(1);
8587 if (N1.getOpcode() == ISD::AND) {
8588 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8589 if (!N11C)
8590 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008591 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008592 unsigned LSB = countTrailingZeros(~InvMask);
8593 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Evan Cheng6d02d902011-06-15 01:12:31 +00008594 unsigned Mask = (1 << Width)-1;
Evan Chengc1778132010-12-14 03:22:07 +00008595 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008596 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008597 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00008598 N->getOperand(0), N1.getOperand(0),
8599 N->getOperand(2));
8600 }
8601 return SDValue();
8602}
8603
Bob Wilson22806742010-09-22 22:09:21 +00008604/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8605/// ARMISD::VMOVRRD.
8606static SDValue PerformVMOVRRDCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00008607 TargetLowering::DAGCombinerInfo &DCI,
8608 const ARMSubtarget *Subtarget) {
Bob Wilson22806742010-09-22 22:09:21 +00008609 // vmovrrd(vmovdrr x, y) -> x,y
8610 SDValue InDouble = N->getOperand(0);
Oliver Stannard51b1d462014-08-21 12:50:31 +00008611 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP())
Bob Wilson22806742010-09-22 22:09:21 +00008612 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008613
8614 // vmovrrd(load f64) -> (load i32), (load i32)
8615 SDNode *InNode = InDouble.getNode();
8616 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8617 InNode->getValueType(0) == MVT::f64 &&
8618 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8619 !cast<LoadSDNode>(InNode)->isVolatile()) {
8620 // TODO: Should this be done for non-FrameIndex operands?
8621 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8622
8623 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008624 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008625 SDValue BasePtr = LD->getBasePtr();
8626 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8627 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008628 LD->isNonTemporal(), LD->isInvariant(),
8629 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008630
8631 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8632 DAG.getConstant(4, MVT::i32));
8633 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8634 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008635 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008636 std::min(4U, LD->getAlignment() / 2));
8637
8638 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
Christian Pirker762b2c62014-06-01 09:30:52 +00008639 if (DCI.DAG.getTargetLoweringInfo().isBigEndian())
8640 std::swap (NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008641 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008642 return Result;
8643 }
8644
Bob Wilson22806742010-09-22 22:09:21 +00008645 return SDValue();
8646}
8647
8648/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8649/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8650static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8651 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8652 SDValue Op0 = N->getOperand(0);
8653 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00008654 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008655 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008656 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008657 Op1 = Op1.getOperand(0);
8658 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8659 Op0.getNode() == Op1.getNode() &&
8660 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008661 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00008662 N->getValueType(0), Op0.getOperand(0));
8663 return SDValue();
8664}
8665
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008666/// PerformSTORECombine - Target-specific dag combine xforms for
8667/// ISD::STORE.
8668static SDValue PerformSTORECombine(SDNode *N,
8669 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008670 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008671 if (St->isVolatile())
8672 return SDValue();
8673
Andrew Trickbc325162012-07-18 18:34:24 +00008674 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosiere0e38f62012-04-09 20:32:02 +00008675 // pack all of the elements in one place. Next, store to memory in fewer
8676 // chunks.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008677 SDValue StVal = St->getValue();
Chad Rosiere0e38f62012-04-09 20:32:02 +00008678 EVT VT = StVal.getValueType();
8679 if (St->isTruncatingStore() && VT.isVector()) {
8680 SelectionDAG &DAG = DCI.DAG;
8681 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8682 EVT StVT = St->getMemoryVT();
8683 unsigned NumElems = VT.getVectorNumElements();
8684 assert(StVT != VT && "Cannot truncate to the same type");
8685 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
8686 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
8687
8688 // From, To sizes and ElemCount must be pow of two
8689 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
8690
8691 // We are going to use the original vector elt for storing.
8692 // Accumulated smaller vector elements must be a multiple of the store size.
8693 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
8694
8695 unsigned SizeRatio = FromEltSz / ToEltSz;
8696 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
8697
8698 // Create a type on which we perform the shuffle.
8699 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
8700 NumElems*SizeRatio);
8701 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
8702
Andrew Trickef9de2a2013-05-25 02:42:55 +00008703 SDLoc DL(St);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008704 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
8705 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Christian Pirker2cc1cf02014-06-16 09:17:30 +00008706 for (unsigned i = 0; i < NumElems; ++i)
8707 ShuffleVec[i] = TLI.isBigEndian() ? (i+1) * SizeRatio - 1 : i * SizeRatio;
Chad Rosiere0e38f62012-04-09 20:32:02 +00008708
8709 // Can't shuffle using an illegal type.
8710 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
8711
8712 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
8713 DAG.getUNDEF(WideVec.getValueType()),
8714 ShuffleVec.data());
8715 // At this point all of the data is stored at the bottom of the
8716 // register. We now need to save it to mem.
8717
8718 // Find the largest store unit
8719 MVT StoreType = MVT::i8;
8720 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
8721 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
8722 MVT Tp = (MVT::SimpleValueType)tp;
8723 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
8724 StoreType = Tp;
8725 }
8726 // Didn't find a legal store type.
8727 if (!TLI.isTypeLegal(StoreType))
8728 return SDValue();
8729
8730 // Bitcast the original vector into a vector of store-size units
8731 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
8732 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
8733 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
8734 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
8735 SmallVector<SDValue, 8> Chains;
8736 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
8737 TLI.getPointerTy());
8738 SDValue BasePtr = St->getBasePtr();
8739
8740 // Perform one or more big stores into memory.
8741 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
8742 for (unsigned I = 0; I < E; I++) {
8743 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
8744 StoreType, ShuffWide,
8745 DAG.getIntPtrConstant(I));
8746 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
8747 St->getPointerInfo(), St->isVolatile(),
8748 St->isNonTemporal(), St->getAlignment());
8749 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
8750 Increment);
8751 Chains.push_back(Ch);
8752 }
Craig Topper48d114b2014-04-26 18:35:24 +00008753 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008754 }
8755
8756 if (!ISD::isNormalStore(St))
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008757 return SDValue();
8758
Chad Rosier99cbde92012-04-09 19:38:15 +00008759 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
8760 // ARM stores of arguments in the same cache line.
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008761 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier99cbde92012-04-09 19:38:15 +00008762 StVal.getNode()->hasOneUse()) {
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008763 SelectionDAG &DAG = DCI.DAG;
Christian Pirkerb5728192014-05-08 14:06:24 +00008764 bool isBigEndian = DAG.getTargetLoweringInfo().isBigEndian();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008765 SDLoc DL(St);
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008766 SDValue BasePtr = St->getBasePtr();
8767 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
Christian Pirkerb5728192014-05-08 14:06:24 +00008768 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
8769 BasePtr, St->getPointerInfo(), St->isVolatile(),
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008770 St->isNonTemporal(), St->getAlignment());
8771
8772 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8773 DAG.getConstant(4, MVT::i32));
Christian Pirkerb5728192014-05-08 14:06:24 +00008774 return DAG.getStore(NewST1.getValue(0), DL,
8775 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008776 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
8777 St->isNonTemporal(),
8778 std::min(4U, St->getAlignment() / 2));
8779 }
8780
8781 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008782 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8783 return SDValue();
8784
Chad Rosier99cbde92012-04-09 19:38:15 +00008785 // Bitcast an i64 store extracted from a vector to f64.
8786 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008787 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008788 SDLoc dl(StVal);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008789 SDValue IntVec = StVal.getOperand(0);
8790 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8791 IntVec.getValueType().getVectorNumElements());
8792 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
8793 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
8794 Vec, StVal.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008795 dl = SDLoc(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008796 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
8797 // Make the DAGCombiner fold the bitcasts.
8798 DCI.AddToWorklist(Vec.getNode());
8799 DCI.AddToWorklist(ExtElt.getNode());
8800 DCI.AddToWorklist(V.getNode());
8801 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
8802 St->getPointerInfo(), St->isVolatile(),
8803 St->isNonTemporal(), St->getAlignment(),
Hal Finkelcc39b672014-07-24 12:16:19 +00008804 St->getAAInfo());
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008805}
8806
8807/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8808/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8809/// i64 vector to have f64 elements, since the value can then be loaded
8810/// directly into a VFP register.
8811static bool hasNormalLoadOperand(SDNode *N) {
8812 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8813 for (unsigned i = 0; i < NumElts; ++i) {
8814 SDNode *Elt = N->getOperand(i).getNode();
8815 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8816 return true;
8817 }
8818 return false;
8819}
8820
Bob Wilsoncb6db982010-09-17 22:59:05 +00008821/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8822/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008823static SDValue PerformBUILD_VECTORCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00008824 TargetLowering::DAGCombinerInfo &DCI,
8825 const ARMSubtarget *Subtarget) {
Bob Wilsoncb6db982010-09-17 22:59:05 +00008826 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8827 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8828 // into a pair of GPRs, which is fine when the value is used as a scalar,
8829 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008830 SelectionDAG &DAG = DCI.DAG;
8831 if (N->getNumOperands() == 2) {
8832 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8833 if (RV.getNode())
8834 return RV;
8835 }
Bob Wilsoncb6db982010-09-17 22:59:05 +00008836
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008837 // Load i64 elements as f64 values so that type legalization does not split
8838 // them up into i32 values.
8839 EVT VT = N->getValueType(0);
8840 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8841 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008842 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008843 SmallVector<SDValue, 8> Ops;
8844 unsigned NumElts = VT.getVectorNumElements();
8845 for (unsigned i = 0; i < NumElts; ++i) {
8846 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8847 Ops.push_back(V);
8848 // Make the DAGCombiner fold the bitcast.
8849 DCI.AddToWorklist(V.getNode());
8850 }
8851 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00008852 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008853 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8854}
8855
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008856/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
8857static SDValue
8858PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8859 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
8860 // At that time, we may have inserted bitcasts from integer to float.
8861 // If these bitcasts have survived DAGCombine, change the lowering of this
8862 // BUILD_VECTOR in something more vector friendly, i.e., that does not
8863 // force to use floating point types.
8864
8865 // Make sure we can change the type of the vector.
8866 // This is possible iff:
8867 // 1. The vector is only used in a bitcast to a integer type. I.e.,
8868 // 1.1. Vector is used only once.
8869 // 1.2. Use is a bit convert to an integer type.
8870 // 2. The size of its operands are 32-bits (64-bits are not legal).
8871 EVT VT = N->getValueType(0);
8872 EVT EltVT = VT.getVectorElementType();
8873
8874 // Check 1.1. and 2.
8875 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
8876 return SDValue();
8877
8878 // By construction, the input type must be float.
8879 assert(EltVT == MVT::f32 && "Unexpected type!");
8880
8881 // Check 1.2.
8882 SDNode *Use = *N->use_begin();
8883 if (Use->getOpcode() != ISD::BITCAST ||
8884 Use->getValueType(0).isFloatingPoint())
8885 return SDValue();
8886
8887 // Check profitability.
8888 // Model is, if more than half of the relevant operands are bitcast from
8889 // i32, turn the build_vector into a sequence of insert_vector_elt.
8890 // Relevant operands are everything that is not statically
8891 // (i.e., at compile time) bitcasted.
8892 unsigned NumOfBitCastedElts = 0;
8893 unsigned NumElts = VT.getVectorNumElements();
8894 unsigned NumOfRelevantElts = NumElts;
8895 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
8896 SDValue Elt = N->getOperand(Idx);
8897 if (Elt->getOpcode() == ISD::BITCAST) {
8898 // Assume only bit cast to i32 will go away.
8899 if (Elt->getOperand(0).getValueType() == MVT::i32)
8900 ++NumOfBitCastedElts;
8901 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt))
8902 // Constants are statically casted, thus do not count them as
8903 // relevant operands.
8904 --NumOfRelevantElts;
8905 }
8906
8907 // Check if more than half of the elements require a non-free bitcast.
8908 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
8909 return SDValue();
8910
8911 SelectionDAG &DAG = DCI.DAG;
8912 // Create the new vector type.
8913 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
8914 // Check if the type is legal.
8915 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8916 if (!TLI.isTypeLegal(VecVT))
8917 return SDValue();
8918
8919 // Combine:
8920 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
8921 // => BITCAST INSERT_VECTOR_ELT
8922 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
8923 // (BITCAST EN), N.
8924 SDValue Vec = DAG.getUNDEF(VecVT);
8925 SDLoc dl(N);
8926 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
8927 SDValue V = N->getOperand(Idx);
8928 if (V.getOpcode() == ISD::UNDEF)
8929 continue;
8930 if (V.getOpcode() == ISD::BITCAST &&
8931 V->getOperand(0).getValueType() == MVT::i32)
8932 // Fold obvious case.
8933 V = V.getOperand(0);
8934 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00008935 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008936 // Make the DAGCombiner fold the bitcasts.
8937 DCI.AddToWorklist(V.getNode());
8938 }
8939 SDValue LaneIdx = DAG.getConstant(Idx, MVT::i32);
8940 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
8941 }
8942 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
8943 // Make the DAGCombiner fold the bitcasts.
8944 DCI.AddToWorklist(Vec.getNode());
8945 return Vec;
8946}
8947
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008948/// PerformInsertEltCombine - Target-specific dag combine xforms for
8949/// ISD::INSERT_VECTOR_ELT.
8950static SDValue PerformInsertEltCombine(SDNode *N,
8951 TargetLowering::DAGCombinerInfo &DCI) {
8952 // Bitcast an i64 load inserted into a vector to f64.
8953 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8954 EVT VT = N->getValueType(0);
8955 SDNode *Elt = N->getOperand(1).getNode();
8956 if (VT.getVectorElementType() != MVT::i64 ||
8957 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8958 return SDValue();
8959
8960 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008961 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008962 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8963 VT.getVectorNumElements());
8964 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8965 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8966 // Make the DAGCombiner fold the bitcasts.
8967 DCI.AddToWorklist(Vec.getNode());
8968 DCI.AddToWorklist(V.getNode());
8969 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8970 Vec, V, N->getOperand(2));
8971 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00008972}
8973
Bob Wilsonc7334a12010-10-27 20:38:28 +00008974/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8975/// ISD::VECTOR_SHUFFLE.
8976static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8977 // The LLVM shufflevector instruction does not require the shuffle mask
8978 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8979 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8980 // operands do not match the mask length, they are extended by concatenating
8981 // them with undef vectors. That is probably the right thing for other
8982 // targets, but for NEON it is better to concatenate two double-register
8983 // size vector operands into a single quad-register size vector. Do that
8984 // transformation here:
8985 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8986 // shuffle(concat(v1, v2), undef)
8987 SDValue Op0 = N->getOperand(0);
8988 SDValue Op1 = N->getOperand(1);
8989 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8990 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8991 Op0.getNumOperands() != 2 ||
8992 Op1.getNumOperands() != 2)
8993 return SDValue();
8994 SDValue Concat0Op1 = Op0.getOperand(1);
8995 SDValue Concat1Op1 = Op1.getOperand(1);
8996 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8997 Concat1Op1.getOpcode() != ISD::UNDEF)
8998 return SDValue();
8999 // Skip the transformation if any of the types are illegal.
9000 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9001 EVT VT = N->getValueType(0);
9002 if (!TLI.isTypeLegal(VT) ||
9003 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
9004 !TLI.isTypeLegal(Concat1Op1.getValueType()))
9005 return SDValue();
9006
Andrew Trickef9de2a2013-05-25 02:42:55 +00009007 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009008 Op0.getOperand(0), Op1.getOperand(0));
9009 // Translate the shuffle mask.
9010 SmallVector<int, 16> NewMask;
9011 unsigned NumElts = VT.getVectorNumElements();
9012 unsigned HalfElts = NumElts/2;
9013 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9014 for (unsigned n = 0; n < NumElts; ++n) {
9015 int MaskElt = SVN->getMaskElt(n);
9016 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00009017 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00009018 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00009019 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00009020 NewElt = HalfElts + MaskElt - NumElts;
9021 NewMask.push_back(NewElt);
9022 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009023 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009024 DAG.getUNDEF(VT), NewMask.data());
9025}
9026
Bob Wilson06fce872011-02-07 17:43:21 +00009027/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
9028/// NEON load/store intrinsics to merge base address updates.
9029static SDValue CombineBaseUpdate(SDNode *N,
9030 TargetLowering::DAGCombinerInfo &DCI) {
9031 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9032 return SDValue();
9033
9034 SelectionDAG &DAG = DCI.DAG;
9035 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
9036 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
9037 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
9038 SDValue Addr = N->getOperand(AddrOpIdx);
9039
9040 // Search for a use of the address operand that is an increment.
9041 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
9042 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
9043 SDNode *User = *UI;
9044 if (User->getOpcode() != ISD::ADD ||
9045 UI.getUse().getResNo() != Addr.getResNo())
9046 continue;
9047
9048 // Check that the add is independent of the load/store. Otherwise, folding
9049 // it would create a cycle.
9050 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
9051 continue;
9052
9053 // Find the new opcode for the updating load/store.
9054 bool isLoad = true;
9055 bool isLaneOp = false;
9056 unsigned NewOpc = 0;
9057 unsigned NumVecs = 0;
9058 if (isIntrinsic) {
9059 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9060 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00009061 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009062 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
9063 NumVecs = 1; break;
9064 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
9065 NumVecs = 2; break;
9066 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
9067 NumVecs = 3; break;
9068 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
9069 NumVecs = 4; break;
9070 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
9071 NumVecs = 2; isLaneOp = true; break;
9072 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
9073 NumVecs = 3; isLaneOp = true; break;
9074 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
9075 NumVecs = 4; isLaneOp = true; break;
9076 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
9077 NumVecs = 1; isLoad = false; break;
9078 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
9079 NumVecs = 2; isLoad = false; break;
9080 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
9081 NumVecs = 3; isLoad = false; break;
9082 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
9083 NumVecs = 4; isLoad = false; break;
9084 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
9085 NumVecs = 2; isLoad = false; isLaneOp = true; break;
9086 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
9087 NumVecs = 3; isLoad = false; isLaneOp = true; break;
9088 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
9089 NumVecs = 4; isLoad = false; isLaneOp = true; break;
9090 }
9091 } else {
9092 isLaneOp = true;
9093 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00009094 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009095 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
9096 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
9097 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
9098 }
9099 }
9100
9101 // Find the size of memory referenced by the load/store.
9102 EVT VecTy;
9103 if (isLoad)
9104 VecTy = N->getValueType(0);
Owen Anderson77aa2662011-04-05 21:48:57 +00009105 else
Bob Wilson06fce872011-02-07 17:43:21 +00009106 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
9107 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
9108 if (isLaneOp)
9109 NumBytes /= VecTy.getVectorNumElements();
9110
9111 // If the increment is a constant, it must match the memory ref size.
9112 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
9113 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
9114 uint64_t IncVal = CInc->getZExtValue();
9115 if (IncVal != NumBytes)
9116 continue;
9117 } else if (NumBytes >= 3 * 16) {
9118 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
9119 // separate instructions that make it harder to use a non-constant update.
9120 continue;
9121 }
9122
9123 // Create the new updating load/store node.
9124 EVT Tys[6];
9125 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
9126 unsigned n;
9127 for (n = 0; n < NumResultVecs; ++n)
9128 Tys[n] = VecTy;
9129 Tys[n++] = MVT::i32;
9130 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009131 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2));
Bob Wilson06fce872011-02-07 17:43:21 +00009132 SmallVector<SDValue, 8> Ops;
9133 Ops.push_back(N->getOperand(0)); // incoming chain
9134 Ops.push_back(N->getOperand(AddrOpIdx));
9135 Ops.push_back(Inc);
9136 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
9137 Ops.push_back(N->getOperand(i));
9138 }
9139 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009140 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, SDLoc(N), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00009141 Ops, MemInt->getMemoryVT(),
Bob Wilson06fce872011-02-07 17:43:21 +00009142 MemInt->getMemOperand());
9143
9144 // Update the uses.
9145 std::vector<SDValue> NewResults;
9146 for (unsigned i = 0; i < NumResultVecs; ++i) {
9147 NewResults.push_back(SDValue(UpdN.getNode(), i));
9148 }
9149 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
9150 DCI.CombineTo(N, NewResults);
9151 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
9152
9153 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00009154 }
Bob Wilson06fce872011-02-07 17:43:21 +00009155 return SDValue();
9156}
9157
Bob Wilson2d790df2010-11-28 06:51:26 +00009158/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
9159/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
9160/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
9161/// return true.
9162static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9163 SelectionDAG &DAG = DCI.DAG;
9164 EVT VT = N->getValueType(0);
9165 // vldN-dup instructions only support 64-bit vectors for N > 1.
9166 if (!VT.is64BitVector())
9167 return false;
9168
9169 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
9170 SDNode *VLD = N->getOperand(0).getNode();
9171 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
9172 return false;
9173 unsigned NumVecs = 0;
9174 unsigned NewOpc = 0;
9175 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
9176 if (IntNo == Intrinsic::arm_neon_vld2lane) {
9177 NumVecs = 2;
9178 NewOpc = ARMISD::VLD2DUP;
9179 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
9180 NumVecs = 3;
9181 NewOpc = ARMISD::VLD3DUP;
9182 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
9183 NumVecs = 4;
9184 NewOpc = ARMISD::VLD4DUP;
9185 } else {
9186 return false;
9187 }
9188
9189 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
9190 // numbers match the load.
9191 unsigned VLDLaneNo =
9192 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
9193 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9194 UI != UE; ++UI) {
9195 // Ignore uses of the chain result.
9196 if (UI.getUse().getResNo() == NumVecs)
9197 continue;
9198 SDNode *User = *UI;
9199 if (User->getOpcode() != ARMISD::VDUPLANE ||
9200 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
9201 return false;
9202 }
9203
9204 // Create the vldN-dup node.
9205 EVT Tys[5];
9206 unsigned n;
9207 for (n = 0; n < NumVecs; ++n)
9208 Tys[n] = VT;
9209 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009210 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00009211 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
9212 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009213 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00009214 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00009215 VLDMemInt->getMemOperand());
9216
9217 // Update the uses.
9218 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9219 UI != UE; ++UI) {
9220 unsigned ResNo = UI.getUse().getResNo();
9221 // Ignore uses of the chain result.
9222 if (ResNo == NumVecs)
9223 continue;
9224 SDNode *User = *UI;
9225 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
9226 }
9227
9228 // Now the vldN-lane intrinsic is dead except for its chain result.
9229 // Update uses of the chain.
9230 std::vector<SDValue> VLDDupResults;
9231 for (unsigned n = 0; n < NumVecs; ++n)
9232 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
9233 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
9234 DCI.CombineTo(VLD, VLDDupResults);
9235
9236 return true;
9237}
9238
Bob Wilson103a0dc2010-07-14 01:22:12 +00009239/// PerformVDUPLANECombine - Target-specific dag combine xforms for
9240/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00009241static SDValue PerformVDUPLANECombine(SDNode *N,
9242 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00009243 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009244
Bob Wilson2d790df2010-11-28 06:51:26 +00009245 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
9246 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
9247 if (CombineVLDDUP(N, DCI))
9248 return SDValue(N, 0);
9249
9250 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
9251 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00009252 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009253 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00009254 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009255 return SDValue();
9256
9257 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
9258 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
9259 // The canonical VMOV for a zero vector uses a 32-bit element size.
9260 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9261 unsigned EltBits;
9262 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
9263 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00009264 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009265 if (EltSize > VT.getVectorElementType().getSizeInBits())
9266 return SDValue();
9267
Andrew Trickef9de2a2013-05-25 02:42:55 +00009268 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009269}
9270
Eric Christopher1b8b94192011-06-29 21:10:36 +00009271// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosierfa8d8932011-06-24 19:23:04 +00009272// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
9273static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
9274{
Chad Rosier6b610b32011-06-28 17:26:57 +00009275 integerPart cN;
9276 integerPart c0 = 0;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009277 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
9278 I != E; I++) {
9279 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
9280 if (!C)
9281 return false;
9282
Eric Christopher1b8b94192011-06-29 21:10:36 +00009283 bool isExact;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009284 APFloat APF = C->getValueAPF();
9285 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
9286 != APFloat::opOK || !isExact)
9287 return false;
9288
9289 c0 = (I == 0) ? cN : c0;
9290 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
9291 return false;
9292 }
9293 C = c0;
9294 return true;
9295}
9296
9297/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
9298/// can replace combinations of VMUL and VCVT (floating-point to integer)
9299/// when the VMUL has a constant operand that is a power of 2.
9300///
9301/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9302/// vmul.f32 d16, d17, d16
9303/// vcvt.s32.f32 d16, d16
9304/// becomes:
9305/// vcvt.s32.f32 d16, d16, #3
9306static SDValue PerformVCVTCombine(SDNode *N,
9307 TargetLowering::DAGCombinerInfo &DCI,
9308 const ARMSubtarget *Subtarget) {
9309 SelectionDAG &DAG = DCI.DAG;
9310 SDValue Op = N->getOperand(0);
9311
9312 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
9313 Op.getOpcode() != ISD::FMUL)
9314 return SDValue();
9315
9316 uint64_t C;
9317 SDValue N0 = Op->getOperand(0);
9318 SDValue ConstVec = Op->getOperand(1);
9319 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
9320
Eric Christopher1b8b94192011-06-29 21:10:36 +00009321 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosierfa8d8932011-06-24 19:23:04 +00009322 !isConstVecPow2(ConstVec, isSigned, C))
9323 return SDValue();
9324
Tim Northover7cbc2152013-06-28 15:29:25 +00009325 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
9326 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
9327 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9328 // These instructions only exist converting from f32 to i32. We can handle
9329 // smaller integers by generating an extra truncate, but larger ones would
9330 // be lossy.
9331 return SDValue();
9332 }
9333
Chad Rosierfa8d8932011-06-24 19:23:04 +00009334 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
9335 Intrinsic::arm_neon_vcvtfp2fxu;
Tim Northover7cbc2152013-06-28 15:29:25 +00009336 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9337 SDValue FixConv = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
9338 NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9339 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
9340 DAG.getConstant(Log2_64(C), MVT::i32));
9341
9342 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9343 FixConv = DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), FixConv);
9344
9345 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009346}
9347
9348/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
9349/// can replace combinations of VCVT (integer to floating-point) and VDIV
9350/// when the VDIV has a constant operand that is a power of 2.
9351///
9352/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9353/// vcvt.f32.s32 d16, d16
9354/// vdiv.f32 d16, d17, d16
9355/// becomes:
9356/// vcvt.f32.s32 d16, d16, #3
9357static SDValue PerformVDIVCombine(SDNode *N,
9358 TargetLowering::DAGCombinerInfo &DCI,
9359 const ARMSubtarget *Subtarget) {
9360 SelectionDAG &DAG = DCI.DAG;
9361 SDValue Op = N->getOperand(0);
9362 unsigned OpOpcode = Op.getNode()->getOpcode();
9363
9364 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
9365 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
9366 return SDValue();
9367
9368 uint64_t C;
9369 SDValue ConstVec = N->getOperand(1);
9370 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
9371
9372 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
9373 !isConstVecPow2(ConstVec, isSigned, C))
9374 return SDValue();
9375
Tim Northover7cbc2152013-06-28 15:29:25 +00009376 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
9377 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
9378 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9379 // These instructions only exist converting from i32 to f32. We can handle
9380 // smaller integers by generating an extra extend, but larger ones would
9381 // be lossy.
9382 return SDValue();
9383 }
9384
9385 SDValue ConvInput = Op.getOperand(0);
9386 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9387 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9388 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
9389 SDLoc(N), NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9390 ConvInput);
9391
Eric Christopher1b8b94192011-06-29 21:10:36 +00009392 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +00009393 Intrinsic::arm_neon_vcvtfxu2fp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009394 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
Chad Rosierfa8d8932011-06-24 19:23:04 +00009395 Op.getValueType(),
Eric Christopher1b8b94192011-06-29 21:10:36 +00009396 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Tim Northover7cbc2152013-06-28 15:29:25 +00009397 ConvInput, DAG.getConstant(Log2_64(C), MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +00009398}
9399
9400/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +00009401/// operand of a vector shift operation, where all the elements of the
9402/// build_vector must have the same constant integer value.
9403static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
9404 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +00009405 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00009406 Op = Op.getOperand(0);
9407 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
9408 APInt SplatBits, SplatUndef;
9409 unsigned SplatBitSize;
9410 bool HasAnyUndefs;
9411 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
9412 HasAnyUndefs, ElementBits) ||
9413 SplatBitSize > ElementBits)
9414 return false;
9415 Cnt = SplatBits.getSExtValue();
9416 return true;
9417}
9418
9419/// isVShiftLImm - Check if this is a valid build_vector for the immediate
9420/// operand of a vector shift left operation. That value must be in the range:
9421/// 0 <= Value < ElementBits for a left shift; or
9422/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009423static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009424 assert(VT.isVector() && "vector shift count is not a vector type");
9425 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9426 if (! getVShiftImm(Op, ElementBits, Cnt))
9427 return false;
9428 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
9429}
9430
9431/// isVShiftRImm - Check if this is a valid build_vector for the immediate
9432/// operand of a vector shift right operation. For a shift opcode, the value
9433/// is positive, but for an intrinsic the value count must be negative. The
9434/// absolute value must be in the range:
9435/// 1 <= |Value| <= ElementBits for a right shift; or
9436/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009437static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +00009438 int64_t &Cnt) {
9439 assert(VT.isVector() && "vector shift count is not a vector type");
9440 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9441 if (! getVShiftImm(Op, ElementBits, Cnt))
9442 return false;
9443 if (isIntrinsic)
9444 Cnt = -Cnt;
9445 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
9446}
9447
9448/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
9449static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
9450 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
9451 switch (IntNo) {
9452 default:
9453 // Don't do anything for most intrinsics.
9454 break;
9455
9456 // Vector shifts: check for immediate versions and lower them.
9457 // Note: This is done during DAG combining instead of DAG legalizing because
9458 // the build_vectors for 64-bit vector element shift counts are generally
9459 // not legal, and it is hard to see their values after they get legalized to
9460 // loads from a constant pool.
9461 case Intrinsic::arm_neon_vshifts:
9462 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +00009463 case Intrinsic::arm_neon_vrshifts:
9464 case Intrinsic::arm_neon_vrshiftu:
9465 case Intrinsic::arm_neon_vrshiftn:
9466 case Intrinsic::arm_neon_vqshifts:
9467 case Intrinsic::arm_neon_vqshiftu:
9468 case Intrinsic::arm_neon_vqshiftsu:
9469 case Intrinsic::arm_neon_vqshiftns:
9470 case Intrinsic::arm_neon_vqshiftnu:
9471 case Intrinsic::arm_neon_vqshiftnsu:
9472 case Intrinsic::arm_neon_vqrshiftns:
9473 case Intrinsic::arm_neon_vqrshiftnu:
9474 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009475 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009476 int64_t Cnt;
9477 unsigned VShiftOpc = 0;
9478
9479 switch (IntNo) {
9480 case Intrinsic::arm_neon_vshifts:
9481 case Intrinsic::arm_neon_vshiftu:
9482 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
9483 VShiftOpc = ARMISD::VSHL;
9484 break;
9485 }
9486 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
9487 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
9488 ARMISD::VSHRs : ARMISD::VSHRu);
9489 break;
9490 }
9491 return SDValue();
9492
Bob Wilson2e076c42009-06-22 23:27:02 +00009493 case Intrinsic::arm_neon_vrshifts:
9494 case Intrinsic::arm_neon_vrshiftu:
9495 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9496 break;
9497 return SDValue();
9498
9499 case Intrinsic::arm_neon_vqshifts:
9500 case Intrinsic::arm_neon_vqshiftu:
9501 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9502 break;
9503 return SDValue();
9504
9505 case Intrinsic::arm_neon_vqshiftsu:
9506 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9507 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +00009508 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009509
Bob Wilson2e076c42009-06-22 23:27:02 +00009510 case Intrinsic::arm_neon_vrshiftn:
9511 case Intrinsic::arm_neon_vqshiftns:
9512 case Intrinsic::arm_neon_vqshiftnu:
9513 case Intrinsic::arm_neon_vqshiftnsu:
9514 case Intrinsic::arm_neon_vqrshiftns:
9515 case Intrinsic::arm_neon_vqrshiftnu:
9516 case Intrinsic::arm_neon_vqrshiftnsu:
9517 // Narrowing shifts require an immediate right shift.
9518 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9519 break;
Jim Grosbach84511e12010-06-02 21:53:11 +00009520 llvm_unreachable("invalid shift count for narrowing vector shift "
9521 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009522
9523 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00009524 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00009525 }
9526
9527 switch (IntNo) {
9528 case Intrinsic::arm_neon_vshifts:
9529 case Intrinsic::arm_neon_vshiftu:
9530 // Opcode already set above.
9531 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00009532 case Intrinsic::arm_neon_vrshifts:
9533 VShiftOpc = ARMISD::VRSHRs; break;
9534 case Intrinsic::arm_neon_vrshiftu:
9535 VShiftOpc = ARMISD::VRSHRu; break;
9536 case Intrinsic::arm_neon_vrshiftn:
9537 VShiftOpc = ARMISD::VRSHRN; break;
9538 case Intrinsic::arm_neon_vqshifts:
9539 VShiftOpc = ARMISD::VQSHLs; break;
9540 case Intrinsic::arm_neon_vqshiftu:
9541 VShiftOpc = ARMISD::VQSHLu; break;
9542 case Intrinsic::arm_neon_vqshiftsu:
9543 VShiftOpc = ARMISD::VQSHLsu; break;
9544 case Intrinsic::arm_neon_vqshiftns:
9545 VShiftOpc = ARMISD::VQSHRNs; break;
9546 case Intrinsic::arm_neon_vqshiftnu:
9547 VShiftOpc = ARMISD::VQSHRNu; break;
9548 case Intrinsic::arm_neon_vqshiftnsu:
9549 VShiftOpc = ARMISD::VQSHRNsu; break;
9550 case Intrinsic::arm_neon_vqrshiftns:
9551 VShiftOpc = ARMISD::VQRSHRNs; break;
9552 case Intrinsic::arm_neon_vqrshiftnu:
9553 VShiftOpc = ARMISD::VQRSHRNu; break;
9554 case Intrinsic::arm_neon_vqrshiftnsu:
9555 VShiftOpc = ARMISD::VQRSHRNsu; break;
9556 }
9557
Andrew Trickef9de2a2013-05-25 02:42:55 +00009558 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009559 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009560 }
9561
9562 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009563 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009564 int64_t Cnt;
9565 unsigned VShiftOpc = 0;
9566
9567 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9568 VShiftOpc = ARMISD::VSLI;
9569 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9570 VShiftOpc = ARMISD::VSRI;
9571 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009572 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009573 }
9574
Andrew Trickef9de2a2013-05-25 02:42:55 +00009575 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +00009576 N->getOperand(1), N->getOperand(2),
Owen Anderson9f944592009-08-11 20:47:22 +00009577 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009578 }
9579
9580 case Intrinsic::arm_neon_vqrshifts:
9581 case Intrinsic::arm_neon_vqrshiftu:
9582 // No immediate versions of these to check for.
9583 break;
9584 }
9585
9586 return SDValue();
9587}
9588
9589/// PerformShiftCombine - Checks for immediate versions of vector shifts and
9590/// lowers them. As with the vector shift intrinsics, this is done during DAG
9591/// combining instead of DAG legalizing because the build_vectors for 64-bit
9592/// vector element shift counts are generally not legal, and it is hard to see
9593/// their values after they get legalized to loads from a constant pool.
9594static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9595 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009596 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +00009597 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9598 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9599 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9600 SDValue N1 = N->getOperand(1);
9601 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9602 SDValue N0 = N->getOperand(0);
9603 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9604 DAG.MaskedValueIsZero(N0.getOperand(0),
9605 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009606 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +00009607 }
9608 }
Bob Wilson2e076c42009-06-22 23:27:02 +00009609
9610 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +00009611 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9612 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +00009613 return SDValue();
9614
9615 assert(ST->hasNEON() && "unexpected vector shift");
9616 int64_t Cnt;
9617
9618 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009619 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009620
9621 case ISD::SHL:
9622 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009623 return DAG.getNode(ARMISD::VSHL, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009624 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009625 break;
9626
9627 case ISD::SRA:
9628 case ISD::SRL:
9629 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
9630 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
9631 ARMISD::VSHRs : ARMISD::VSHRu);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009632 return DAG.getNode(VShiftOpc, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009633 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009634 }
9635 }
9636 return SDValue();
9637}
9638
9639/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
9640/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
9641static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
9642 const ARMSubtarget *ST) {
9643 SDValue N0 = N->getOperand(0);
9644
9645 // Check for sign- and zero-extensions of vector extract operations of 8-
9646 // and 16-bit vector elements. NEON supports these directly. They are
9647 // handled during DAG combining because type legalization will promote them
9648 // to 32-bit types and it is messy to recognize the operations after that.
9649 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9650 SDValue Vec = N0.getOperand(0);
9651 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00009652 EVT VT = N->getValueType(0);
9653 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009654 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9655
Owen Anderson9f944592009-08-11 20:47:22 +00009656 if (VT == MVT::i32 &&
9657 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +00009658 TLI.isTypeLegal(Vec.getValueType()) &&
9659 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009660
9661 unsigned Opc = 0;
9662 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009663 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009664 case ISD::SIGN_EXTEND:
9665 Opc = ARMISD::VGETLANEs;
9666 break;
9667 case ISD::ZERO_EXTEND:
9668 case ISD::ANY_EXTEND:
9669 Opc = ARMISD::VGETLANEu;
9670 break;
9671 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009672 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +00009673 }
9674 }
9675
9676 return SDValue();
9677}
9678
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009679/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9680/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9681static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9682 const ARMSubtarget *ST) {
9683 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng55f0c6b2010-07-15 22:07:12 +00009684 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009685 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9686 // a NaN; only do the transformation when it matches that behavior.
9687
9688 // For now only do this when using NEON for FP operations; if using VFP, it
9689 // is not obvious that the benefit outweighs the cost of switching to the
9690 // NEON pipeline.
9691 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9692 N->getValueType(0) != MVT::f32)
9693 return SDValue();
9694
9695 SDValue CondLHS = N->getOperand(0);
9696 SDValue CondRHS = N->getOperand(1);
9697 SDValue LHS = N->getOperand(2);
9698 SDValue RHS = N->getOperand(3);
9699 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9700
9701 unsigned Opcode = 0;
9702 bool IsReversed;
Bob Wilsonba8ac742010-02-24 22:15:53 +00009703 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009704 IsReversed = false; // x CC y ? x : y
Bob Wilsonba8ac742010-02-24 22:15:53 +00009705 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009706 IsReversed = true ; // x CC y ? y : x
9707 } else {
9708 return SDValue();
9709 }
9710
Bob Wilsonba8ac742010-02-24 22:15:53 +00009711 bool IsUnordered;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009712 switch (CC) {
9713 default: break;
9714 case ISD::SETOLT:
9715 case ISD::SETOLE:
9716 case ISD::SETLT:
9717 case ISD::SETLE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009718 case ISD::SETULT:
9719 case ISD::SETULE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009720 // If LHS is NaN, an ordered comparison will be false and the result will
9721 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
9722 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9723 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
9724 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9725 break;
9726 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
9727 // will return -0, so vmin can only be used for unsafe math or if one of
9728 // the operands is known to be nonzero.
9729 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009730 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009731 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9732 break;
9733 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009734 break;
9735
9736 case ISD::SETOGT:
9737 case ISD::SETOGE:
9738 case ISD::SETGT:
9739 case ISD::SETGE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009740 case ISD::SETUGT:
9741 case ISD::SETUGE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009742 // If LHS is NaN, an ordered comparison will be false and the result will
9743 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
9744 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9745 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
9746 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9747 break;
9748 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
9749 // will return +0, so vmax can only be used for unsafe math or if one of
9750 // the operands is known to be nonzero.
9751 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009752 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009753 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9754 break;
9755 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009756 break;
9757 }
9758
9759 if (!Opcode)
9760 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009761 return DAG.getNode(Opcode, SDLoc(N), N->getValueType(0), LHS, RHS);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009762}
9763
Evan Chengf863e3f2011-07-13 00:42:17 +00009764/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
9765SDValue
9766ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
9767 SDValue Cmp = N->getOperand(4);
9768 if (Cmp.getOpcode() != ARMISD::CMPZ)
9769 // Only looking at EQ and NE cases.
9770 return SDValue();
9771
9772 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009773 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +00009774 SDValue LHS = Cmp.getOperand(0);
9775 SDValue RHS = Cmp.getOperand(1);
9776 SDValue FalseVal = N->getOperand(0);
9777 SDValue TrueVal = N->getOperand(1);
9778 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +00009779 ARMCC::CondCodes CC =
9780 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +00009781
9782 // Simplify
9783 // mov r1, r0
9784 // cmp r1, x
9785 // mov r0, y
9786 // moveq r0, x
9787 // to
9788 // cmp r0, x
9789 // movne r0, y
9790 //
9791 // mov r1, r0
9792 // cmp r1, x
9793 // mov r0, x
9794 // movne r0, y
9795 // to
9796 // cmp r0, x
9797 // movne r0, y
9798 /// FIXME: Turn this into a target neutral optimization?
9799 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +00009800 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +00009801 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
9802 N->getOperand(3), Cmp);
9803 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
9804 SDValue ARMcc;
9805 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
9806 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
9807 N->getOperand(3), NewCmp);
9808 }
9809
9810 if (Res.getNode()) {
9811 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00009812 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +00009813 // Capture demanded bits information that would be otherwise lost.
9814 if (KnownZero == 0xfffffffe)
9815 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9816 DAG.getValueType(MVT::i1));
9817 else if (KnownZero == 0xffffff00)
9818 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9819 DAG.getValueType(MVT::i8));
9820 else if (KnownZero == 0xffff0000)
9821 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9822 DAG.getValueType(MVT::i16));
9823 }
9824
9825 return Res;
9826}
9827
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009828SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +00009829 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009830 switch (N->getOpcode()) {
9831 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009832 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +00009833 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009834 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009835 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +00009836 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +00009837 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
9838 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +00009839 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +00009840 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget);
Bob Wilson22806742010-09-22 22:09:21 +00009841 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009842 case ISD::STORE: return PerformSTORECombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +00009843 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009844 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +00009845 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +00009846 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +00009847 case ISD::FP_TO_SINT:
9848 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
9849 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009850 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00009851 case ISD::SHL:
9852 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009853 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00009854 case ISD::SIGN_EXTEND:
9855 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009856 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
9857 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +00009858 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson06fce872011-02-07 17:43:21 +00009859 case ARMISD::VLD2DUP:
9860 case ARMISD::VLD3DUP:
9861 case ARMISD::VLD4DUP:
9862 return CombineBaseUpdate(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009863 case ARMISD::BUILD_VECTOR:
9864 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +00009865 case ISD::INTRINSIC_VOID:
9866 case ISD::INTRINSIC_W_CHAIN:
9867 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9868 case Intrinsic::arm_neon_vld1:
9869 case Intrinsic::arm_neon_vld2:
9870 case Intrinsic::arm_neon_vld3:
9871 case Intrinsic::arm_neon_vld4:
9872 case Intrinsic::arm_neon_vld2lane:
9873 case Intrinsic::arm_neon_vld3lane:
9874 case Intrinsic::arm_neon_vld4lane:
9875 case Intrinsic::arm_neon_vst1:
9876 case Intrinsic::arm_neon_vst2:
9877 case Intrinsic::arm_neon_vst3:
9878 case Intrinsic::arm_neon_vst4:
9879 case Intrinsic::arm_neon_vst2lane:
9880 case Intrinsic::arm_neon_vst3lane:
9881 case Intrinsic::arm_neon_vst4lane:
9882 return CombineBaseUpdate(N, DCI);
9883 default: break;
9884 }
9885 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009886 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009887 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009888}
9889
Evan Chengd42641c2011-02-02 01:06:55 +00009890bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9891 EVT VT) const {
9892 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9893}
9894
Matt Arsenault6f2a5262014-07-27 17:46:40 +00009895bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
9896 unsigned,
9897 unsigned,
9898 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009899 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +00009900 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009901
9902 switch (VT.getSimpleVT().SimpleTy) {
9903 default:
9904 return false;
9905 case MVT::i8:
9906 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009907 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009908 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +00009909 if (AllowsUnaligned) {
9910 if (Fast)
9911 *Fast = Subtarget->hasV7Ops();
9912 return true;
9913 }
9914 return false;
9915 }
Evan Chengeec6bc62012-08-15 17:44:53 +00009916 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009917 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009918 // For any little-endian targets with neon, we can support unaligned ld/st
9919 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +00009920 // A big-endian target may also explicitly support unaligned accesses
Evan Cheng79e2ca92012-12-10 23:21:26 +00009921 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) {
9922 if (Fast)
9923 *Fast = true;
9924 return true;
9925 }
9926 return false;
9927 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009928 }
9929}
9930
Lang Hames9929c422011-11-02 22:52:45 +00009931static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
9932 unsigned AlignCheck) {
9933 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
9934 (DstAlign == 0 || DstAlign % AlignCheck == 0));
9935}
9936
9937EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
9938 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00009939 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +00009940 bool MemcpyStrSrc,
9941 MachineFunction &MF) const {
9942 const Function *F = MF.getFunction();
9943
9944 // See if we can use NEON instructions for this...
Evan Cheng962711e2012-12-12 02:34:41 +00009945 if ((!IsMemset || ZeroMemset) &&
Evan Cheng79e2ca92012-12-10 23:21:26 +00009946 Subtarget->hasNEON() &&
Bill Wendling698e84f2012-12-30 10:32:01 +00009947 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
9948 Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009949 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +00009950 if (Size >= 16 &&
9951 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +00009952 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009953 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +00009954 } else if (Size >= 8 &&
9955 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +00009956 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) &&
9957 Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009958 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +00009959 }
9960 }
9961
Lang Hamesb85fcd02011-11-08 18:56:23 +00009962 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +00009963 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +00009964 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +00009965 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +00009966 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +00009967
Lang Hames9929c422011-11-02 22:52:45 +00009968 // Let the target-independent logic figure it out.
9969 return MVT::Other;
9970}
9971
Evan Cheng9ec512d2012-12-06 19:13:27 +00009972bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
9973 if (Val.getOpcode() != ISD::LOAD)
9974 return false;
9975
9976 EVT VT1 = Val.getValueType();
9977 if (!VT1.isSimple() || !VT1.isInteger() ||
9978 !VT2.isSimple() || !VT2.isInteger())
9979 return false;
9980
9981 switch (VT1.getSimpleVT().SimpleTy) {
9982 default: break;
9983 case MVT::i1:
9984 case MVT::i8:
9985 case MVT::i16:
9986 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
9987 return true;
9988 }
9989
9990 return false;
9991}
9992
Tim Northovercc2e9032013-08-06 13:58:03 +00009993bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
9994 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
9995 return false;
9996
9997 if (!isTypeLegal(EVT::getEVT(Ty1)))
9998 return false;
9999
10000 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
10001
10002 // Assuming the caller doesn't have a zeroext or signext return parameter,
10003 // truncation all the way down to i1 is valid.
10004 return true;
10005}
10006
10007
Evan Chengdc49a8d2009-08-14 20:09:37 +000010008static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
10009 if (V < 0)
10010 return false;
10011
10012 unsigned Scale = 1;
10013 switch (VT.getSimpleVT().SimpleTy) {
10014 default: return false;
10015 case MVT::i1:
10016 case MVT::i8:
10017 // Scale == 1;
10018 break;
10019 case MVT::i16:
10020 // Scale == 2;
10021 Scale = 2;
10022 break;
10023 case MVT::i32:
10024 // Scale == 4;
10025 Scale = 4;
10026 break;
10027 }
10028
10029 if ((V & (Scale - 1)) != 0)
10030 return false;
10031 V /= Scale;
10032 return V == (V & ((1LL << 5) - 1));
10033}
10034
10035static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
10036 const ARMSubtarget *Subtarget) {
10037 bool isNeg = false;
10038 if (V < 0) {
10039 isNeg = true;
10040 V = - V;
10041 }
10042
10043 switch (VT.getSimpleVT().SimpleTy) {
10044 default: return false;
10045 case MVT::i1:
10046 case MVT::i8:
10047 case MVT::i16:
10048 case MVT::i32:
10049 // + imm12 or - imm8
10050 if (isNeg)
10051 return V == (V & ((1LL << 8) - 1));
10052 return V == (V & ((1LL << 12) - 1));
10053 case MVT::f32:
10054 case MVT::f64:
10055 // Same as ARM mode. FIXME: NEON?
10056 if (!Subtarget->hasVFP2())
10057 return false;
10058 if ((V & 3) != 0)
10059 return false;
10060 V >>= 2;
10061 return V == (V & ((1LL << 8) - 1));
10062 }
10063}
10064
Evan Cheng2150b922007-03-12 23:30:29 +000010065/// isLegalAddressImmediate - Return true if the integer value can be used
10066/// as the offset of the target addressing mode for load / store of the
10067/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010068static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010069 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +000010070 if (V == 0)
10071 return true;
10072
Evan Chengce5dfb62009-03-09 19:15:00 +000010073 if (!VT.isSimple())
10074 return false;
10075
Evan Chengdc49a8d2009-08-14 20:09:37 +000010076 if (Subtarget->isThumb1Only())
10077 return isLegalT1AddressImmediate(V, VT);
10078 else if (Subtarget->isThumb2())
10079 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +000010080
Evan Chengdc49a8d2009-08-14 20:09:37 +000010081 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +000010082 if (V < 0)
10083 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +000010084 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +000010085 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010086 case MVT::i1:
10087 case MVT::i8:
10088 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +000010089 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010090 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010091 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +000010092 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010093 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010094 case MVT::f32:
10095 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010096 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +000010097 return false;
Evan Chengbef131de2007-05-03 02:00:18 +000010098 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +000010099 return false;
10100 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010101 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +000010102 }
Evan Cheng10043e22007-01-19 07:51:42 +000010103}
10104
Evan Chengdc49a8d2009-08-14 20:09:37 +000010105bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
10106 EVT VT) const {
10107 int Scale = AM.Scale;
10108 if (Scale < 0)
10109 return false;
10110
10111 switch (VT.getSimpleVT().SimpleTy) {
10112 default: return false;
10113 case MVT::i1:
10114 case MVT::i8:
10115 case MVT::i16:
10116 case MVT::i32:
10117 if (Scale == 1)
10118 return true;
10119 // r + r << imm
10120 Scale = Scale & ~1;
10121 return Scale == 2 || Scale == 4 || Scale == 8;
10122 case MVT::i64:
10123 // r + r
10124 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
10125 return true;
10126 return false;
10127 case MVT::isVoid:
10128 // Note, we allow "void" uses (basically, uses that aren't loads or
10129 // stores), because arm allows folding a scale into many arithmetic
10130 // operations. This should be made more precise and revisited later.
10131
10132 // Allow r << imm, but the imm has to be a multiple of two.
10133 if (Scale & 1) return false;
10134 return isPowerOf2_32(Scale);
10135 }
10136}
10137
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010138/// isLegalAddressingMode - Return true if the addressing mode represented
10139/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson7117a912009-03-20 22:42:55 +000010140bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +000010141 Type *Ty) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010142 EVT VT = getValueType(Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +000010143 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +000010144 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010145
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010146 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +000010147 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010148 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010149
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010150 switch (AM.Scale) {
10151 case 0: // no scale reg, must be "r+i" or "r", or "i".
10152 break;
10153 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010154 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010155 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +000010156 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010157 default:
Chris Lattner502c3f42007-04-13 06:50:55 +000010158 // ARM doesn't support any R+R*scale+imm addr modes.
10159 if (AM.BaseOffs)
10160 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010161
Bob Wilson866c1742009-04-08 17:55:28 +000010162 if (!VT.isSimple())
10163 return false;
10164
Evan Chengdc49a8d2009-08-14 20:09:37 +000010165 if (Subtarget->isThumb2())
10166 return isLegalT2ScaledAddressingMode(AM, VT);
10167
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010168 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +000010169 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010170 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010171 case MVT::i1:
10172 case MVT::i8:
10173 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010174 if (Scale < 0) Scale = -Scale;
10175 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010176 return true;
10177 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +000010178 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +000010179 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010180 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010181 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010182 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010183 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +000010184 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010185
Owen Anderson9f944592009-08-11 20:47:22 +000010186 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010187 // Note, we allow "void" uses (basically, uses that aren't loads or
10188 // stores), because arm allows folding a scale into many arithmetic
10189 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +000010190
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010191 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +000010192 if (Scale & 1) return false;
10193 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010194 }
Evan Cheng2150b922007-03-12 23:30:29 +000010195 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010196 return true;
Evan Cheng2150b922007-03-12 23:30:29 +000010197}
10198
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010199/// isLegalICmpImmediate - Return true if the specified immediate is legal
10200/// icmp immediate, that is the target has icmp instructions which can compare
10201/// a register against the immediate without having to materialize the
10202/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +000010203bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010204 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010205 if (!Subtarget->isThumb())
Chandler Carruth8a102c22012-04-06 20:10:52 +000010206 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010207 if (Subtarget->isThumb2())
Chandler Carruth8a102c22012-04-06 20:10:52 +000010208 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010209 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +000010210 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010211}
10212
Andrew Tricka22cdb72012-07-18 18:34:27 +000010213/// isLegalAddImmediate - Return true if the specified immediate is a legal add
10214/// *or sub* immediate, that is the target has add or sub instructions which can
10215/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +000010216/// immediate into a register.
10217bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +000010218 // Same encoding for add/sub, just flip the sign.
10219 int64_t AbsImm = llvm::abs64(Imm);
10220 if (!Subtarget->isThumb())
10221 return ARM_AM::getSOImmVal(AbsImm) != -1;
10222 if (Subtarget->isThumb2())
10223 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
10224 // Thumb1 only has 8-bit unsigned immediate.
10225 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +000010226}
10227
Owen Anderson53aa7a92009-08-10 22:56:29 +000010228static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010229 bool isSEXTLoad, SDValue &Base,
10230 SDValue &Offset, bool &isInc,
10231 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +000010232 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10233 return false;
10234
Owen Anderson9f944592009-08-11 20:47:22 +000010235 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +000010236 // AddressingMode 3
10237 Base = Ptr->getOperand(0);
10238 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010239 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010240 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010241 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010242 isInc = false;
10243 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10244 return true;
10245 }
10246 }
10247 isInc = (Ptr->getOpcode() == ISD::ADD);
10248 Offset = Ptr->getOperand(1);
10249 return true;
Owen Anderson9f944592009-08-11 20:47:22 +000010250 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +000010251 // AddressingMode 2
10252 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010253 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010254 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010255 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010256 isInc = false;
10257 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10258 Base = Ptr->getOperand(0);
10259 return true;
10260 }
10261 }
10262
10263 if (Ptr->getOpcode() == ISD::ADD) {
10264 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +000010265 ARM_AM::ShiftOpc ShOpcVal=
10266 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +000010267 if (ShOpcVal != ARM_AM::no_shift) {
10268 Base = Ptr->getOperand(1);
10269 Offset = Ptr->getOperand(0);
10270 } else {
10271 Base = Ptr->getOperand(0);
10272 Offset = Ptr->getOperand(1);
10273 }
10274 return true;
10275 }
10276
10277 isInc = (Ptr->getOpcode() == ISD::ADD);
10278 Base = Ptr->getOperand(0);
10279 Offset = Ptr->getOperand(1);
10280 return true;
10281 }
10282
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000010283 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000010284 return false;
10285}
10286
Owen Anderson53aa7a92009-08-10 22:56:29 +000010287static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010288 bool isSEXTLoad, SDValue &Base,
10289 SDValue &Offset, bool &isInc,
10290 SelectionDAG &DAG) {
10291 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10292 return false;
10293
10294 Base = Ptr->getOperand(0);
10295 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
10296 int RHSC = (int)RHS->getZExtValue();
10297 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
10298 assert(Ptr->getOpcode() == ISD::ADD);
10299 isInc = false;
10300 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10301 return true;
10302 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
10303 isInc = Ptr->getOpcode() == ISD::ADD;
10304 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
10305 return true;
10306 }
10307 }
10308
10309 return false;
10310}
10311
Evan Cheng10043e22007-01-19 07:51:42 +000010312/// getPreIndexedAddressParts - returns true by value, base pointer and
10313/// offset pointer and addressing mode by reference if the node's address
10314/// can be legally represented as pre-indexed load / store address.
10315bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010316ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
10317 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010318 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010319 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010320 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010321 return false;
10322
Owen Anderson53aa7a92009-08-10 22:56:29 +000010323 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010324 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010325 bool isSEXTLoad = false;
10326 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
10327 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010328 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010329 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10330 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
10331 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010332 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010333 } else
10334 return false;
10335
10336 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010337 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010338 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010339 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
10340 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010341 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010342 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000010343 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000010344 if (!isLegal)
10345 return false;
10346
10347 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
10348 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010349}
10350
10351/// getPostIndexedAddressParts - returns true by value, base pointer and
10352/// offset pointer and addressing mode by reference if this node can be
10353/// combined with a load / store to form a post-indexed load / store.
10354bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010355 SDValue &Base,
10356 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010357 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010358 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010359 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010360 return false;
10361
Owen Anderson53aa7a92009-08-10 22:56:29 +000010362 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010363 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010364 bool isSEXTLoad = false;
10365 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010366 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010367 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010368 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10369 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010370 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010371 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010372 } else
10373 return false;
10374
10375 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010376 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010377 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010378 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000010379 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010380 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010381 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
10382 isInc, DAG);
10383 if (!isLegal)
10384 return false;
10385
Evan Chengf19384d2010-05-18 21:31:17 +000010386 if (Ptr != Base) {
10387 // Swap base ptr and offset to catch more post-index load / store when
10388 // it's legal. In Thumb2 mode, offset must be an immediate.
10389 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
10390 !Subtarget->isThumb2())
10391 std::swap(Base, Offset);
10392
10393 // Post-indexed load / store update the base pointer.
10394 if (Ptr != Base)
10395 return false;
10396 }
10397
Evan Cheng84c6cda2009-07-02 07:28:31 +000010398 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
10399 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010400}
10401
Jay Foada0653a32014-05-14 21:14:37 +000010402void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
10403 APInt &KnownZero,
10404 APInt &KnownOne,
10405 const SelectionDAG &DAG,
10406 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000010407 unsigned BitWidth = KnownOne.getBitWidth();
10408 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000010409 switch (Op.getOpcode()) {
10410 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000010411 case ARMISD::ADDC:
10412 case ARMISD::ADDE:
10413 case ARMISD::SUBC:
10414 case ARMISD::SUBE:
10415 // These nodes' second result is a boolean
10416 if (Op.getResNo() == 0)
10417 break;
10418 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
10419 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010420 case ARMISD::CMOV: {
10421 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000010422 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010423 if (KnownZero == 0 && KnownOne == 0) return;
10424
Dan Gohmanf990faf2008-02-13 00:35:47 +000010425 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000010426 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010427 KnownZero &= KnownZeroRHS;
10428 KnownOne &= KnownOneRHS;
10429 return;
10430 }
Tim Northover01b4aa92014-04-03 15:10:35 +000010431 case ISD::INTRINSIC_W_CHAIN: {
10432 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
10433 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
10434 switch (IntID) {
10435 default: return;
10436 case Intrinsic::arm_ldaex:
10437 case Intrinsic::arm_ldrex: {
10438 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
10439 unsigned MemBits = VT.getScalarType().getSizeInBits();
10440 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
10441 return;
10442 }
10443 }
10444 }
Evan Cheng10043e22007-01-19 07:51:42 +000010445 }
10446}
10447
10448//===----------------------------------------------------------------------===//
10449// ARM Inline Assembly Support
10450//===----------------------------------------------------------------------===//
10451
Evan Cheng078b0b02011-01-08 01:24:27 +000010452bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
10453 // Looking for "rev" which is V6+.
10454 if (!Subtarget->hasV6Ops())
10455 return false;
10456
10457 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10458 std::string AsmStr = IA->getAsmString();
10459 SmallVector<StringRef, 4> AsmPieces;
10460 SplitString(AsmStr, AsmPieces, ";\n");
10461
10462 switch (AsmPieces.size()) {
10463 default: return false;
10464 case 1:
10465 AsmStr = AsmPieces[0];
10466 AsmPieces.clear();
10467 SplitString(AsmStr, AsmPieces, " \t,");
10468
10469 // rev $0, $1
10470 if (AsmPieces.size() == 3 &&
10471 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
10472 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000010473 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000010474 if (Ty && Ty->getBitWidth() == 32)
10475 return IntrinsicLowering::LowerToByteSwap(CI);
10476 }
10477 break;
10478 }
10479
10480 return false;
10481}
10482
Evan Cheng10043e22007-01-19 07:51:42 +000010483/// getConstraintType - Given a constraint letter, return the type of
10484/// constraint it is for this target.
10485ARMTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +000010486ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
10487 if (Constraint.size() == 1) {
10488 switch (Constraint[0]) {
10489 default: break;
10490 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000010491 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000010492 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000010493 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000010494 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000010495 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000010496 // An address with a single base register. Due to the way we
10497 // currently handle addresses it is the same as an 'r' memory constraint.
10498 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000010499 }
Eric Christophere256cd02011-06-21 22:10:57 +000010500 } else if (Constraint.size() == 2) {
10501 switch (Constraint[0]) {
10502 default: break;
10503 // All 'U+' constraints are addresses.
10504 case 'U': return C_Memory;
10505 }
Evan Cheng10043e22007-01-19 07:51:42 +000010506 }
Chris Lattnerd6855142007-03-25 02:14:49 +000010507 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000010508}
10509
John Thompsone8360b72010-10-29 17:29:13 +000010510/// Examine constraint type and operand type and determine a weight value.
10511/// This object must already have been set up with the operand type
10512/// and the current alternative constraint selected.
10513TargetLowering::ConstraintWeight
10514ARMTargetLowering::getSingleConstraintMatchWeight(
10515 AsmOperandInfo &info, const char *constraint) const {
10516 ConstraintWeight weight = CW_Invalid;
10517 Value *CallOperandVal = info.CallOperandVal;
10518 // If we don't have a value, we can't do a match,
10519 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000010520 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000010521 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000010522 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000010523 // Look at the constraint type.
10524 switch (*constraint) {
10525 default:
10526 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10527 break;
10528 case 'l':
10529 if (type->isIntegerTy()) {
10530 if (Subtarget->isThumb())
10531 weight = CW_SpecificReg;
10532 else
10533 weight = CW_Register;
10534 }
10535 break;
10536 case 'w':
10537 if (type->isFloatingPointTy())
10538 weight = CW_Register;
10539 break;
10540 }
10541 return weight;
10542}
10543
Eric Christophercf2007c2011-06-30 23:50:52 +000010544typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
10545RCPair
Evan Cheng10043e22007-01-19 07:51:42 +000010546ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +000010547 MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000010548 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010549 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000010550 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000010551 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010552 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010553 return RCPair(0U, &ARM::tGPRRegClass);
10554 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000010555 case 'h': // High regs or no regs.
10556 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010557 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000010558 break;
Chris Lattner6223e832007-04-02 17:24:08 +000010559 case 'r':
Craig Topperc7242e02012-04-20 07:30:17 +000010560 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010561 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000010562 if (VT == MVT::Other)
10563 break;
Owen Anderson9f944592009-08-11 20:47:22 +000010564 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010565 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000010566 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010567 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000010568 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010569 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010570 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010571 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000010572 if (VT == MVT::Other)
10573 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010574 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010575 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010576 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010577 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010578 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010579 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010580 break;
Eric Christopherc011d312011-07-01 00:30:46 +000010581 case 't':
10582 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010583 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000010584 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010585 }
10586 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000010587 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000010588 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000010589
Evan Cheng10043e22007-01-19 07:51:42 +000010590 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
10591}
10592
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010593/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10594/// vector. If it is invalid, don't add anything to Ops.
10595void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000010596 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010597 std::vector<SDValue>&Ops,
10598 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000010599 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010600
Eric Christopherde9399b2011-06-02 23:16:42 +000010601 // Currently only support length 1 constraints.
10602 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000010603
Eric Christopherde9399b2011-06-02 23:16:42 +000010604 char ConstraintLetter = Constraint[0];
10605 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010606 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000010607 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010608 case 'I': case 'J': case 'K': case 'L':
10609 case 'M': case 'N': case 'O':
10610 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10611 if (!C)
10612 return;
10613
10614 int64_t CVal64 = C->getSExtValue();
10615 int CVal = (int) CVal64;
10616 // None of these constraints allow values larger than 32 bits. Check
10617 // that the value fits in an int.
10618 if (CVal != CVal64)
10619 return;
10620
Eric Christopherde9399b2011-06-02 23:16:42 +000010621 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000010622 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000010623 // Constant suitable for movw, must be between 0 and
10624 // 65535.
10625 if (Subtarget->hasV6T2Ops())
10626 if (CVal >= 0 && CVal <= 65535)
10627 break;
10628 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010629 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000010630 if (Subtarget->isThumb1Only()) {
10631 // This must be a constant between 0 and 255, for ADD
10632 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010633 if (CVal >= 0 && CVal <= 255)
10634 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010635 } else if (Subtarget->isThumb2()) {
10636 // A constant that can be used as an immediate value in a
10637 // data-processing instruction.
10638 if (ARM_AM::getT2SOImmVal(CVal) != -1)
10639 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010640 } else {
10641 // A constant that can be used as an immediate value in a
10642 // data-processing instruction.
10643 if (ARM_AM::getSOImmVal(CVal) != -1)
10644 break;
10645 }
10646 return;
10647
10648 case 'J':
David Goodwin22c2fba2009-07-08 23:10:31 +000010649 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010650 // This must be a constant between -255 and -1, for negated ADD
10651 // immediates. This can be used in GCC with an "n" modifier that
10652 // prints the negated value, for use with SUB instructions. It is
10653 // not useful otherwise but is implemented for compatibility.
10654 if (CVal >= -255 && CVal <= -1)
10655 break;
10656 } else {
10657 // This must be a constant between -4095 and 4095. It is not clear
10658 // what this constraint is intended for. Implemented for
10659 // compatibility with GCC.
10660 if (CVal >= -4095 && CVal <= 4095)
10661 break;
10662 }
10663 return;
10664
10665 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000010666 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010667 // A 32-bit value where only one byte has a nonzero value. Exclude
10668 // zero to match GCC. This constraint is used by GCC internally for
10669 // constants that can be loaded with a move/shift combination.
10670 // It is not useful otherwise but is implemented for compatibility.
10671 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
10672 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010673 } else if (Subtarget->isThumb2()) {
10674 // A constant whose bitwise inverse can be used as an immediate
10675 // value in a data-processing instruction. This can be used in GCC
10676 // with a "B" modifier that prints the inverted value, for use with
10677 // BIC and MVN instructions. It is not useful otherwise but is
10678 // implemented for compatibility.
10679 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
10680 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010681 } else {
10682 // A constant whose bitwise inverse can be used as an immediate
10683 // value in a data-processing instruction. This can be used in GCC
10684 // with a "B" modifier that prints the inverted value, for use with
10685 // BIC and MVN instructions. It is not useful otherwise but is
10686 // implemented for compatibility.
10687 if (ARM_AM::getSOImmVal(~CVal) != -1)
10688 break;
10689 }
10690 return;
10691
10692 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000010693 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010694 // This must be a constant between -7 and 7,
10695 // for 3-operand ADD/SUB immediate instructions.
10696 if (CVal >= -7 && CVal < 7)
10697 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010698 } else if (Subtarget->isThumb2()) {
10699 // A constant whose negation can be used as an immediate value in a
10700 // data-processing instruction. This can be used in GCC with an "n"
10701 // modifier that prints the negated value, for use with SUB
10702 // instructions. It is not useful otherwise but is implemented for
10703 // compatibility.
10704 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
10705 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010706 } else {
10707 // A constant whose negation can be used as an immediate value in a
10708 // data-processing instruction. This can be used in GCC with an "n"
10709 // modifier that prints the negated value, for use with SUB
10710 // instructions. It is not useful otherwise but is implemented for
10711 // compatibility.
10712 if (ARM_AM::getSOImmVal(-CVal) != -1)
10713 break;
10714 }
10715 return;
10716
10717 case 'M':
David Goodwin22c2fba2009-07-08 23:10:31 +000010718 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010719 // This must be a multiple of 4 between 0 and 1020, for
10720 // ADD sp + immediate.
10721 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
10722 break;
10723 } else {
10724 // A power of two or a constant between 0 and 32. This is used in
10725 // GCC for the shift amount on shifted register operands, but it is
10726 // useful in general for any shift amounts.
10727 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
10728 break;
10729 }
10730 return;
10731
10732 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000010733 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010734 // This must be a constant between 0 and 31, for shift amounts.
10735 if (CVal >= 0 && CVal <= 31)
10736 break;
10737 }
10738 return;
10739
10740 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000010741 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010742 // This must be a multiple of 4 between -508 and 508, for
10743 // ADD/SUB sp = sp + immediate.
10744 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
10745 break;
10746 }
10747 return;
10748 }
10749 Result = DAG.getTargetConstant(CVal, Op.getValueType());
10750 break;
10751 }
10752
10753 if (Result.getNode()) {
10754 Ops.push_back(Result);
10755 return;
10756 }
Dale Johannesence97d552010-06-25 21:55:36 +000010757 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010758}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010759
Renato Golin87610692013-07-16 09:32:17 +000010760SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
10761 assert(Subtarget->isTargetAEABI() && "Register-based DivRem lowering only");
10762 unsigned Opcode = Op->getOpcode();
10763 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
Saleem Abdulrasool740be892014-08-17 22:50:59 +000010764 "Invalid opcode for Div/Rem lowering");
Renato Golin87610692013-07-16 09:32:17 +000010765 bool isSigned = (Opcode == ISD::SDIVREM);
10766 EVT VT = Op->getValueType(0);
10767 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
10768
10769 RTLIB::Libcall LC;
10770 switch (VT.getSimpleVT().SimpleTy) {
10771 default: llvm_unreachable("Unexpected request for libcall!");
Saleem Abdulrasool740be892014-08-17 22:50:59 +000010772 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
10773 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
10774 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
10775 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
Renato Golin87610692013-07-16 09:32:17 +000010776 }
10777
10778 SDValue InChain = DAG.getEntryNode();
10779
10780 TargetLowering::ArgListTy Args;
10781 TargetLowering::ArgListEntry Entry;
10782 for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) {
10783 EVT ArgVT = Op->getOperand(i).getValueType();
10784 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
10785 Entry.Node = Op->getOperand(i);
10786 Entry.Ty = ArgTy;
10787 Entry.isSExt = isSigned;
10788 Entry.isZExt = !isSigned;
10789 Args.push_back(Entry);
10790 }
10791
10792 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
10793 getPointerTy());
10794
10795 Type *RetTy = (Type*)StructType::get(Ty, Ty, NULL);
10796
10797 SDLoc dl(Op);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010798 TargetLowering::CallLoweringInfo CLI(DAG);
10799 CLI.setDebugLoc(dl).setChain(InChain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +000010800 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010801 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000010802
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010803 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000010804 return CallInfo.first;
10805}
10806
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000010807SDValue
10808ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
10809 assert(Subtarget->isTargetWindows() && "unsupported target platform");
10810 SDLoc DL(Op);
10811
10812 // Get the inputs.
10813 SDValue Chain = Op.getOperand(0);
10814 SDValue Size = Op.getOperand(1);
10815
10816 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
10817 DAG.getConstant(2, MVT::i32));
10818
10819 SDValue Flag;
10820 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
10821 Flag = Chain.getValue(1);
10822
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +000010823 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000010824 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
10825
10826 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
10827 Chain = NewSP.getValue(1);
10828
10829 SDValue Ops[2] = { NewSP, Chain };
10830 return DAG.getMergeValues(Ops, DL);
10831}
10832
Oliver Stannard51b1d462014-08-21 12:50:31 +000010833SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const {
10834 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() &&
10835 "Unexpected type for custom-lowering FP_EXTEND");
10836
10837 RTLIB::Libcall LC;
10838 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType());
10839
10840 SDValue SrcVal = Op.getOperand(0);
10841 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1,
10842 /*isSigned*/ false, SDLoc(Op)).first;
10843}
10844
10845SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
10846 assert(Op.getOperand(0).getValueType() == MVT::f64 &&
10847 Subtarget->isFPOnlySP() &&
10848 "Unexpected type for custom-lowering FP_ROUND");
10849
10850 RTLIB::Libcall LC;
10851 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType());
10852
10853 SDValue SrcVal = Op.getOperand(0);
10854 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1,
10855 /*isSigned*/ false, SDLoc(Op)).first;
10856}
10857
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010858bool
10859ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
10860 // The ARM target isn't yet aware of offsets.
10861 return false;
10862}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010863
Jim Grosbach11013ed2010-07-16 23:05:05 +000010864bool ARM::isBitFieldInvertedMask(unsigned v) {
10865 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010866 return false;
10867
Jim Grosbach11013ed2010-07-16 23:05:05 +000010868 // there can be 1's on either or both "outsides", all the "inside"
10869 // bits must be 0's
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010870 unsigned TO = CountTrailingOnes_32(v);
10871 unsigned LO = CountLeadingOnes_32(v);
10872 v = (v >> TO) << TO;
10873 v = (v << LO) >> LO;
10874 return v == 0;
Jim Grosbach11013ed2010-07-16 23:05:05 +000010875}
10876
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010877/// isFPImmLegal - Returns true if the target can instruction select the
10878/// specified FP immediate natively. If false, the legalizer will
10879/// materialize the FP immediate as a load from a constant pool.
10880bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
10881 if (!Subtarget->hasVFP3())
10882 return false;
10883 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000010884 return ARM_AM::getFP32Imm(Imm) != -1;
Oliver Stannard51b1d462014-08-21 12:50:31 +000010885 if (VT == MVT::f64 && !Subtarget->isFPOnlySP())
Jim Grosbachefc761a2011-09-30 00:50:06 +000010886 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010887 return false;
10888}
Bob Wilson5549d492010-09-21 17:56:22 +000010889
Wesley Peck527da1b2010-11-23 03:31:01 +000010890/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000010891/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
10892/// specified in the intrinsic calls.
10893bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
10894 const CallInst &I,
10895 unsigned Intrinsic) const {
10896 switch (Intrinsic) {
10897 case Intrinsic::arm_neon_vld1:
10898 case Intrinsic::arm_neon_vld2:
10899 case Intrinsic::arm_neon_vld3:
10900 case Intrinsic::arm_neon_vld4:
10901 case Intrinsic::arm_neon_vld2lane:
10902 case Intrinsic::arm_neon_vld3lane:
10903 case Intrinsic::arm_neon_vld4lane: {
10904 Info.opc = ISD::INTRINSIC_W_CHAIN;
10905 // Conservatively set memVT to the entire set of vectors loaded.
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010906 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010907 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10908 Info.ptrVal = I.getArgOperand(0);
10909 Info.offset = 0;
10910 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10911 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10912 Info.vol = false; // volatile loads with NEON intrinsics not supported
10913 Info.readMem = true;
10914 Info.writeMem = false;
10915 return true;
10916 }
10917 case Intrinsic::arm_neon_vst1:
10918 case Intrinsic::arm_neon_vst2:
10919 case Intrinsic::arm_neon_vst3:
10920 case Intrinsic::arm_neon_vst4:
10921 case Intrinsic::arm_neon_vst2lane:
10922 case Intrinsic::arm_neon_vst3lane:
10923 case Intrinsic::arm_neon_vst4lane: {
10924 Info.opc = ISD::INTRINSIC_VOID;
10925 // Conservatively set memVT to the entire set of vectors stored.
10926 unsigned NumElts = 0;
10927 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000010928 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000010929 if (!ArgTy->isVectorTy())
10930 break;
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010931 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010932 }
10933 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10934 Info.ptrVal = I.getArgOperand(0);
10935 Info.offset = 0;
10936 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10937 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10938 Info.vol = false; // volatile stores with NEON intrinsics not supported
10939 Info.readMem = false;
10940 Info.writeMem = true;
10941 return true;
10942 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010943 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000010944 case Intrinsic::arm_ldrex: {
10945 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
10946 Info.opc = ISD::INTRINSIC_W_CHAIN;
10947 Info.memVT = MVT::getVT(PtrTy->getElementType());
10948 Info.ptrVal = I.getArgOperand(0);
10949 Info.offset = 0;
10950 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10951 Info.vol = true;
10952 Info.readMem = true;
10953 Info.writeMem = false;
10954 return true;
10955 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010956 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000010957 case Intrinsic::arm_strex: {
10958 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
10959 Info.opc = ISD::INTRINSIC_W_CHAIN;
10960 Info.memVT = MVT::getVT(PtrTy->getElementType());
10961 Info.ptrVal = I.getArgOperand(1);
10962 Info.offset = 0;
10963 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10964 Info.vol = true;
10965 Info.readMem = false;
10966 Info.writeMem = true;
10967 return true;
10968 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010969 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010970 case Intrinsic::arm_strexd: {
10971 Info.opc = ISD::INTRINSIC_W_CHAIN;
10972 Info.memVT = MVT::i64;
10973 Info.ptrVal = I.getArgOperand(2);
10974 Info.offset = 0;
10975 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000010976 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010977 Info.readMem = false;
10978 Info.writeMem = true;
10979 return true;
10980 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010981 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010982 case Intrinsic::arm_ldrexd: {
10983 Info.opc = ISD::INTRINSIC_W_CHAIN;
10984 Info.memVT = MVT::i64;
10985 Info.ptrVal = I.getArgOperand(0);
10986 Info.offset = 0;
10987 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000010988 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010989 Info.readMem = true;
10990 Info.writeMem = false;
10991 return true;
10992 }
Bob Wilson5549d492010-09-21 17:56:22 +000010993 default:
10994 break;
10995 }
10996
10997 return false;
10998}
Juergen Ributzka659ce002014-01-28 01:20:14 +000010999
11000/// \brief Returns true if it is beneficial to convert a load of a constant
11001/// to just the constant itself.
11002bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
11003 Type *Ty) const {
11004 assert(Ty->isIntegerTy());
11005
11006 unsigned Bits = Ty->getPrimitiveSizeInBits();
11007 if (Bits == 0 || Bits > 32)
11008 return false;
11009 return true;
11010}
Tim Northover037f26f22014-04-17 18:22:47 +000011011
Robin Morisset25c8e312014-09-17 00:06:58 +000011012bool ARMTargetLowering::hasLoadLinkedStoreConditional() const { return true; }
11013
Robin Morisset5349e8e2014-09-18 18:56:04 +000011014Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder,
11015 ARM_MB::MemBOpt Domain) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000011016 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morisset5349e8e2014-09-18 18:56:04 +000011017
11018 // First, if the target has no DMB, see what fallback we can use.
11019 if (!Subtarget->hasDataBarrier()) {
11020 // Some ARMv6 cpus can support data barriers with an mcr instruction.
11021 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
11022 // here.
11023 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) {
11024 Function *MCR = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_mcr);
11025 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0),
11026 Builder.getInt32(0), Builder.getInt32(7),
11027 Builder.getInt32(10), Builder.getInt32(5)};
11028 return Builder.CreateCall(MCR, args);
11029 } else {
11030 // Instead of using barriers, atomic accesses on these subtargets use
11031 // libcalls.
11032 llvm_unreachable("makeDMB on a target so old that it has no barriers");
11033 }
11034 } else {
11035 Function *DMB = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_dmb);
11036 // Only a full system barrier exists in the M-class architectures.
11037 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain;
11038 Constant *CDomain = Builder.getInt32(Domain);
11039 return Builder.CreateCall(DMB, CDomain);
11040 }
Robin Morisseta47cb412014-09-03 21:01:03 +000011041}
11042
11043// Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
Robin Morissetdedef332014-09-23 20:31:14 +000011044Instruction* ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011045 AtomicOrdering Ord, bool IsStore,
11046 bool IsLoad) const {
11047 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011048 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011049
11050 switch (Ord) {
11051 case NotAtomic:
11052 case Unordered:
11053 llvm_unreachable("Invalid fence: unordered/non-atomic");
11054 case Monotonic:
11055 case Acquire:
Robin Morissetdedef332014-09-23 20:31:14 +000011056 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011057 case SequentiallyConsistent:
11058 if (!IsStore)
Robin Morissetdedef332014-09-23 20:31:14 +000011059 return nullptr; // Nothing to do
11060 /*FALLTHROUGH*/
Robin Morisseta47cb412014-09-03 21:01:03 +000011061 case Release:
11062 case AcquireRelease:
11063 if (Subtarget->isSwift())
Robin Morissetdedef332014-09-23 20:31:14 +000011064 return makeDMB(Builder, ARM_MB::ISHST);
Robin Morisseta47cb412014-09-03 21:01:03 +000011065 // FIXME: add a comment with a link to documentation justifying this.
11066 else
Robin Morissetdedef332014-09-23 20:31:14 +000011067 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011068 }
Robin Morissetdedef332014-09-23 20:31:14 +000011069 llvm_unreachable("Unknown fence ordering in emitLeadingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011070}
11071
Robin Morissetdedef332014-09-23 20:31:14 +000011072Instruction* ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011073 AtomicOrdering Ord, bool IsStore,
11074 bool IsLoad) const {
11075 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011076 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011077
11078 switch (Ord) {
11079 case NotAtomic:
11080 case Unordered:
11081 llvm_unreachable("Invalid fence: unordered/not-atomic");
11082 case Monotonic:
11083 case Release:
Robin Morissetdedef332014-09-23 20:31:14 +000011084 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011085 case Acquire:
11086 case AcquireRelease:
Robin Morissetdedef332014-09-23 20:31:14 +000011087 case SequentiallyConsistent:
11088 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011089 }
Robin Morissetdedef332014-09-23 20:31:14 +000011090 llvm_unreachable("Unknown fence ordering in emitTrailingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011091}
11092
Robin Morisseted3d48f2014-09-03 21:29:59 +000011093// Loads and stores less than 64-bits are already atomic; ones above that
11094// are doomed anyway, so defer to the default libcall and blame the OS when
11095// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11096// anything for those.
11097bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
11098 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
11099 return (Size == 64) && !Subtarget->isMClass();
11100}
Tim Northover037f26f22014-04-17 18:22:47 +000011101
Robin Morisseted3d48f2014-09-03 21:29:59 +000011102// Loads and stores less than 64-bits are already atomic; ones above that
11103// are doomed anyway, so defer to the default libcall and blame the OS when
11104// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11105// anything for those.
Robin Morisseta7b357f2014-09-23 18:33:21 +000011106// FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that
11107// guarantee, see DDI0406C ARM architecture reference manual,
11108// sections A8.8.72-74 LDRD)
Robin Morisseted3d48f2014-09-03 21:29:59 +000011109bool ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
11110 unsigned Size = LI->getType()->getPrimitiveSizeInBits();
11111 return (Size == 64) && !Subtarget->isMClass();
11112}
11113
11114// For the real atomic operations, we have ldrex/strex up to 32 bits,
11115// and up to 64 bits on the non-M profiles
11116bool ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
11117 unsigned Size = AI->getType()->getPrimitiveSizeInBits();
Aaron Ballman169eeb912014-09-04 11:52:24 +000011118 return Size <= (Subtarget->isMClass() ? 32U : 64U);
Tim Northover037f26f22014-04-17 18:22:47 +000011119}
11120
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011121// This has so far only been implemented for MachO.
11122bool ARMTargetLowering::useLoadStackGuardNode() const {
11123 return Subtarget->getTargetTriple().getObjectFormat() == Triple::MachO;
11124}
11125
Quentin Colombetc32615d2014-10-31 17:52:53 +000011126bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
11127 unsigned &Cost) const {
11128 // If we do not have NEON, vector types are not natively supported.
11129 if (!Subtarget->hasNEON())
11130 return false;
11131
11132 // Floating point values and vector values map to the same register file.
11133 // Therefore, althought we could do a store extract of a vector type, this is
11134 // better to leave at float as we have more freedom in the addressing mode for
11135 // those.
11136 if (VectorTy->isFPOrFPVectorTy())
11137 return false;
11138
11139 // If the index is unknown at compile time, this is very expensive to lower
11140 // and it is not possible to combine the store with the extract.
11141 if (!isa<ConstantInt>(Idx))
11142 return false;
11143
11144 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type");
11145 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth();
11146 // We can do a store + vector extract on any vector that fits perfectly in a D
11147 // or Q register.
11148 if (BitWidth == 64 || BitWidth == 128) {
11149 Cost = 0;
11150 return true;
11151 }
11152 return false;
11153}
11154
Tim Northover037f26f22014-04-17 18:22:47 +000011155Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
11156 AtomicOrdering Ord) const {
11157 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
11158 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
Robin Morissetb155f522014-08-18 16:48:58 +000011159 bool IsAcquire = isAtLeastAcquire(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011160
11161 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
11162 // intrinsic must return {i32, i32} and we have to recombine them into a
11163 // single i64 here.
11164 if (ValTy->getPrimitiveSizeInBits() == 64) {
11165 Intrinsic::ID Int =
11166 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
11167 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
11168
11169 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
11170 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
11171
11172 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
11173 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011174 if (!Subtarget->isLittle())
11175 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011176 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
11177 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
11178 return Builder.CreateOr(
11179 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
11180 }
11181
11182 Type *Tys[] = { Addr->getType() };
11183 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
11184 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
11185
11186 return Builder.CreateTruncOrBitCast(
11187 Builder.CreateCall(Ldrex, Addr),
11188 cast<PointerType>(Addr->getType())->getElementType());
11189}
11190
11191Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
11192 Value *Addr,
11193 AtomicOrdering Ord) const {
11194 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morissetb155f522014-08-18 16:48:58 +000011195 bool IsRelease = isAtLeastRelease(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011196
11197 // Since the intrinsics must have legal type, the i64 intrinsics take two
11198 // parameters: "i32, i32". We must marshal Val into the appropriate form
11199 // before the call.
11200 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
11201 Intrinsic::ID Int =
11202 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
11203 Function *Strex = Intrinsic::getDeclaration(M, Int);
11204 Type *Int32Ty = Type::getInt32Ty(M->getContext());
11205
11206 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
11207 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011208 if (!Subtarget->isLittle())
11209 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011210 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
11211 return Builder.CreateCall3(Strex, Lo, Hi, Addr);
11212 }
11213
11214 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
11215 Type *Tys[] = { Addr->getType() };
11216 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
11217
11218 return Builder.CreateCall2(
11219 Strex, Builder.CreateZExtOrBitCast(
11220 Val, Strex->getFunctionType()->getParamType(0)),
11221 Addr);
11222}
Oliver Stannardc24f2172014-05-09 14:01:47 +000011223
11224enum HABaseType {
11225 HA_UNKNOWN = 0,
11226 HA_FLOAT,
11227 HA_DOUBLE,
11228 HA_VECT64,
11229 HA_VECT128
11230};
11231
11232static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
11233 uint64_t &Members) {
11234 if (const StructType *ST = dyn_cast<StructType>(Ty)) {
11235 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
11236 uint64_t SubMembers = 0;
11237 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
11238 return false;
11239 Members += SubMembers;
11240 }
11241 } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
11242 uint64_t SubMembers = 0;
11243 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
11244 return false;
11245 Members += SubMembers * AT->getNumElements();
11246 } else if (Ty->isFloatTy()) {
11247 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
11248 return false;
11249 Members = 1;
11250 Base = HA_FLOAT;
11251 } else if (Ty->isDoubleTy()) {
11252 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
11253 return false;
11254 Members = 1;
11255 Base = HA_DOUBLE;
11256 } else if (const VectorType *VT = dyn_cast<VectorType>(Ty)) {
11257 Members = 1;
11258 switch (Base) {
11259 case HA_FLOAT:
11260 case HA_DOUBLE:
11261 return false;
11262 case HA_VECT64:
11263 return VT->getBitWidth() == 64;
11264 case HA_VECT128:
11265 return VT->getBitWidth() == 128;
11266 case HA_UNKNOWN:
11267 switch (VT->getBitWidth()) {
11268 case 64:
11269 Base = HA_VECT64;
11270 return true;
11271 case 128:
11272 Base = HA_VECT128;
11273 return true;
11274 default:
11275 return false;
11276 }
11277 }
11278 }
11279
11280 return (Members > 0 && Members <= 4);
11281}
11282
11283/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate.
11284bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
11285 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000011286 if (getEffectiveCallingConv(CallConv, isVarArg) !=
11287 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000011288 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000011289
11290 HABaseType Base = HA_UNKNOWN;
11291 uint64_t Members = 0;
11292 bool result = isHomogeneousAggregate(Ty, Base, Members);
Justin Bognerc0087f32014-08-12 03:24:59 +000011293 DEBUG(dbgs() << "isHA: " << result << " "; Ty->dump());
Tim Northover4f1909f2014-05-27 10:43:38 +000011294 return result;
Oliver Stannardc24f2172014-05-09 14:01:47 +000011295}