blob: bdd547fe286d83bb98dacd233188615050af3e42 [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
Dale Johannesend679ff72010-06-03 21:09:53 +000015#define DEBUG_TYPE "arm-isel"
Craig Topper188ed9d2012-03-17 07:33:42 +000016#include "ARMISelLowering.h"
Eric Christopher1c069172010-09-10 22:42:06 +000017#include "ARMCallingConv.h"
Evan Cheng10043e22007-01-19 07:51:42 +000018#include "ARMConstantPoolValue.h"
Evan Cheng10043e22007-01-19 07:51:42 +000019#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov9a232f42009-08-21 12:41:24 +000020#include "ARMPerfectShuffle.h"
Evan Cheng10043e22007-01-19 07:51:42 +000021#include "ARMSubtarget.h"
22#include "ARMTargetMachine.h"
Chris Lattner4e7dfaf2009-08-02 00:34:36 +000023#include "ARMTargetObjectFile.h"
Evan Chenga20cde32011-07-20 23:34:39 +000024#include "MCTargetDesc/ARMAddressingModes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/ADT/Statistic.h"
26#include "llvm/ADT/StringExtras.h"
Bob Wilsona4c22902009-04-17 19:07:39 +000027#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng078b0b02011-01-08 01:24:27 +000028#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Cheng10043e22007-01-19 07:51:42 +000029#include "llvm/CodeGen/MachineBasicBlock.h"
30#include "llvm/CodeGen/MachineFrameInfo.h"
31#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.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"
Torok Edwin6dd27302009-07-08 18:01:40 +000047#include "llvm/Support/ErrorHandling.h"
Evan Cheng2150b922007-03-12 23:30:29 +000048#include "llvm/Support/MathExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000049#include "llvm/Target/TargetOptions.h"
David Peixottoc32e24a2013-10-17 19:49:22 +000050#include <utility>
Evan Cheng10043e22007-01-19 07:51:42 +000051using namespace llvm;
52
Dale Johannesend679ff72010-06-03 21:09:53 +000053STATISTIC(NumTailCalls, "Number of tail calls");
Evan Cheng68aec142011-01-19 02:16:49 +000054STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Manman Ren9f911162012-06-01 02:44:42 +000055STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
Dale Johannesend679ff72010-06-03 21:09:53 +000056
Eric Christopher347f4c32010-12-15 23:47:29 +000057cl::opt<bool>
Jim Grosbach32bb3622010-04-14 22:28:31 +000058EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng25f93642010-07-08 02:08:50 +000059 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbach32bb3622010-04-14 22:28:31 +000060 cl::init(false));
61
Evan Chengf128bdc2010-06-16 07:35:02 +000062static cl::opt<bool>
63ARMInterworking("arm-interworking", cl::Hidden,
64 cl::desc("Enable / disable ARM interworking (for debugging only)"),
65 cl::init(true));
66
Benjamin Kramer7ba71be2011-11-26 23:01:57 +000067namespace {
Cameron Zwarich89019782011-06-10 20:59:24 +000068 class ARMCCState : public CCState {
69 public:
70 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
Craig Topperb94011f2013-07-14 04:42:23 +000071 const TargetMachine &TM, SmallVectorImpl<CCValAssign> &locs,
Cameron Zwarich89019782011-06-10 20:59:24 +000072 LLVMContext &C, ParmContext PC)
73 : CCState(CC, isVarArg, MF, TM, locs, C) {
74 assert(((PC == Call) || (PC == Prologue)) &&
75 "ARMCCState users must specify whether their context is call"
76 "or prologue generation.");
77 CallOrPrologue = PC;
78 }
79 };
80}
81
Stuart Hastings45fe3c32011-04-20 16:47:52 +000082// The APCS parameter registers.
Craig Topper840beec2014-04-04 05:16:06 +000083static const MCPhysReg GPRArgRegs[] = {
Stuart Hastings45fe3c32011-04-20 16:47:52 +000084 ARM::R0, ARM::R1, ARM::R2, ARM::R3
85};
86
Craig Topper4fa625f2012-08-12 03:16:37 +000087void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT,
88 MVT PromotedBitwiseVT) {
Bob Wilson2e076c42009-06-22 23:27:02 +000089 if (VT != PromotedLdStVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +000090 setOperationAction(ISD::LOAD, VT, Promote);
91 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000092
Craig Topper4fa625f2012-08-12 03:16:37 +000093 setOperationAction(ISD::STORE, VT, Promote);
94 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000095 }
96
Craig Topper4fa625f2012-08-12 03:16:37 +000097 MVT ElemTy = VT.getVectorElementType();
Owen Anderson9f944592009-08-11 20:47:22 +000098 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Craig Topper4fa625f2012-08-12 03:16:37 +000099 setOperationAction(ISD::SETCC, VT, Custom);
100 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
101 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000102 if (ElemTy == MVT::i32) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000103 setOperationAction(ISD::SINT_TO_FP, VT, Custom);
104 setOperationAction(ISD::UINT_TO_FP, VT, Custom);
105 setOperationAction(ISD::FP_TO_SINT, VT, Custom);
106 setOperationAction(ISD::FP_TO_UINT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000107 } else {
Craig Topper4fa625f2012-08-12 03:16:37 +0000108 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
109 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
110 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
111 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Bob Wilson5d8cfb22009-09-16 20:20:44 +0000112 }
Craig Topper4fa625f2012-08-12 03:16:37 +0000113 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
114 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
115 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal);
116 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
117 setOperationAction(ISD::SELECT, VT, Expand);
118 setOperationAction(ISD::SELECT_CC, VT, Expand);
Jim Grosbach30af4422012-10-12 22:59:21 +0000119 setOperationAction(ISD::VSELECT, VT, Expand);
Craig Topper4fa625f2012-08-12 03:16:37 +0000120 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000121 if (VT.isInteger()) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000122 setOperationAction(ISD::SHL, VT, Custom);
123 setOperationAction(ISD::SRA, VT, Custom);
124 setOperationAction(ISD::SRL, VT, Custom);
Bob Wilson2e076c42009-06-22 23:27:02 +0000125 }
126
127 // Promote all bit-wise operations.
128 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000129 setOperationAction(ISD::AND, VT, Promote);
130 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
131 setOperationAction(ISD::OR, VT, Promote);
132 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT);
133 setOperationAction(ISD::XOR, VT, Promote);
134 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
Bob Wilson2e076c42009-06-22 23:27:02 +0000135 }
Bob Wilson4ed397c2009-09-16 00:17:28 +0000136
137 // Neon does not support vector divide/remainder operations.
Craig Topper4fa625f2012-08-12 03:16:37 +0000138 setOperationAction(ISD::SDIV, VT, Expand);
139 setOperationAction(ISD::UDIV, VT, Expand);
140 setOperationAction(ISD::FDIV, VT, Expand);
141 setOperationAction(ISD::SREM, VT, Expand);
142 setOperationAction(ISD::UREM, VT, Expand);
143 setOperationAction(ISD::FREM, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000144}
145
Craig Topper4fa625f2012-08-12 03:16:37 +0000146void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topperc7242e02012-04-20 07:30:17 +0000147 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000148 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000149}
150
Craig Topper4fa625f2012-08-12 03:16:37 +0000151void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Jakob Stoklund Olesen20912062014-01-14 06:18:34 +0000152 addRegisterClass(VT, &ARM::DPairRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000153 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000154}
155
Chris Lattner5e693ed2009-07-28 03:13:23 +0000156static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
Tim Northoverd6a729b2014-01-06 14:28:05 +0000157 if (TM.getSubtarget<ARMSubtarget>().isTargetMachO())
Bill Wendlingbbcaa402010-03-15 21:09:38 +0000158 return new TargetLoweringObjectFileMachO();
Bill Wendling46ffefc2010-03-09 02:46:12 +0000159
Chris Lattner4e7dfaf2009-08-02 00:34:36 +0000160 return new ARMElfTargetObjectFile();
Chris Lattner5e693ed2009-07-28 03:13:23 +0000161}
162
Evan Cheng10043e22007-01-19 07:51:42 +0000163ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Cheng408aa562009-11-06 22:24:13 +0000164 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng10043e22007-01-19 07:51:42 +0000165 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Chengdf907f42010-07-23 22:39:59 +0000166 RegInfo = TM.getRegisterInfo();
Evan Chengbf407072010-09-10 01:29:16 +0000167 Itins = TM.getInstrItineraryData();
Evan Cheng10043e22007-01-19 07:51:42 +0000168
Duncan Sandsf2641e12011-09-06 19:07:46 +0000169 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
170
Tim Northoverd6a729b2014-01-06 14:28:05 +0000171 if (Subtarget->isTargetMachO()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000172 // Uses VFP for Thumb libfuncs if available.
Jim Grosbach1d1d6d42013-10-24 23:07:11 +0000173 if (Subtarget->isThumb() && Subtarget->hasVFP2() &&
174 Subtarget->hasARMOps()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000175 // Single-precision floating-point arithmetic.
176 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
177 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
178 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
179 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000180
Evan Chengc9f22fd12007-04-27 08:15:43 +0000181 // Double-precision floating-point arithmetic.
182 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
183 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
184 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
185 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng143576d2007-01-31 09:30:58 +0000186
Evan Chengc9f22fd12007-04-27 08:15:43 +0000187 // Single-precision comparisons.
188 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
189 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
190 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
191 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
192 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
193 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
194 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
195 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000196
Evan Chengc9f22fd12007-04-27 08:15:43 +0000197 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
198 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
199 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
200 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
201 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
202 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
203 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
204 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng143576d2007-01-31 09:30:58 +0000205
Evan Chengc9f22fd12007-04-27 08:15:43 +0000206 // Double-precision comparisons.
207 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
208 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
209 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
210 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
211 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
212 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
213 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
214 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000215
Evan Chengc9f22fd12007-04-27 08:15:43 +0000216 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
217 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
218 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
219 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
220 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
221 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
222 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
223 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Cheng10043e22007-01-19 07:51:42 +0000224
Evan Chengc9f22fd12007-04-27 08:15:43 +0000225 // Floating-point to integer conversions.
226 // i64 conversions are done via library routines even when generating VFP
227 // instructions, so use the same ones.
228 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
229 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
230 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
231 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000232
Evan Chengc9f22fd12007-04-27 08:15:43 +0000233 // Conversions between floating types.
234 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
235 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
236
237 // Integer to floating-point conversions.
238 // i64 conversions are done via library routines even when generating VFP
239 // instructions, so use the same ones.
Bob Wilsondc40d5a2009-03-20 23:16:43 +0000240 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
241 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengc9f22fd12007-04-27 08:15:43 +0000242 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
243 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
244 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
245 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
246 }
Evan Cheng10043e22007-01-19 07:51:42 +0000247 }
248
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000249 // These libcalls are not available in 32-bit.
250 setLibcallName(RTLIB::SHL_I128, 0);
251 setLibcallName(RTLIB::SRL_I128, 0);
252 setLibcallName(RTLIB::SRA_I128, 0);
253
Saleem Abdulrasoolcd130822014-04-02 20:32:05 +0000254 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetMachO() &&
255 !Subtarget->isTargetWindows()) {
Wesley Peck527da1b2010-11-23 03:31:01 +0000256 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000257 // RTABI chapter 4.1.2, Table 2
258 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
259 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
260 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
261 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
262 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
263 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
264 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
265 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
266
267 // Double-precision floating-point comparison helper functions
268 // RTABI chapter 4.1.2, Table 3
269 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
270 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
271 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
272 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
273 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
274 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
275 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
276 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
277 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
278 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
279 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
280 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
281 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
282 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
283 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
284 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
285 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
286 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
287 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
288 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
289 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
290 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
291 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
292 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
293
294 // Single-precision floating-point arithmetic helper functions
295 // RTABI chapter 4.1.2, Table 4
296 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
297 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
298 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
299 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
300 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
301 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
302 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
303 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
304
305 // Single-precision floating-point comparison helper functions
306 // RTABI chapter 4.1.2, Table 5
307 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
308 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
309 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
310 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
311 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
312 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
313 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
314 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
315 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
316 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
317 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
318 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
319 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
320 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
321 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
322 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
323 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
324 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
325 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
326 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
327 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
328 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
329 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
330 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
331
332 // Floating-point to integer conversions.
333 // RTABI chapter 4.1.2, Table 6
334 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
335 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
336 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
337 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
338 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
339 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
340 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
341 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
342 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
343 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
344 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
345 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
346 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
347 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
348 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
349 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
350
351 // Conversions between floating types.
352 // RTABI chapter 4.1.2, Table 7
353 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
354 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
355 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peck527da1b2010-11-23 03:31:01 +0000356 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000357
358 // Integer to floating-point conversions.
359 // RTABI chapter 4.1.2, Table 8
360 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
361 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
362 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
363 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
364 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
365 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
366 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
367 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
368 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
369 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
370 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
371 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
372 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
373 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
374 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
375 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
376
377 // Long long helper functions
378 // RTABI chapter 4.2, Table 9
379 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000380 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
381 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
382 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
383 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
384 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
385 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
386 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
387 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
388 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
389
390 // Integer division functions
391 // RTABI chapter 4.3.1
392 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
393 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
394 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
Anton Korobeynikovd0c46552012-01-29 09:11:50 +0000395 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000396 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
397 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
398 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
Anton Korobeynikovd0c46552012-01-29 09:11:50 +0000399 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000400 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
401 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
402 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikovd0c46552012-01-29 09:11:50 +0000403 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000404 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
405 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peck527da1b2010-11-23 03:31:01 +0000406 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikovd0c46552012-01-29 09:11:50 +0000407 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
Renato Golin4cd51872011-05-22 21:41:23 +0000408
409 // Memory operations
410 // RTABI chapter 4.3.4
411 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
412 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
413 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikovd0c46552012-01-29 09:11:50 +0000414 setLibcallCallingConv(RTLIB::MEMCPY, CallingConv::ARM_AAPCS);
415 setLibcallCallingConv(RTLIB::MEMMOVE, CallingConv::ARM_AAPCS);
416 setLibcallCallingConv(RTLIB::MEMSET, CallingConv::ARM_AAPCS);
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +0000417 }
418
Bob Wilsonbc158992011-10-07 16:59:21 +0000419 // Use divmod compiler-rt calls for iOS 5.0 and later.
Cameron Esfahani943908b2013-08-29 20:23:14 +0000420 if (Subtarget->getTargetTriple().isiOS() &&
Bob Wilsonbc158992011-10-07 16:59:21 +0000421 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
422 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
423 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
424 }
425
David Goodwin22c2fba2009-07-08 23:10:31 +0000426 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000427 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000428 else
Craig Topperc7242e02012-04-20 07:30:17 +0000429 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000430 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
431 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000432 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Jim Grosbach4d5dc3e2010-08-11 15:44:15 +0000433 if (!Subtarget->isFPOnlySP())
Craig Topperc7242e02012-04-20 07:30:17 +0000434 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Bob Wilson7117a912009-03-20 22:42:55 +0000435
Owen Anderson9f944592009-08-11 20:47:22 +0000436 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000437 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000438
Eli Friedman6f84fed2011-11-08 01:43:53 +0000439 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
440 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
441 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
442 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
443 setTruncStoreAction((MVT::SimpleValueType)VT,
444 (MVT::SimpleValueType)InnerVT, Expand);
445 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
446 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
447 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
448 }
449
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000450 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000451 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000452
Bob Wilson2e076c42009-06-22 23:27:02 +0000453 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000454 addDRTypeForNEON(MVT::v2f32);
455 addDRTypeForNEON(MVT::v8i8);
456 addDRTypeForNEON(MVT::v4i16);
457 addDRTypeForNEON(MVT::v2i32);
458 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000459
Owen Anderson9f944592009-08-11 20:47:22 +0000460 addQRTypeForNEON(MVT::v4f32);
461 addQRTypeForNEON(MVT::v2f64);
462 addQRTypeForNEON(MVT::v16i8);
463 addQRTypeForNEON(MVT::v8i16);
464 addQRTypeForNEON(MVT::v4i32);
465 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000466
Bob Wilson194a2512009-09-15 23:55:57 +0000467 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
468 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000469 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
470 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000471 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
472 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
473 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000474 // FIXME: Code duplication: FDIV and FREM are expanded always, see
475 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000476 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
477 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000478 // FIXME: Create unittest.
479 // In another words, find a way when "copysign" appears in DAG with vector
480 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000481 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000482 // FIXME: Code duplication: SETCC has custom operation action, see
483 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000484 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000485 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000486 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
487 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
488 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
489 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
490 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
491 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
492 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
493 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
494 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
495 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
496 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
497 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000498 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000499 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
500 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
501 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
502 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
503 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000504 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000505
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000506 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
507 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
508 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
509 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
510 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
511 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
512 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
513 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
514 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
515 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000516 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
517 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
518 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
519 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000520 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000521
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000522 // Mark v2f32 intrinsics.
523 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
524 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
525 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
526 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
527 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
528 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
529 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
530 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
531 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
532 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
533 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
534 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
535 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
536 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
537 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
538
Bob Wilson6cc46572009-09-16 00:32:15 +0000539 // Neon does not support some operations on v1i64 and v2i64 types.
540 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000541 // Custom handling for some quad-vector types to detect VMULL.
542 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
543 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
544 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000545 // Custom handling for some vector types to avoid expensive expansions
546 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
547 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
548 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
549 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000550 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
551 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000552 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000553 // a destination type that is wider than the source, and nor does
554 // it have a FP_TO_[SU]INT instruction with a narrower destination than
555 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000556 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
557 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000558 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
559 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000560
Eli Friedmane6385e62012-11-15 22:44:27 +0000561 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000562 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000563
Evan Chengb4eae132012-12-04 22:41:50 +0000564 // NEON does not have single instruction CTPOP for vectors with element
565 // types wider than 8-bits. However, custom lowering can leverage the
566 // v8i8/v16i8 vcnt instruction.
567 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
568 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
569 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
570 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
571
Jim Grosbach5f215872013-02-27 21:31:12 +0000572 // NEON only has FMA instructions as of VFP4.
573 if (!Subtarget->hasVFP4()) {
574 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
575 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
576 }
577
Bob Wilson06fce872011-02-07 17:43:21 +0000578 setTargetDAGCombine(ISD::INTRINSIC_VOID);
579 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000580 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
581 setTargetDAGCombine(ISD::SHL);
582 setTargetDAGCombine(ISD::SRL);
583 setTargetDAGCombine(ISD::SRA);
584 setTargetDAGCombine(ISD::SIGN_EXTEND);
585 setTargetDAGCombine(ISD::ZERO_EXTEND);
586 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsonc6c13a32010-02-18 06:05:53 +0000587 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000588 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000589 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000590 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
591 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000592 setTargetDAGCombine(ISD::FP_TO_SINT);
593 setTargetDAGCombine(ISD::FP_TO_UINT);
594 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem097106b2011-10-15 20:03:12 +0000595
James Molloy547d4c02012-02-20 09:24:05 +0000596 // It is legal to extload from v4i8 to v4i16 or v4i32.
597 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
598 MVT::v4i16, MVT::v2i16,
599 MVT::v2i32};
600 for (unsigned i = 0; i < 6; ++i) {
601 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
602 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
603 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
604 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000605 }
606
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000607 // ARM and Thumb2 support UMLAL/SMLAL.
608 if (!Subtarget->isThumb1Only())
609 setTargetDAGCombine(ISD::ADDC);
610
611
Evan Cheng6addd652007-05-18 00:19:34 +0000612 computeRegisterProperties();
Evan Cheng10043e22007-01-19 07:51:42 +0000613
614 // ARM does not have f32 extending load.
Owen Anderson9f944592009-08-11 20:47:22 +0000615 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000616
Duncan Sands95d46ef2008-01-23 20:39:46 +0000617 // ARM does not have i1 sign extending load.
Owen Anderson9f944592009-08-11 20:47:22 +0000618 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000619
Evan Cheng10043e22007-01-19 07:51:42 +0000620 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000621 if (!Subtarget->isThumb1Only()) {
622 for (unsigned im = (unsigned)ISD::PRE_INC;
623 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000624 setIndexedLoadAction(im, MVT::i1, Legal);
625 setIndexedLoadAction(im, MVT::i8, Legal);
626 setIndexedLoadAction(im, MVT::i16, Legal);
627 setIndexedLoadAction(im, MVT::i32, Legal);
628 setIndexedStoreAction(im, MVT::i1, Legal);
629 setIndexedStoreAction(im, MVT::i8, Legal);
630 setIndexedStoreAction(im, MVT::i16, Legal);
631 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000632 }
Evan Cheng10043e22007-01-19 07:51:42 +0000633 }
634
635 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000636 setOperationAction(ISD::MUL, MVT::i64, Expand);
637 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000638 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000639 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
640 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000641 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000642 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
643 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000644 setOperationAction(ISD::MULHS, MVT::i32, Expand);
645
Jim Grosbach5d994042009-10-31 19:38:01 +0000646 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000647 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000648 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000649 setOperationAction(ISD::SRL, MVT::i64, Custom);
650 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000651
Evan Chenge8916542011-08-30 01:34:54 +0000652 if (!Subtarget->isThumb1Only()) {
653 // FIXME: We should do this for Thumb1 as well.
654 setOperationAction(ISD::ADDC, MVT::i32, Custom);
655 setOperationAction(ISD::ADDE, MVT::i32, Custom);
656 setOperationAction(ISD::SUBC, MVT::i32, Custom);
657 setOperationAction(ISD::SUBE, MVT::i32, Custom);
658 }
659
Evan Cheng10043e22007-01-19 07:51:42 +0000660 // ARM does not have ROTL.
Owen Anderson9f944592009-08-11 20:47:22 +0000661 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach8546ec92010-01-18 19:58:49 +0000662 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000663 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000664 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000665 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000666
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000667 // These just redirect to CTTZ and CTLZ on ARM.
668 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
669 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
670
Tim Northoverbc933082013-05-23 19:11:20 +0000671 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
672
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000673 // Only ARMv6 has BSWAP.
674 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000675 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000676
Bob Wilsone8a549c2012-09-29 21:43:49 +0000677 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
678 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
679 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbach92d999002010-05-05 20:44:35 +0000680 setOperationAction(ISD::SDIV, MVT::i32, Expand);
681 setOperationAction(ISD::UDIV, MVT::i32, Expand);
682 }
Renato Golin87610692013-07-16 09:32:17 +0000683
684 // FIXME: Also set divmod for SREM on EABI
Owen Anderson9f944592009-08-11 20:47:22 +0000685 setOperationAction(ISD::SREM, MVT::i32, Expand);
686 setOperationAction(ISD::UREM, MVT::i32, Expand);
Renato Golin87610692013-07-16 09:32:17 +0000687 // Register based DivRem for AEABI (RTABI 4.2)
688 if (Subtarget->isTargetAEABI()) {
689 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
690 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
691 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
692 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
693 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
694 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
695 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
696 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
697
698 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
699 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
700 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
701 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
702 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
703 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
704 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
705 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
706
707 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
708 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
709 } else {
710 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
711 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
712 }
Bob Wilson7117a912009-03-20 22:42:55 +0000713
Owen Anderson9f944592009-08-11 20:47:22 +0000714 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
715 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
716 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
717 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000718 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000719
Evan Cheng74d92c12011-04-08 21:37:21 +0000720 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000721
Evan Cheng10043e22007-01-19 07:51:42 +0000722 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000723 setOperationAction(ISD::VASTART, MVT::Other, Custom);
724 setOperationAction(ISD::VAARG, MVT::Other, Expand);
725 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
726 setOperationAction(ISD::VAEND, MVT::Other, Expand);
727 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
728 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000729
Tim Northoverd6a729b2014-01-06 14:28:05 +0000730 if (!Subtarget->isTargetMachO()) {
731 // Non-MachO platforms may return values in these registers via the
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000732 // personality function.
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000733 setExceptionPointerRegister(ARM::R0);
734 setExceptionSelectorRegister(ARM::R1);
735 }
Anton Korobeynikovf3a62312011-01-24 22:38:45 +0000736
Evan Chengf7f97b42010-04-15 22:20:34 +0000737 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng6e809de2010-08-11 06:22:01 +0000738 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
739 // the default expansion.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000740 if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000741 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
742 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000743 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000744
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000745 // On v8, we have particularly efficient implementations of atomic fences
746 // if they can be combined with nearby atomic loads and stores.
747 if (!Subtarget->hasV8Ops()) {
748 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
749 setInsertFencesForAtomic(true);
750 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000751 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000752 // If there's anything we can use as a barrier, go through custom lowering
753 // for ATOMIC_FENCE.
754 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
755 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
756
Jim Grosbach6860bb72010-06-18 22:35:32 +0000757 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000758 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000759 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000760 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000761 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000762 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000763 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000764 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000765 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000766 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000767 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000768 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000769 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000770 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
771 // Unordered/Monotonic case.
772 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
773 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000774 }
Evan Cheng10043e22007-01-19 07:51:42 +0000775
Evan Cheng21acf9f2010-11-04 05:19:35 +0000776 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000777
Eli Friedman8cfa7712010-06-26 04:36:50 +0000778 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
779 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000780 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
781 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000782 }
Owen Anderson9f944592009-08-11 20:47:22 +0000783 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000784
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000785 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
786 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000787 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000788 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000789 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000790 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
791 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000792
793 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000794 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbach31984832010-07-07 00:07:57 +0000795 if (Subtarget->isTargetDarwin()) {
796 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
797 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall7d84ece2011-05-29 19:50:32 +0000798 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbach31984832010-07-07 00:07:57 +0000799 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000800
Owen Anderson9f944592009-08-11 20:47:22 +0000801 setOperationAction(ISD::SETCC, MVT::i32, Expand);
802 setOperationAction(ISD::SETCC, MVT::f32, Expand);
803 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000804 setOperationAction(ISD::SELECT, MVT::i32, Custom);
805 setOperationAction(ISD::SELECT, MVT::f32, Custom);
806 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000807 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
808 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
809 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000810
Owen Anderson9f944592009-08-11 20:47:22 +0000811 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
812 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
813 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
814 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
815 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000816
Dan Gohman482732a2007-10-11 23:21:31 +0000817 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000818 setOperationAction(ISD::FSIN, MVT::f64, Expand);
819 setOperationAction(ISD::FSIN, MVT::f32, Expand);
820 setOperationAction(ISD::FCOS, MVT::f32, Expand);
821 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000822 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
823 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000824 setOperationAction(ISD::FREM, MVT::f64, Expand);
825 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000826 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
827 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000828 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
829 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000830 }
Owen Anderson9f944592009-08-11 20:47:22 +0000831 setOperationAction(ISD::FPOW, MVT::f64, Expand);
832 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000833
Evan Chengd0007f32012-04-10 21:40:28 +0000834 if (!Subtarget->hasVFP4()) {
835 setOperationAction(ISD::FMA, MVT::f64, Expand);
836 setOperationAction(ISD::FMA, MVT::f32, Expand);
837 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000838
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000839 // Various VFP goodness
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000840 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilsone4191e72010-03-19 22:51:32 +0000841 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
842 if (Subtarget->hasVFP2()) {
843 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
844 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
845 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
846 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
847 }
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000848 // Special handling for half-precision FP.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000849 if (!Subtarget->hasFP16()) {
850 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
851 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000852 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000853 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000854
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000855 // Combine sin / cos into one node or libcall if possible.
856 if (Subtarget->hasSinCos()) {
857 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
858 setLibcallName(RTLIB::SINCOS_F64, "sincos");
859 if (Subtarget->getTargetTriple().getOS() == Triple::IOS) {
860 // For iOS, we don't want to the normal expansion of a libcall to
861 // sincos. We want to issue a libcall to __sincos_stret.
862 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
863 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
864 }
865 }
Evan Cheng10043e22007-01-19 07:51:42 +0000866
Chris Lattnerf3f4ad92007-11-27 22:36:16 +0000867 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000868 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +0000869 setTargetDAGCombine(ISD::ADD);
870 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +0000871 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +0000872 setTargetDAGCombine(ISD::AND);
873 setTargetDAGCombine(ISD::OR);
874 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +0000875
Evan Chengf258a152012-02-23 02:58:19 +0000876 if (Subtarget->hasV6Ops())
877 setTargetDAGCombine(ISD::SRL);
878
Evan Cheng10043e22007-01-19 07:51:42 +0000879 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +0000880
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000881 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
882 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +0000883 setSchedulingPreference(Sched::RegPressure);
884 else
885 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +0000886
Evan Cheng3ae2b792011-01-06 06:52:41 +0000887 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000888 MaxStoresPerMemset = 8;
889 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
890 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
891 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
892 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
893 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
Evan Chengb71233f2010-06-26 01:52:05 +0000894
Rafael Espindolaa76eccf2010-07-11 04:01:49 +0000895 // On ARM arguments smaller than 4 bytes are extended, so all arguments
896 // are at least 4 bytes aligned.
897 setMinStackArgumentAlignment(4);
898
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000899 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000900 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000901
Eli Friedman2518f832011-05-06 20:34:06 +0000902 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +0000903}
904
Andrew Trick43f25632011-01-19 02:35:27 +0000905// FIXME: It might make sense to define the representative register class as the
906// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
907// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
908// SPR's representative would be DPR_VFP2. This should work well if register
909// pressure tracking were modified such that a register use would increment the
910// pressure of the register class's representative and all of it's super
911// classes' representatives transitively. We have not implemented this because
912// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000913// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +0000914// and extractions.
Evan Chenga77f3d32010-07-21 06:09:07 +0000915std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000916ARMTargetLowering::findRepresentativeClass(MVT VT) const{
Evan Chenga77f3d32010-07-21 06:09:07 +0000917 const TargetRegisterClass *RRC = 0;
918 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000919 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +0000920 default:
Evan Chenga77f3d32010-07-21 06:09:07 +0000921 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng28590382010-07-21 23:53:58 +0000922 // Use DPR as representative register class for all floating point
923 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
924 // the cost is 1 for both f32 and f64.
925 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +0000926 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +0000927 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +0000928 // When NEON is used for SP, only half of the register file is available
929 // because operations that define both SP and DP results will be constrained
930 // to the VFP2 class (D0-D15). We currently model this constraint prior to
931 // coalescing by double-counting the SP regs. See the FIXME above.
932 if (Subtarget->useNEONForSinglePrecisionFP())
933 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000934 break;
935 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
936 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +0000937 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000938 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000939 break;
940 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +0000941 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000942 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +0000943 break;
944 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +0000945 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000946 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +0000947 break;
Evan Cheng10f99a32010-07-19 22:15:08 +0000948 }
Evan Chenga77f3d32010-07-21 06:09:07 +0000949 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +0000950}
951
Evan Cheng10043e22007-01-19 07:51:42 +0000952const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
953 switch (Opcode) {
954 default: return 0;
955 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +0000956 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +0000957 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
958 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +0000959 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +0000960 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
961 case ARMISD::tCALL: return "ARMISD::tCALL";
962 case ARMISD::BRCOND: return "ARMISD::BRCOND";
963 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +0000964 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +0000965 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +0000966 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +0000967 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
968 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +0000969 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +0000970 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +0000971 case ARMISD::CMPFP: return "ARMISD::CMPFP";
972 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +0000973 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +0000974 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +0000975
Evan Cheng10043e22007-01-19 07:51:42 +0000976 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +0000977
Jim Grosbach8546ec92010-01-18 19:58:49 +0000978 case ARMISD::RBIT: return "ARMISD::RBIT";
979
Bob Wilsone4191e72010-03-19 22:51:32 +0000980 case ARMISD::FTOSI: return "ARMISD::FTOSI";
981 case ARMISD::FTOUI: return "ARMISD::FTOUI";
982 case ARMISD::SITOF: return "ARMISD::SITOF";
983 case ARMISD::UITOF: return "ARMISD::UITOF";
984
Evan Cheng10043e22007-01-19 07:51:42 +0000985 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
986 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
987 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +0000988
Evan Chenge8916542011-08-30 01:34:54 +0000989 case ARMISD::ADDC: return "ARMISD::ADDC";
990 case ARMISD::ADDE: return "ARMISD::ADDE";
991 case ARMISD::SUBC: return "ARMISD::SUBC";
992 case ARMISD::SUBE: return "ARMISD::SUBE";
993
Bob Wilson22806742010-09-22 22:09:21 +0000994 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
995 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +0000996
Evan Chengec6d7c92009-10-28 06:55:03 +0000997 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
998 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
999
Dale Johannesend679ff72010-06-03 21:09:53 +00001000 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001001
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001002 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001003
Evan Chengb972e562009-08-07 00:34:42 +00001004 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1005
Bob Wilson7ed59712010-10-30 00:54:37 +00001006 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001007
Evan Cheng8740ee32010-11-03 06:34:55 +00001008 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1009
Bob Wilson2e076c42009-06-22 23:27:02 +00001010 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001011 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001012 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001013 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1014 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001015 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1016 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001017 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1018 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001019 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1020 case ARMISD::VTST: return "ARMISD::VTST";
1021
1022 case ARMISD::VSHL: return "ARMISD::VSHL";
1023 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1024 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001025 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1026 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1027 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1028 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1029 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1030 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1031 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1032 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1033 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1034 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1035 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1036 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
1037 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1038 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001039 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001040 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001041 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001042 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001043 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001044 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001045 case ARMISD::VREV64: return "ARMISD::VREV64";
1046 case ARMISD::VREV32: return "ARMISD::VREV32";
1047 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001048 case ARMISD::VZIP: return "ARMISD::VZIP";
1049 case ARMISD::VUZP: return "ARMISD::VUZP";
1050 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001051 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1052 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001053 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1054 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001055 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1056 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001057 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilsonc6c13a32010-02-18 06:05:53 +00001058 case ARMISD::FMAX: return "ARMISD::FMAX";
1059 case ARMISD::FMIN: return "ARMISD::FMIN";
Joey Goulye3dd6842013-08-23 12:01:13 +00001060 case ARMISD::VMAXNM: return "ARMISD::VMAX";
1061 case ARMISD::VMINNM: return "ARMISD::VMIN";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001062 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001063 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1064 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001065 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilson2d790df2010-11-28 06:51:26 +00001066 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1067 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1068 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001069 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1070 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1071 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1072 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1073 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1074 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1075 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1076 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1077 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1078 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1079 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1080 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1081 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1082 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1083 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1084 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1085 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001086 }
1087}
1088
Matt Arsenault758659232013-05-18 00:21:46 +00001089EVT ARMTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Duncan Sandsf2641e12011-09-06 19:07:46 +00001090 if (!VT.isVector()) return getPointerTy();
1091 return VT.changeVectorElementTypeToInteger();
1092}
1093
Evan Cheng4cad68e2010-05-15 02:18:07 +00001094/// getRegClassFor - Return the register class that should be used for the
1095/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001096const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001097 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1098 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1099 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001100 if (Subtarget->hasNEON()) {
1101 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001102 return &ARM::QQPRRegClass;
1103 if (VT == MVT::v8i64)
1104 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001105 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001106 return TargetLowering::getRegClassFor(VT);
1107}
1108
Eric Christopher84bdfd82010-07-21 22:26:11 +00001109// Create a fast isel object.
1110FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001111ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1112 const TargetLibraryInfo *libInfo) const {
1113 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001114}
1115
Anton Korobeynikov19edda02010-07-24 21:52:08 +00001116/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1117/// be used for loads / stores from the global.
1118unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1119 return (Subtarget->isThumb1Only() ? 127 : 4095);
1120}
1121
Evan Cheng4401f882010-05-20 23:26:43 +00001122Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001123 unsigned NumVals = N->getNumValues();
1124 if (!NumVals)
1125 return Sched::RegPressure;
1126
1127 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001128 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001129 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001130 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001131 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001132 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001133 }
Evan Chengbf914992010-05-28 23:25:23 +00001134
1135 if (!N->isMachineOpcode())
1136 return Sched::RegPressure;
1137
1138 // Load are scheduled for latency even if there instruction itinerary
1139 // is not available.
1140 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001141 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001142
Evan Cheng6cc775f2011-06-28 19:10:37 +00001143 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001144 return Sched::RegPressure;
1145 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001146 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001147 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001148
Evan Cheng4401f882010-05-20 23:26:43 +00001149 return Sched::RegPressure;
1150}
1151
Evan Cheng10043e22007-01-19 07:51:42 +00001152//===----------------------------------------------------------------------===//
1153// Lowering Code
1154//===----------------------------------------------------------------------===//
1155
Evan Cheng10043e22007-01-19 07:51:42 +00001156/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1157static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1158 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001159 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001160 case ISD::SETNE: return ARMCC::NE;
1161 case ISD::SETEQ: return ARMCC::EQ;
1162 case ISD::SETGT: return ARMCC::GT;
1163 case ISD::SETGE: return ARMCC::GE;
1164 case ISD::SETLT: return ARMCC::LT;
1165 case ISD::SETLE: return ARMCC::LE;
1166 case ISD::SETUGT: return ARMCC::HI;
1167 case ISD::SETUGE: return ARMCC::HS;
1168 case ISD::SETULT: return ARMCC::LO;
1169 case ISD::SETULE: return ARMCC::LS;
1170 }
1171}
1172
Bob Wilsona2e83332009-09-09 23:14:54 +00001173/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1174static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001175 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001176 CondCode2 = ARMCC::AL;
1177 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001178 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001179 case ISD::SETEQ:
1180 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1181 case ISD::SETGT:
1182 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1183 case ISD::SETGE:
1184 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1185 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001186 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001187 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1188 case ISD::SETO: CondCode = ARMCC::VC; break;
1189 case ISD::SETUO: CondCode = ARMCC::VS; break;
1190 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1191 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1192 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1193 case ISD::SETLT:
1194 case ISD::SETULT: CondCode = ARMCC::LT; break;
1195 case ISD::SETLE:
1196 case ISD::SETULE: CondCode = ARMCC::LE; break;
1197 case ISD::SETNE:
1198 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1199 }
Evan Cheng10043e22007-01-19 07:51:42 +00001200}
1201
Bob Wilsona4c22902009-04-17 19:07:39 +00001202//===----------------------------------------------------------------------===//
1203// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001204//===----------------------------------------------------------------------===//
1205
1206#include "ARMGenCallingConv.inc"
1207
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001208/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1209/// given CallingConvention value.
Sandeep Patel68c5f472009-09-02 08:44:58 +00001210CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001211 bool Return,
1212 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001213 switch (CC) {
1214 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001215 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001216 case CallingConv::Fast:
Evan Cheng817bbac2010-10-23 02:19:37 +00001217 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng08dd8c82010-10-22 18:23:05 +00001218 if (!Subtarget->isAAPCS_ABI())
1219 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1220 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1221 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1222 }
1223 // Fallthrough
1224 case CallingConv::C: {
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001225 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng08dd8c82010-10-22 18:23:05 +00001226 if (!Subtarget->isAAPCS_ABI())
1227 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1228 else if (Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001229 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1230 !isVarArg)
Evan Cheng08dd8c82010-10-22 18:23:05 +00001231 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1232 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1233 }
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001234 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov1b42e642012-01-29 09:06:09 +00001235 if (!isVarArg)
1236 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1237 // Fallthrough
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001238 case CallingConv::ARM_AAPCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001239 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001240 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001241 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001242 case CallingConv::GHC:
1243 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001244 }
1245}
1246
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001247/// LowerCallResult - Lower the result values of a call into the
1248/// appropriate copies out of appropriate physical registers.
1249SDValue
1250ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001251 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001252 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001253 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001254 SmallVectorImpl<SDValue> &InVals,
1255 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001256
Bob Wilsona4c22902009-04-17 19:07:39 +00001257 // Assign locations to each value returned by this call.
1258 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00001259 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1260 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001261 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001262 CCAssignFnForNode(CallConv, /* Return*/ true,
1263 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001264
1265 // Copy all of the result registers out of their specified physreg.
1266 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1267 CCValAssign VA = RVLocs[i];
1268
Stephen Linb8bd2322013-04-20 05:14:40 +00001269 // Pass 'this' value directly from the argument to return value, to avoid
1270 // reg unit interference
1271 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001272 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1273 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001274 InVals.push_back(ThisVal);
1275 continue;
1276 }
1277
Bob Wilson0041bd32009-04-25 00:33:20 +00001278 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001279 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001280 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001281 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001282 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001283 Chain = Lo.getValue(1);
1284 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001285 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001286 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001287 InFlag);
1288 Chain = Hi.getValue(1);
1289 InFlag = Hi.getValue(2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001290 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001291
Owen Anderson9f944592009-08-11 20:47:22 +00001292 if (VA.getLocVT() == MVT::v2f64) {
1293 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1294 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1295 DAG.getConstant(0, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001296
1297 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001298 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001299 Chain = Lo.getValue(1);
1300 InFlag = Lo.getValue(2);
1301 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001302 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001303 Chain = Hi.getValue(1);
1304 InFlag = Hi.getValue(2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001305 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001306 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1307 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001308 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001309 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001310 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1311 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001312 Chain = Val.getValue(1);
1313 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001314 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001315
1316 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001317 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001318 case CCValAssign::Full: break;
1319 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001320 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001321 break;
1322 }
1323
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001324 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001325 }
1326
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001327 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001328}
1329
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001330/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001331SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001332ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1333 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001334 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001335 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001336 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001337 unsigned LocMemOffset = VA.getLocMemOffset();
1338 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1339 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilsona4c22902009-04-17 19:07:39 +00001340 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner886250c2010-09-21 18:51:21 +00001341 MachinePointerInfo::getStack(LocMemOffset),
David Greene0d0149f2010-02-15 16:55:24 +00001342 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001343}
1344
Andrew Trickef9de2a2013-05-25 02:42:55 +00001345void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001346 SDValue Chain, SDValue &Arg,
1347 RegsToPassVector &RegsToPass,
1348 CCValAssign &VA, CCValAssign &NextVA,
1349 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001350 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001351 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001352
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001353 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001354 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson2e076c42009-06-22 23:27:02 +00001355 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1356
1357 if (NextVA.isRegLoc())
1358 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1359 else {
1360 assert(NextVA.isMemLoc());
1361 if (StackPtr.getNode() == 0)
1362 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1363
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001364 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1365 dl, DAG, NextVA,
1366 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001367 }
1368}
1369
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001370/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001371/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1372/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001373SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001374ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001375 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001376 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001377 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001378 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1379 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1380 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001381 SDValue Chain = CLI.Chain;
1382 SDValue Callee = CLI.Callee;
1383 bool &isTailCall = CLI.IsTailCall;
1384 CallingConv::ID CallConv = CLI.CallConv;
1385 bool doesNotRet = CLI.DoesNotReturn;
1386 bool isVarArg = CLI.IsVarArg;
1387
Dale Johannesend679ff72010-06-03 21:09:53 +00001388 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001389 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1390 bool isThisReturn = false;
1391 bool isSibCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001392
Bob Wilson8decdc42011-10-07 17:17:49 +00001393 // Disable tail calls if they're not supported.
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001394 if (!Subtarget->supportsTailCall() || MF.getTarget().Options.DisableTailCalls)
Bob Wilson3c9ed762010-08-13 22:43:33 +00001395 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001396
Dale Johannesend679ff72010-06-03 21:09:53 +00001397 if (isTailCall) {
1398 // Check if it's really possible to do a tail call.
1399 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001400 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001401 Outs, OutVals, Ins, DAG);
Dale Johannesend679ff72010-06-03 21:09:53 +00001402 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1403 // detected sibcalls.
1404 if (isTailCall) {
1405 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001406 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001407 }
1408 }
Evan Cheng10043e22007-01-19 07:51:42 +00001409
Bob Wilsona4c22902009-04-17 19:07:39 +00001410 // Analyze operands of the call, assigning locations to each operand.
1411 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00001412 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1413 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001414 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001415 CCAssignFnForNode(CallConv, /* Return*/ false,
1416 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001417
Bob Wilsona4c22902009-04-17 19:07:39 +00001418 // Get a count of how many bytes are to be pushed on the stack.
1419 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001420
Dale Johannesend679ff72010-06-03 21:09:53 +00001421 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001422 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001423 NumBytes = 0;
1424
Evan Cheng10043e22007-01-19 07:51:42 +00001425 // Adjust the stack pointer for the new arguments...
1426 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001427 if (!isSibCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00001428 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
1429 dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001430
Jim Grosbach6ad4bcb2010-02-24 01:43:03 +00001431 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Cheng10043e22007-01-19 07:51:42 +00001432
Bob Wilson2e076c42009-06-22 23:27:02 +00001433 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001434 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001435
Bob Wilsona4c22902009-04-17 19:07:39 +00001436 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001437 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001438 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1439 i != e;
1440 ++i, ++realArgIdx) {
1441 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001442 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001443 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001444 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001445
Bob Wilsona4c22902009-04-17 19:07:39 +00001446 // Promote the value if needed.
1447 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001448 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001449 case CCValAssign::Full: break;
1450 case CCValAssign::SExt:
1451 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1452 break;
1453 case CCValAssign::ZExt:
1454 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1455 break;
1456 case CCValAssign::AExt:
1457 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1458 break;
1459 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001460 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001461 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001462 }
1463
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001464 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001465 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001466 if (VA.getLocVT() == MVT::v2f64) {
1467 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1468 DAG.getConstant(0, MVT::i32));
1469 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1470 DAG.getConstant(1, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001471
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001472 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001473 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1474
1475 VA = ArgLocs[++i]; // skip ahead to next loc
1476 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001477 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001478 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1479 } else {
1480 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001481
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001482 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1483 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001484 }
1485 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001486 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001487 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001488 }
1489 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001490 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1491 assert(VA.getLocVT() == MVT::i32 &&
1492 "unexpected calling convention register assignment");
1493 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001494 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001495 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001496 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001497 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001498 } else if (isByVal) {
1499 assert(VA.isMemLoc());
1500 unsigned offset = 0;
1501
1502 // True if this byval aggregate will be split between registers
1503 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001504 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
1505 unsigned CurByValIdx = CCInfo.getInRegsParamsProceed();
1506
1507 if (CurByValIdx < ByValArgsCount) {
1508
1509 unsigned RegBegin, RegEnd;
1510 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1511
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001512 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1513 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001514 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001515 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1516 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1517 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1518 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001519 false, false, false,
1520 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001521 MemOpChains.push_back(Load.getValue(1));
1522 RegsToPass.push_back(std::make_pair(j, Load));
1523 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001524
1525 // If parameter size outsides register area, "offset" value
1526 // helps us to calculate stack slot for remained part properly.
1527 offset = RegEnd - RegBegin;
1528
1529 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001530 }
1531
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001532 if (Flags.getByValSize() > 4*offset) {
Manman Ren9f911162012-06-01 02:44:42 +00001533 unsigned LocMemOffset = VA.getLocMemOffset();
1534 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1535 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1536 StkPtrOff);
1537 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1538 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1539 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1540 MVT::i32);
Manman Rene8735522012-06-01 19:33:18 +00001541 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001542
Manman Ren9f911162012-06-01 02:44:42 +00001543 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001544 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001545 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
1546 Ops, array_lengthof(Ops)));
1547 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001548 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001549 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001550
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001551 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1552 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001553 }
Evan Cheng10043e22007-01-19 07:51:42 +00001554 }
1555
1556 if (!MemOpChains.empty())
Owen Anderson9f944592009-08-11 20:47:22 +00001557 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Cheng10043e22007-01-19 07:51:42 +00001558 &MemOpChains[0], MemOpChains.size());
1559
1560 // Build a sequence of copy-to-reg nodes chained together with token chain
1561 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001562 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001563 // Tail call byval lowering might overwrite argument registers so in case of
1564 // tail call optimization the copies to registers are lowered later.
1565 if (!isTailCall)
1566 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1567 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1568 RegsToPass[i].second, InFlag);
1569 InFlag = Chain.getValue(1);
1570 }
Evan Cheng10043e22007-01-19 07:51:42 +00001571
Dale Johannesend679ff72010-06-03 21:09:53 +00001572 // For tail calls lower the arguments to the 'real' stack slot.
1573 if (isTailCall) {
1574 // Force all the incoming stack arguments to be loaded from the stack
1575 // before any new outgoing arguments are stored to the stack, because the
1576 // outgoing stack slots may alias the incoming argument stack slots, and
1577 // the alias isn't otherwise explicit. This is slightly more conservative
1578 // than necessary, because it means that each store effectively depends
1579 // on every argument instead of just those arguments it would clobber.
1580
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001581 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001582 InFlag = SDValue();
1583 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1584 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1585 RegsToPass[i].second, InFlag);
1586 InFlag = Chain.getValue(1);
1587 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001588 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001589 }
1590
Bill Wendling24c79f22008-09-16 21:48:12 +00001591 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1592 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1593 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001594 bool isDirect = false;
1595 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001596 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001597 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001598
1599 if (EnableARMLongCalls) {
1600 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1601 && "long-calls with non-static relocation model!");
1602 // Handle a global address or an external symbol. If it's not one of
1603 // those, the target's already in a register, so we don't need to do
1604 // anything extra.
1605 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001606 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001607 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001608 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001609 ARMConstantPoolValue *CPV =
1610 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1611
Jim Grosbach32bb3622010-04-14 22:28:31 +00001612 // Get the address of the callee into a register
1613 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1614 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1615 Callee = DAG.getLoad(getPointerTy(), dl,
1616 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001617 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001618 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001619 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1620 const char *Sym = S->getSymbol();
1621
1622 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001623 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001624 ARMConstantPoolValue *CPV =
1625 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1626 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001627 // Get the address of the callee into a register
1628 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1629 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1630 Callee = DAG.getLoad(getPointerTy(), dl,
1631 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001632 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001633 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001634 }
1635 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001636 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001637 isDirect = true;
Chris Lattner55452c22009-07-15 04:12:33 +00001638 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Tim Northoverd6a729b2014-01-06 14:28:05 +00001639 bool isStub = (isExt && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001640 getTargetMachine().getRelocationModel() != Reloc::Static;
1641 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc3c949b42007-06-19 21:05:09 +00001642 // ARM call to a local ARM function is predicable.
Evan Chengf128bdc2010-06-16 07:35:02 +00001643 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001644 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001645 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001646 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Tim Northover72360d22013-12-02 10:35:41 +00001647 Callee = DAG.getNode(ARMISD::WrapperPIC, dl, getPointerTy(),
1648 DAG.getTargetGlobalAddress(GV, dl, getPointerTy()));
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001649 } else {
1650 // On ELF targets for PIC code, direct calls should go through the PLT
1651 unsigned OpFlags = 0;
1652 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001653 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001654 OpFlags = ARMII::MO_PLT;
1655 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1656 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001657 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001658 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001659 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001660 getTargetMachine().getRelocationModel() != Reloc::Static;
1661 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng83f35172007-01-30 20:37:08 +00001662 // tBX takes a register source operand.
1663 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001664 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001665 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001666 ARMConstantPoolValue *CPV =
1667 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1668 ARMPCLabelIndex, 4);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001669 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001670 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen021052a2009-02-04 20:06:27 +00001671 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Chengcdbb70c2009-10-31 03:39:36 +00001672 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001673 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001674 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00001675 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson7117a912009-03-20 22:42:55 +00001676 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen021052a2009-02-04 20:06:27 +00001677 getPointerTy(), Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001678 } else {
1679 unsigned OpFlags = 0;
1680 // On ELF targets for PIC code, direct calls should go through the PLT
1681 if (Subtarget->isTargetELF() &&
1682 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1683 OpFlags = ARMII::MO_PLT;
1684 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1685 }
Evan Cheng10043e22007-01-19 07:51:42 +00001686 }
1687
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001688 // FIXME: handle tail calls differently.
1689 unsigned CallOpc;
Tim Northoverdee86042013-12-02 14:46:26 +00001690 bool HasMinSizeAttr = Subtarget->isMinSize();
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001691 if (Subtarget->isThumb()) {
1692 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001693 CallOpc = ARMISD::CALL_NOLINK;
1694 else
1695 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1696 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001697 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001698 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001699 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet8e1fe842012-11-02 21:32:17 +00001700 // Emit regular call when code size is the priority
1701 !HasMinSizeAttr)
Evan Cheng65f9d192012-02-28 18:51:51 +00001702 // "mov lr, pc; b _foo" to avoid confusing the RSP
1703 CallOpc = ARMISD::CALL_NOLINK;
1704 else
1705 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001706 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001707
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001708 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001709 Ops.push_back(Chain);
1710 Ops.push_back(Callee);
1711
1712 // Add argument registers to the end of the list so that they are known live
1713 // into the call.
1714 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1715 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1716 RegsToPass[i].second.getValueType()));
1717
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001718 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001719 if (!isTailCall) {
1720 const uint32_t *Mask;
1721 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1722 const ARMBaseRegisterInfo *ARI = static_cast<const ARMBaseRegisterInfo*>(TRI);
1723 if (isThisReturn) {
1724 // For 'this' returns, use the R0-preserving mask if applicable
1725 Mask = ARI->getThisReturnPreservedMask(CallConv);
1726 if (!Mask) {
1727 // Set isThisReturn to false if the calling convention is not one that
1728 // allows 'returned' to be modeled in this way, so LowerCallResult does
1729 // not try to pass 'this' straight through
1730 isThisReturn = false;
1731 Mask = ARI->getCallPreservedMask(CallConv);
1732 }
1733 } else
Stephen Linff7fcee2013-06-26 21:42:14 +00001734 Mask = ARI->getCallPreservedMask(CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001735
Matthias Braunc22630e2013-10-04 16:52:54 +00001736 assert(Mask && "Missing call preserved mask for calling convention");
1737 Ops.push_back(DAG.getRegisterMask(Mask));
1738 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001739
Gabor Greiff304a7a2008-08-28 21:40:38 +00001740 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001741 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001742
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001743 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001744 if (isTailCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001745 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesend679ff72010-06-03 21:09:53 +00001746
Duncan Sands739a0542008-07-02 17:40:58 +00001747 // Returns a chain and a flag for retval copy to use.
Dale Johannesend679ff72010-06-03 21:09:53 +00001748 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng10043e22007-01-19 07:51:42 +00001749 InFlag = Chain.getValue(1);
1750
Chris Lattner27539552008-10-11 22:08:30 +00001751 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001752 DAG.getIntPtrConstant(0, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001753 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001754 InFlag = Chain.getValue(1);
1755
Bob Wilsona4c22902009-04-17 19:07:39 +00001756 // Handle result values, copying them out of physregs into vregs that we
1757 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001758 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001759 InVals, isThisReturn,
1760 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001761}
1762
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001763/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001764/// on the stack. Remember the next parameter register to allocate,
1765/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001766/// this.
1767void
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001768ARMTargetLowering::HandleByVal(
1769 CCState *State, unsigned &size, unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001770 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1771 assert((State->getCallOrPrologue() == Prologue ||
1772 State->getCallOrPrologue() == Call) &&
1773 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001774
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001775 if ((ARM::R0 <= reg) && (reg <= ARM::R3)) {
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001776 if (Subtarget->isAAPCS_ABI() && Align > 4) {
1777 unsigned AlignInRegs = Align / 4;
1778 unsigned Waste = (ARM::R4 - reg) % AlignInRegs;
1779 for (unsigned i = 0; i < Waste; ++i)
1780 reg = State->AllocateReg(GPRArgRegs, 4);
1781 }
1782 if (reg != 0) {
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001783 unsigned excess = 4 * (ARM::R4 - reg);
1784
1785 // Special case when NSAA != SP and parameter size greater than size of
1786 // all remained GPR regs. In that case we can't split parameter, we must
1787 // send it to stack. We also must set NCRN to R4, so waste all
1788 // remained registers.
Oliver Stannardd55e1152014-03-05 15:25:27 +00001789 const unsigned NSAAOffset = State->getNextStackOffset();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001790 if (Subtarget->isAAPCS_ABI() && NSAAOffset != 0 && size > excess) {
1791 while (State->AllocateReg(GPRArgRegs, 4))
1792 ;
1793 return;
1794 }
1795
1796 // First register for byval parameter is the first register that wasn't
1797 // allocated before this method call, so it would be "reg".
1798 // If parameter is small enough to be saved in range [reg, r4), then
1799 // the end (first after last) register would be reg + param-size-in-regs,
1800 // else parameter would be splitted between registers and stack,
1801 // end register would be r4 in this case.
1802 unsigned ByValRegBegin = reg;
Stepan Dyatkovskiy2703bca2013-05-08 14:51:27 +00001803 unsigned ByValRegEnd = (size < excess) ? reg + size/4 : (unsigned)ARM::R4;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001804 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
1805 // Note, first register is allocated in the beginning of function already,
1806 // allocate remained amount of registers we need.
1807 for (unsigned i = reg+1; i != ByValRegEnd; ++i)
1808 State->AllocateReg(GPRArgRegs, 4);
Oliver Stannardd55e1152014-03-05 15:25:27 +00001809 // A byval parameter that is split between registers and memory needs its
1810 // size truncated here.
1811 // In the case where the entire structure fits in registers, we set the
1812 // size in memory to zero.
1813 if (size < excess)
1814 size = 0;
1815 else
1816 size -= excess;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001817 }
1818 }
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001819}
1820
Dale Johannesend679ff72010-06-03 21:09:53 +00001821/// MatchingStackOffset - Return true if the given stack call argument is
1822/// already available in the same position (relatively) of the caller's
1823/// incoming argument stack.
1824static
1825bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1826 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00001827 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00001828 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1829 int FI = INT_MAX;
1830 if (Arg.getOpcode() == ISD::CopyFromReg) {
1831 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001832 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00001833 return false;
1834 MachineInstr *Def = MRI->getVRegDef(VR);
1835 if (!Def)
1836 return false;
1837 if (!Flags.isByVal()) {
1838 if (!TII->isLoadFromStackSlot(Def, FI))
1839 return false;
1840 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00001841 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00001842 }
1843 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1844 if (Flags.isByVal())
1845 // ByVal argument is passed in as a pointer but it's now being
1846 // dereferenced. e.g.
1847 // define @foo(%struct.X* %A) {
1848 // tail call @bar(%struct.X* byval %A)
1849 // }
1850 return false;
1851 SDValue Ptr = Ld->getBasePtr();
1852 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1853 if (!FINode)
1854 return false;
1855 FI = FINode->getIndex();
1856 } else
1857 return false;
1858
1859 assert(FI != INT_MAX);
1860 if (!MFI->isFixedObjectIndex(FI))
1861 return false;
1862 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1863}
1864
1865/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1866/// for tail call optimization. Targets which want to do tail call
1867/// optimization should implement this function.
1868bool
1869ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1870 CallingConv::ID CalleeCC,
1871 bool isVarArg,
1872 bool isCalleeStructRet,
1873 bool isCallerStructRet,
1874 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001875 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00001876 const SmallVectorImpl<ISD::InputArg> &Ins,
1877 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00001878 const Function *CallerF = DAG.getMachineFunction().getFunction();
1879 CallingConv::ID CallerCC = CallerF->getCallingConv();
1880 bool CCMatch = CallerCC == CalleeCC;
1881
1882 // Look for obvious safe cases to perform tail call optimization that do not
1883 // require ABI changes. This is what gcc calls sibcall.
1884
Jim Grosbache3864cc2010-06-16 23:45:49 +00001885 // Do not sibcall optimize vararg calls unless the call site is not passing
1886 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00001887 if (isVarArg && !Outs.empty())
1888 return false;
1889
Tim Northoverd8407452013-10-01 14:33:28 +00001890 // Exception-handling functions need a special set of instructions to indicate
1891 // a return to the hardware. Tail-calling another function would probably
1892 // break this.
1893 if (CallerF->hasFnAttribute("interrupt"))
1894 return false;
1895
Dale Johannesend679ff72010-06-03 21:09:53 +00001896 // Also avoid sibcall optimization if either caller or callee uses struct
1897 // return semantics.
1898 if (isCalleeStructRet || isCallerStructRet)
1899 return false;
1900
Dale Johannesend24c66b2010-06-23 18:52:34 +00001901 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach3840c902011-07-08 20:18:11 +00001902 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1903 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1904 // support in the assembler and linker to be used. This would need to be
1905 // fixed to fully support tail calls in Thumb1.
1906 //
Dale Johannesene2289282010-07-08 01:18:23 +00001907 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1908 // LR. This means if we need to reload LR, it takes an extra instructions,
1909 // which outweighs the value of the tail call; but here we don't know yet
1910 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach535d3b42010-09-08 03:54:02 +00001911 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesene2289282010-07-08 01:18:23 +00001912 // emitEpilogue if LR is used.
Dale Johannesene2289282010-07-08 01:18:23 +00001913
1914 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1915 // but we need to make sure there are enough registers; the only valid
1916 // registers are the 4 used for parameters. We don't currently do this
1917 // case.
Evan Chengd4b08732010-11-30 23:55:39 +00001918 if (Subtarget->isThumb1Only())
1919 return false;
Dale Johannesen3ac52b32010-06-18 18:13:11 +00001920
Dale Johannesend679ff72010-06-03 21:09:53 +00001921 // If the calling conventions do not match, then we'd better make sure the
1922 // results are returned in the same way as what the caller expects.
1923 if (!CCMatch) {
1924 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwarich89019782011-06-10 20:59:24 +00001925 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1926 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00001927 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1928
1929 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwarich89019782011-06-10 20:59:24 +00001930 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1931 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00001932 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1933
1934 if (RVLocs1.size() != RVLocs2.size())
1935 return false;
1936 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1937 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1938 return false;
1939 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1940 return false;
1941 if (RVLocs1[i].isRegLoc()) {
1942 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1943 return false;
1944 } else {
1945 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1946 return false;
1947 }
1948 }
1949 }
1950
Manman Ren7e48b252012-10-12 23:39:43 +00001951 // If Caller's vararg or byval argument has been split between registers and
1952 // stack, do not perform tail call, since part of the argument is in caller's
1953 // local frame.
1954 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
1955 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00001956 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00001957 return false;
1958
Dale Johannesend679ff72010-06-03 21:09:53 +00001959 // If the callee takes no arguments then go on to check the results of the
1960 // call.
1961 if (!Outs.empty()) {
1962 // Check if stack adjustment is needed. For now, do not do this if any
1963 // argument is passed on the stack.
1964 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00001965 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1966 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00001967 CCInfo.AnalyzeCallOperands(Outs,
1968 CCAssignFnForNode(CalleeCC, false, isVarArg));
1969 if (CCInfo.getNextStackOffset()) {
1970 MachineFunction &MF = DAG.getMachineFunction();
1971
1972 // Check if the arguments are already laid out in the right way as
1973 // the caller's fixed stack objects.
1974 MachineFrameInfo *MFI = MF.getFrameInfo();
1975 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topper07720d82012-03-25 23:49:58 +00001976 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001977 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1978 i != e;
1979 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00001980 CCValAssign &VA = ArgLocs[i];
1981 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001982 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001983 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00001984 if (VA.getLocInfo() == CCValAssign::Indirect)
1985 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001986 if (VA.needsCustom()) {
1987 // f64 and vector types are split into multiple registers or
1988 // register/stack-slot combinations. The types will not match
1989 // the registers; give up on memory f64 refs until we figure
1990 // out what to do about this.
1991 if (!VA.isRegLoc())
1992 return false;
1993 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00001994 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001995 if (RegVT == MVT::v2f64) {
1996 if (!ArgLocs[++i].isRegLoc())
1997 return false;
1998 if (!ArgLocs[++i].isRegLoc())
1999 return false;
2000 }
2001 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002002 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2003 MFI, MRI, TII))
2004 return false;
2005 }
2006 }
2007 }
2008 }
2009
2010 return true;
2011}
2012
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002013bool
2014ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2015 MachineFunction &MF, bool isVarArg,
2016 const SmallVectorImpl<ISD::OutputArg> &Outs,
2017 LLVMContext &Context) const {
2018 SmallVector<CCValAssign, 16> RVLocs;
2019 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), RVLocs, Context);
2020 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2021 isVarArg));
2022}
2023
Tim Northoverd8407452013-10-01 14:33:28 +00002024static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2025 SDLoc DL, SelectionDAG &DAG) {
2026 const MachineFunction &MF = DAG.getMachineFunction();
2027 const Function *F = MF.getFunction();
2028
2029 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2030
2031 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2032 // version of the "preferred return address". These offsets affect the return
2033 // instruction if this is a return from PL1 without hypervisor extensions.
2034 // IRQ/FIQ: +4 "subs pc, lr, #4"
2035 // SWI: 0 "subs pc, lr, #0"
2036 // ABORT: +4 "subs pc, lr, #4"
2037 // UNDEF: +4/+2 "subs pc, lr, #0"
2038 // UNDEF varies depending on where the exception came from ARM or Thumb
2039 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2040
2041 int64_t LROffset;
2042 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2043 IntKind == "ABORT")
2044 LROffset = 4;
2045 else if (IntKind == "SWI" || IntKind == "UNDEF")
2046 LROffset = 0;
2047 else
2048 report_fatal_error("Unsupported interrupt attribute. If present, value "
2049 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2050
2051 RetOps.insert(RetOps.begin() + 1, DAG.getConstant(LROffset, MVT::i32, false));
2052
2053 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other,
2054 RetOps.data(), RetOps.size());
2055}
2056
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002057SDValue
2058ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002059 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002060 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002061 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002062 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002063
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002064 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002065 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002066
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002067 // CCState - Info about the registers and stack slots.
Cameron Zwarich89019782011-06-10 20:59:24 +00002068 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2069 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002070
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002071 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002072 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2073 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002074
Bob Wilsona4c22902009-04-17 19:07:39 +00002075 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002076 SmallVector<SDValue, 4> RetOps;
2077 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Bob Wilsona4c22902009-04-17 19:07:39 +00002078
2079 // Copy the result values into the output registers.
2080 for (unsigned i = 0, realRVLocIdx = 0;
2081 i != RVLocs.size();
2082 ++i, ++realRVLocIdx) {
2083 CCValAssign &VA = RVLocs[i];
2084 assert(VA.isRegLoc() && "Can only return in registers!");
2085
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002086 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002087
2088 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002089 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002090 case CCValAssign::Full: break;
2091 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002092 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002093 break;
2094 }
2095
Bob Wilsona4c22902009-04-17 19:07:39 +00002096 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002097 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002098 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002099 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2100 DAG.getConstant(0, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002101 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002102 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002103
2104 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
2105 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002106 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002107 VA = RVLocs[++i]; // skip ahead to next loc
2108 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2109 HalfGPRs.getValue(1), Flag);
2110 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002111 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002112 VA = RVLocs[++i]; // skip ahead to next loc
2113
2114 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002115 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2116 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002117 }
2118 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2119 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002120 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002121 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilsona4c22902009-04-17 19:07:39 +00002122 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002123 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002124 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002125 VA = RVLocs[++i]; // skip ahead to next loc
2126 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
2127 Flag);
2128 } else
2129 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2130
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002131 // Guarantee that all emitted copies are
2132 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002133 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002134 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002135 }
2136
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002137 // Update chain and glue.
2138 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002139 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002140 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002141
Tim Northoverd8407452013-10-01 14:33:28 +00002142 // CPUs which aren't M-class use a special sequence to return from
2143 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2144 // though we use "subs pc, lr, #N").
2145 //
2146 // M-class CPUs actually use a normal return sequence with a special
2147 // (hardware-provided) value in LR, so the normal code path works.
2148 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2149 !Subtarget->isMClass()) {
2150 if (Subtarget->isThumb1Only())
2151 report_fatal_error("interrupt attribute is not supported in Thumb1");
2152 return LowerInterruptReturn(RetOps, dl, DAG);
2153 }
2154
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002155 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other,
2156 RetOps.data(), RetOps.size());
Evan Cheng10043e22007-01-19 07:51:42 +00002157}
2158
Evan Chengf8bad082012-04-10 01:51:00 +00002159bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002160 if (N->getNumValues() != 1)
2161 return false;
2162 if (!N->hasNUsesOfValue(1, 0))
2163 return false;
2164
Evan Chengf8bad082012-04-10 01:51:00 +00002165 SDValue TCChain = Chain;
2166 SDNode *Copy = *N->use_begin();
2167 if (Copy->getOpcode() == ISD::CopyToReg) {
2168 // If the copy has a glue operand, we conservatively assume it isn't safe to
2169 // perform a tail call.
2170 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2171 return false;
2172 TCChain = Copy->getOperand(0);
2173 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2174 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002175 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002176 SmallPtrSet<SDNode*, 2> Copies;
2177 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002178 UI != UE; ++UI) {
2179 if (UI->getOpcode() != ISD::CopyToReg)
2180 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002181 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002182 }
Evan Chengf8bad082012-04-10 01:51:00 +00002183 if (Copies.size() > 2)
2184 return false;
2185
2186 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2187 UI != UE; ++UI) {
2188 SDValue UseChain = UI->getOperand(0);
2189 if (Copies.count(UseChain.getNode()))
2190 // Second CopyToReg
2191 Copy = *UI;
2192 else
2193 // First CopyToReg
2194 TCChain = UseChain;
2195 }
2196 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002197 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002198 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002199 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002200 Copy = *Copy->use_begin();
2201 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002202 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002203 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002204 } else {
2205 return false;
2206 }
2207
Evan Cheng419ea282010-12-01 22:59:46 +00002208 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002209 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2210 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002211 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2212 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002213 return false;
2214 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002215 }
2216
Evan Chengf8bad082012-04-10 01:51:00 +00002217 if (!HasRet)
2218 return false;
2219
2220 Chain = TCChain;
2221 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002222}
2223
Evan Cheng0663f232011-03-21 01:19:09 +00002224bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002225 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002226 return false;
2227
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00002228 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng0663f232011-03-21 01:19:09 +00002229 return false;
2230
2231 return !Subtarget->isThumb1Only();
2232}
2233
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002234// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2235// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2236// one of the above mentioned nodes. It has to be wrapped because otherwise
2237// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2238// be used to form addressing mode. These wrapped nodes will be selected
2239// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002240static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002241 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002242 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002243 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002244 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002245 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002246 if (CP->isMachineConstantPoolEntry())
2247 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2248 CP->getAlignment());
2249 else
2250 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2251 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002252 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002253}
2254
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002255unsigned ARMTargetLowering::getJumpTableEncoding() const {
2256 return MachineJumpTableInfo::EK_Inline;
2257}
2258
Dan Gohman21cea8a2010-04-17 15:26:15 +00002259SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2260 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002261 MachineFunction &MF = DAG.getMachineFunction();
2262 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2263 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002264 SDLoc DL(Op);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002265 EVT PtrVT = getPointerTy();
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002266 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002267 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2268 SDValue CPAddr;
2269 if (RelocM == Reloc::Static) {
2270 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2271 } else {
2272 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002273 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002274 ARMConstantPoolValue *CPV =
2275 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2276 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002277 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2278 }
2279 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2280 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002281 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002282 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002283 if (RelocM == Reloc::Static)
2284 return Result;
Evan Cheng408aa562009-11-06 22:24:13 +00002285 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002286 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002287}
2288
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002289// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002290SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002291ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002292 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002293 SDLoc dl(GA);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002294 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002295 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002296 MachineFunction &MF = DAG.getMachineFunction();
2297 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002298 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002299 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002300 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2301 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002302 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002303 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002304 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattner7727d052010-09-21 06:44:06 +00002305 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002306 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002307 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002308
Evan Cheng408aa562009-11-06 22:24:13 +00002309 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002310 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002311
2312 // call __tls_get_addr.
2313 ArgListTy Args;
2314 ArgListEntry Entry;
2315 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002316 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002317 Args.push_back(Entry);
Dale Johannesen555a3752009-01-30 23:10:59 +00002318 // FIXME: is there useful debug info available here?
Justin Holewinskiaa583972012-05-25 16:35:28 +00002319 TargetLowering::CallLoweringInfo CLI(Chain,
2320 (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng09c070f2009-08-14 19:11:20 +00002321 false, false, false, false,
Evan Cheng65f9d192012-02-28 18:51:51 +00002322 0, CallingConv::C, /*isTailCall=*/false,
2323 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling78c5b7a2010-03-02 01:55:18 +00002324 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Justin Holewinskiaa583972012-05-25 16:35:28 +00002325 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002326 return CallResult.first;
2327}
2328
2329// Lower ISD::GlobalTLSAddress using the "initial exec" or
2330// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002331SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002332ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002333 SelectionDAG &DAG,
2334 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002335 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002336 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002337 SDValue Offset;
2338 SDValue Chain = DAG.getEntryNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002339 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002340 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002341 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002342
Hans Wennborgaea41202012-05-04 09:40:39 +00002343 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002344 MachineFunction &MF = DAG.getMachineFunction();
2345 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002346 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002347 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002348 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2349 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002350 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2351 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2352 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002353 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002354 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002355 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002356 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002357 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002358 Chain = Offset.getValue(1);
2359
Evan Cheng408aa562009-11-06 22:24:13 +00002360 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002361 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002362
Evan Chengcdbb70c2009-10-31 03:39:36 +00002363 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002364 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002365 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002366 } else {
2367 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002368 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002369 ARMConstantPoolValue *CPV =
2370 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002371 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002372 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002373 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002374 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002375 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002376 }
2377
2378 // The address of the thread local variable is the add of the thread
2379 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002380 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002381}
2382
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002383SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002384ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002385 // TODO: implement the "local dynamic" model
2386 assert(Subtarget->isTargetELF() &&
2387 "TLS not implemented for non-ELF targets");
2388 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgaea41202012-05-04 09:40:39 +00002389
2390 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2391
2392 switch (model) {
2393 case TLSModel::GeneralDynamic:
2394 case TLSModel::LocalDynamic:
2395 return LowerToTLSGeneralDynamicModel(GA, DAG);
2396 case TLSModel::InitialExec:
2397 case TLSModel::LocalExec:
2398 return LowerToTLSExecModels(GA, DAG, model);
2399 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002400 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002401}
2402
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002403SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002404 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002405 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002406 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002407 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002408 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Rafael Espindola6de96a12009-01-15 20:18:42 +00002409 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002410 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002411 ARMConstantPoolConstant::Create(GV,
2412 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002413 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002414 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson7117a912009-03-20 22:42:55 +00002415 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002416 CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002417 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002418 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002419 SDValue Chain = Result.getValue(1);
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002420 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00002421 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002422 if (!UseGOTOFF)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002423 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002424 MachinePointerInfo::getGOT(),
2425 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002426 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002427 }
2428
2429 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002430 // pair. This is always cheaper.
2431 if (Subtarget->useMovt()) {
Evan Cheng68aec142011-01-19 02:16:49 +00002432 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002433 // FIXME: Once remat is capable of dealing with instructions with register
2434 // operands, expand this into two nodes.
2435 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2436 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002437 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002438 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2439 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2440 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2441 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002442 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002443 }
2444}
2445
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002446SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002447 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002448 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002449 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002450 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002451 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002452
Tim Northover72360d22013-12-02 10:35:41 +00002453 if (Subtarget->useMovt())
Evan Cheng68aec142011-01-19 02:16:49 +00002454 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002455
Tim Northover72360d22013-12-02 10:35:41 +00002456 // FIXME: Once remat is capable of dealing with instructions with register
2457 // operands, expand this into multiple nodes
2458 unsigned Wrapper =
2459 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002460
Tim Northover72360d22013-12-02 10:35:41 +00002461 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2462 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002463
Evan Cheng1b389522009-09-03 07:04:02 +00002464 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002465 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2466 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002467 return Result;
2468}
2469
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002470SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002471 SelectionDAG &DAG) const {
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002472 assert(Subtarget->isTargetELF() &&
2473 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Cheng408aa562009-11-06 22:24:13 +00002474 MachineFunction &MF = DAG.getMachineFunction();
2475 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002476 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002477 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002478 SDLoc dl(Op);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002479 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingc214cb02011-10-01 08:58:29 +00002480 ARMConstantPoolValue *CPV =
2481 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2482 ARMPCLabelIndex, PCAdj);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002483 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002484 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002485 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002486 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002487 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00002488 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002489 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002490}
2491
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002492SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002493ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002494 SDLoc dl(Op);
Jim Grosbachfaa3abb2010-05-27 23:49:24 +00002495 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002496 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2497 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002498 Op.getOperand(1), Val);
2499}
2500
2501SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002502ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002503 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002504 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2505 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2506}
2507
2508SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002509ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002510 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002511 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002512 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002513 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002514 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson17f88782009-08-04 00:25:01 +00002515 case Intrinsic::arm_thread_pointer: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002516 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson17f88782009-08-04 00:25:01 +00002517 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2518 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002519 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002520 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002521 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002522 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002523 EVT PtrVT = getPointerTy();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002524 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2525 SDValue CPAddr;
2526 unsigned PCAdj = (RelocM != Reloc::PIC_)
2527 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002528 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002529 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2530 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002531 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002532 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002533 SDValue Result =
Evan Chengcdbb70c2009-10-31 03:39:36 +00002534 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002535 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002536 false, false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002537
2538 if (RelocM == Reloc::PIC_) {
Evan Cheng408aa562009-11-06 22:24:13 +00002539 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002540 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2541 }
2542 return Result;
2543 }
Evan Cheng18381b42011-03-29 23:06:19 +00002544 case Intrinsic::arm_neon_vmulls:
2545 case Intrinsic::arm_neon_vmullu: {
2546 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2547 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002548 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002549 Op.getOperand(1), Op.getOperand(2));
2550 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002551 }
2552}
2553
Eli Friedman30a49e92011-08-03 21:06:02 +00002554static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2555 const ARMSubtarget *Subtarget) {
2556 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002557 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00002558 if (!Subtarget->hasDataBarrier()) {
2559 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2560 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2561 // here.
2562 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00002563 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00002564 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00002565 DAG.getConstant(0, MVT::i32));
2566 }
2567
Tim Northover36b24172013-07-03 09:20:36 +00002568 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
2569 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
2570 unsigned Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00002571 if (Subtarget->isMClass()) {
2572 // Only a full system barrier exists in the M-class architectures.
2573 Domain = ARM_MB::SY;
2574 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00002575 // Swift happens to implement ISHST barriers in a way that's compatible with
2576 // Release semantics but weaker than ISH so we'd be fools not to use
2577 // it. Beware: other processors probably don't!
2578 Domain = ARM_MB::ISHST;
2579 }
2580
Joey Gouly926d3f52013-09-05 15:35:24 +00002581 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
2582 DAG.getConstant(Intrinsic::arm_dmb, MVT::i32),
Tim Northover36b24172013-07-03 09:20:36 +00002583 DAG.getConstant(Domain, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002584}
2585
Evan Cheng8740ee32010-11-03 06:34:55 +00002586static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2587 const ARMSubtarget *Subtarget) {
2588 // ARM pre v5TE and Thumb1 does not have preload instructions.
2589 if (!(Subtarget->isThumb2() ||
2590 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2591 // Just preserve the chain.
2592 return Op.getOperand(0);
2593
Andrew Trickef9de2a2013-05-25 02:42:55 +00002594 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00002595 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2596 if (!isRead &&
2597 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2598 // ARMv7 with MP extension has PLDW.
2599 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00002600
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002601 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2602 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00002603 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00002604 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002605 isData = ~isData & 1;
2606 }
Evan Cheng8740ee32010-11-03 06:34:55 +00002607
2608 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng21acf9f2010-11-04 05:19:35 +00002609 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2610 DAG.getConstant(isData, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00002611}
2612
Dan Gohman31ae5862010-04-17 14:41:14 +00002613static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2614 MachineFunction &MF = DAG.getMachineFunction();
2615 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2616
Evan Cheng10043e22007-01-19 07:51:42 +00002617 // vastart just stores the address of the VarArgsFrameIndex slot into the
2618 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002619 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002620 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00002621 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002622 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00002623 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2624 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002625}
2626
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002627SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00002628ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2629 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002630 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00002631 MachineFunction &MF = DAG.getMachineFunction();
2632 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2633
Craig Topper760b1342012-02-22 05:59:10 +00002634 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00002635 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002636 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002637 else
Craig Topperc7242e02012-04-20 07:30:17 +00002638 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002639
2640 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002641 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002642 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002643
2644 SDValue ArgValue2;
2645 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002646 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00002647 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00002648
2649 // Create load node to retrieve arguments from the stack.
2650 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Chengcdbb70c2009-10-31 03:39:36 +00002651 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002652 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002653 false, false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00002654 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00002655 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002656 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002657 }
2658
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002659 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00002660}
2661
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002662void
2663ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002664 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002665 unsigned ArgSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002666 unsigned &ArgRegsSize,
2667 unsigned &ArgRegsSaveSize)
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002668 const {
2669 unsigned NumGPRs;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002670 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2671 unsigned RBegin, REnd;
2672 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2673 NumGPRs = REnd - RBegin;
2674 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002675 unsigned int firstUnalloced;
2676 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2677 sizeof(GPRArgRegs) /
2678 sizeof(GPRArgRegs[0]));
2679 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2680 }
2681
2682 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002683 ArgRegsSize = NumGPRs * 4;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002684
2685 // If parameter is split between stack and GPRs...
Mark Seabornbe266aa2014-02-16 18:59:48 +00002686 if (NumGPRs && Align > 4 &&
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002687 (ArgRegsSize < ArgSize ||
2688 InRegsParamRecordIdx >= CCInfo.getInRegsParamsCount())) {
Mark Seabornbe266aa2014-02-16 18:59:48 +00002689 // Add padding for part of param recovered from GPRs. For example,
2690 // if Align == 8, its last byte must be at address K*8 - 1.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002691 // We need to do it, since remained (stack) part of parameter has
2692 // stack alignment, and we need to "attach" "GPRs head" without gaps
2693 // to it:
2694 // Stack:
2695 // |---- 8 bytes block ----| |---- 8 bytes block ----| |---- 8 bytes...
2696 // [ [padding] [GPRs head] ] [ Tail passed via stack ....
2697 //
2698 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2699 unsigned Padding =
Mark Seabornbe266aa2014-02-16 18:59:48 +00002700 OffsetToAlignment(ArgRegsSize + AFI->getArgRegsSaveSize(), Align);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002701 ArgRegsSaveSize = ArgRegsSize + Padding;
2702 } else
2703 // We don't need to extend regs save size for byval parameters if they
2704 // are passed via GPRs only.
2705 ArgRegsSaveSize = ArgRegsSize;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002706}
2707
2708// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00002709// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002710// byval). Either way, we allocate stack slots adjacent to the data
2711// provided by our caller, and store the unallocated registers there.
2712// If this is a variadic function, the va_list pointer will begin with
2713// these values; otherwise, this reassembles a (byval) structure that
2714// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002715// Return: The frame index registers were stored into.
2716int
2717ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002718 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002719 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002720 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002721 unsigned OffsetFromOrigArg,
2722 unsigned ArgOffset,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002723 unsigned ArgSize,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002724 bool ForceMutable,
2725 unsigned ByValStoreOffset,
2726 unsigned TotalArgRegsSaveSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002727
2728 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00002729 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002730 // Setup first unallocated register as first byval register;
2731 // eat all remained registers
2732 // (these two actions are performed by HandleByVal method).
2733 // Then, here, we initialize stack frame with
2734 // "store-reg" instructions.
2735 // Case #2. Var-args function, that doesn't contain byval parameters.
2736 // The same: eat all remained unallocated registers,
2737 // initialize stack frame.
2738
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002739 MachineFunction &MF = DAG.getMachineFunction();
2740 MachineFrameInfo *MFI = MF.getFrameInfo();
2741 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002742 unsigned firstRegToSaveIndex, lastRegToSaveIndex;
2743 unsigned RBegin, REnd;
2744 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2745 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2746 firstRegToSaveIndex = RBegin - ARM::R0;
2747 lastRegToSaveIndex = REnd - ARM::R0;
2748 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002749 firstRegToSaveIndex = CCInfo.getFirstUnallocated
Craig Topper58713212013-07-15 04:27:47 +00002750 (GPRArgRegs, array_lengthof(GPRArgRegs));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002751 lastRegToSaveIndex = 4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002752 }
2753
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002754 unsigned ArgRegsSize, ArgRegsSaveSize;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002755 computeRegArea(CCInfo, MF, InRegsParamRecordIdx, ArgSize,
2756 ArgRegsSize, ArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002757
2758 // Store any by-val regs to their spots on the stack so that they may be
2759 // loaded by deferencing the result of formal parameter pointer or va_next.
2760 // Note: once stack area for byval/varargs registers
2761 // was initialized, it can't be initialized again.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002762 if (ArgRegsSaveSize) {
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002763 unsigned Padding = ArgRegsSaveSize - ArgRegsSize;
2764
2765 if (Padding) {
2766 assert(AFI->getStoredByValParamsPadding() == 0 &&
2767 "The only parameter may be padded.");
2768 AFI->setStoredByValParamsPadding(Padding);
2769 }
2770
Oliver Stannardd55e1152014-03-05 15:25:27 +00002771 int FrameIndex = MFI->CreateFixedObject(ArgRegsSaveSize,
2772 Padding +
2773 ByValStoreOffset -
2774 (int64_t)TotalArgRegsSaveSize,
2775 false);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002776 SDValue FIN = DAG.getFrameIndex(FrameIndex, getPointerTy());
Oliver Stannardd55e1152014-03-05 15:25:27 +00002777 if (Padding) {
2778 MFI->CreateFixedObject(Padding,
2779 ArgOffset + ByValStoreOffset -
2780 (int64_t)ArgRegsSaveSize,
2781 false);
2782 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002783
2784 SmallVector<SDValue, 4> MemOps;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002785 for (unsigned i = 0; firstRegToSaveIndex < lastRegToSaveIndex;
2786 ++firstRegToSaveIndex, ++i) {
Craig Topper760b1342012-02-22 05:59:10 +00002787 const TargetRegisterClass *RC;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002788 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002789 RC = &ARM::tGPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002790 else
Craig Topperc7242e02012-04-20 07:30:17 +00002791 RC = &ARM::GPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002792
2793 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2794 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2795 SDValue Store =
2796 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002797 MachinePointerInfo(OrigArg, OffsetFromOrigArg + 4*i),
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002798 false, false, 0);
2799 MemOps.push_back(Store);
2800 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2801 DAG.getConstant(4, getPointerTy()));
2802 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002803
2804 AFI->setArgRegsSaveSize(ArgRegsSaveSize + AFI->getArgRegsSaveSize());
2805
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002806 if (!MemOps.empty())
2807 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2808 &MemOps[0], MemOps.size());
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002809 return FrameIndex;
Oliver Stannardd55e1152014-03-05 15:25:27 +00002810 } else {
2811 if (ArgSize == 0) {
2812 // We cannot allocate a zero-byte object for the first variadic argument,
2813 // so just make up a size.
2814 ArgSize = 4;
2815 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002816 // This will point to the next argument passed via stack.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002817 return MFI->CreateFixedObject(
Oliver Stannardd55e1152014-03-05 15:25:27 +00002818 ArgSize, ArgOffset, !ForceMutable);
2819 }
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002820}
2821
2822// Setup stack frame, the va_list pointer will start from.
2823void
2824ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002825 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002826 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002827 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002828 bool ForceMutable) const {
2829 MachineFunction &MF = DAG.getMachineFunction();
2830 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2831
2832 // Try to store any remaining integer argument regs
2833 // to their spots on the stack so that they may be loaded by deferencing
2834 // the result of va_next.
2835 // If there is no regs to be stored, just point address after last
2836 // argument passed via stack.
2837 int FrameIndex =
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002838 StoreByValRegs(CCInfo, DAG, dl, Chain, 0, CCInfo.getInRegsParamsCount(),
Oliver Stannardd55e1152014-03-05 15:25:27 +00002839 0, ArgOffset, 0, ForceMutable, 0, TotalArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002840
2841 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002842}
2843
Bob Wilson2e076c42009-06-22 23:27:02 +00002844SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002845ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002846 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002847 const SmallVectorImpl<ISD::InputArg>
2848 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002849 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002850 SmallVectorImpl<SDValue> &InVals)
2851 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00002852 MachineFunction &MF = DAG.getMachineFunction();
2853 MachineFrameInfo *MFI = MF.getFrameInfo();
2854
Bob Wilsona4c22902009-04-17 19:07:39 +00002855 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2856
2857 // Assign locations to all of the incoming arguments.
2858 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00002859 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2860 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002861 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002862 CCAssignFnForNode(CallConv, /* Return*/ false,
2863 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00002864
Bob Wilsona4c22902009-04-17 19:07:39 +00002865 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002866 int lastInsIndex = -1;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002867 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002868 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
2869 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002870
2871 // Initially ArgRegsSaveSize is zero.
2872 // Then we increase this value each time we meet byval parameter.
2873 // We also increase this value in case of varargs function.
2874 AFI->setArgRegsSaveSize(0);
2875
Oliver Stannardd55e1152014-03-05 15:25:27 +00002876 unsigned ByValStoreOffset = 0;
2877 unsigned TotalArgRegsSaveSize = 0;
2878 unsigned ArgRegsSaveSizeMaxAlign = 4;
2879
2880 // Calculate the amount of stack space that we need to allocate to store
2881 // byval and variadic arguments that are passed in registers.
2882 // We need to know this before we allocate the first byval or variadic
2883 // argument, as they will be allocated a stack slot below the CFA (Canonical
2884 // Frame Address, the stack pointer at entry to the function).
2885 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2886 CCValAssign &VA = ArgLocs[i];
2887 if (VA.isMemLoc()) {
2888 int index = VA.getValNo();
2889 if (index != lastInsIndex) {
2890 ISD::ArgFlagsTy Flags = Ins[index].Flags;
2891 if (Flags.isByVal()) {
2892 unsigned ExtraArgRegsSize;
2893 unsigned ExtraArgRegsSaveSize;
2894 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsProceed(),
2895 Flags.getByValSize(),
2896 ExtraArgRegsSize, ExtraArgRegsSaveSize);
2897
2898 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
2899 if (Flags.getByValAlign() > ArgRegsSaveSizeMaxAlign)
2900 ArgRegsSaveSizeMaxAlign = Flags.getByValAlign();
2901 CCInfo.nextInRegsParam();
2902 }
2903 lastInsIndex = index;
2904 }
2905 }
2906 }
2907 CCInfo.rewindByValRegsInfo();
2908 lastInsIndex = -1;
2909 if (isVarArg) {
2910 unsigned ExtraArgRegsSize;
2911 unsigned ExtraArgRegsSaveSize;
2912 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsCount(), 0,
2913 ExtraArgRegsSize, ExtraArgRegsSaveSize);
2914 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
2915 }
2916 // If the arg regs save area contains N-byte aligned values, the
2917 // bottom of it must be at least N-byte aligned.
2918 TotalArgRegsSaveSize = RoundUpToAlignment(TotalArgRegsSaveSize, ArgRegsSaveSizeMaxAlign);
2919 TotalArgRegsSaveSize = std::min(TotalArgRegsSaveSize, 16U);
2920
Bob Wilsona4c22902009-04-17 19:07:39 +00002921 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2922 CCValAssign &VA = ArgLocs[i];
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002923 std::advance(CurOrigArg, Ins[VA.getValNo()].OrigArgIndex - CurArgIdx);
2924 CurArgIdx = Ins[VA.getValNo()].OrigArgIndex;
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002925 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00002926 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002927 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00002928
Bob Wilsona4c22902009-04-17 19:07:39 +00002929 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002930 // f64 and vector types are split up into multiple registers or
2931 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00002932 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002933 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002934 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00002935 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00002936 SDValue ArgValue2;
2937 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00002938 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson699bdf72010-04-13 22:03:22 +00002939 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2940 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002941 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002942 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00002943 } else {
2944 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2945 Chain, DAG, dl);
2946 }
Owen Anderson9f944592009-08-11 20:47:22 +00002947 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2948 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00002949 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson9f944592009-08-11 20:47:22 +00002950 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00002951 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2952 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002953 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00002954
Bob Wilson2e076c42009-06-22 23:27:02 +00002955 } else {
Craig Topper760b1342012-02-22 05:59:10 +00002956 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002957
Owen Anderson9f944592009-08-11 20:47:22 +00002958 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00002959 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00002960 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00002961 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00002962 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00002963 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00002964 else if (RegVT == MVT::i32)
Craig Topperc7242e02012-04-20 07:30:17 +00002965 RC = AFI->isThumb1OnlyFunction() ?
2966 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2967 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002968 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00002969 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00002970
2971 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002972 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002973 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00002974 }
2975
2976 // If this is an 8 or 16-bit value, it is really passed promoted
2977 // to 32 bits. Insert an assert[sz]ext to capture this, then
2978 // truncate to the right size.
2979 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002980 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002981 case CCValAssign::Full: break;
2982 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002983 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00002984 break;
2985 case CCValAssign::SExt:
2986 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2987 DAG.getValueType(VA.getValVT()));
2988 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2989 break;
2990 case CCValAssign::ZExt:
2991 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2992 DAG.getValueType(VA.getValVT()));
2993 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2994 break;
2995 }
2996
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002997 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00002998
2999 } else { // VA.isRegLoc()
3000
3001 // sanity check
3002 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003003 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003004
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003005 int index = ArgLocs[i].getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003006
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003007 // Some Ins[] entries become multiple ArgLoc[] entries.
3008 // Process them only once.
3009 if (index != lastInsIndex)
3010 {
3011 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003012 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003013 // This can be changed with more analysis.
3014 // In case of tail call optimization mark all arguments mutable.
3015 // Since they could be overwritten by lowering of arguments in case of
3016 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003017 if (Flags.isByVal()) {
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003018 unsigned CurByValIndex = CCInfo.getInRegsParamsProceed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003019
3020 ByValStoreOffset = RoundUpToAlignment(ByValStoreOffset, Flags.getByValAlign());
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003021 int FrameIndex = StoreByValRegs(
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003022 CCInfo, DAG, dl, Chain, CurOrigArg,
3023 CurByValIndex,
3024 Ins[VA.getValNo()].PartOffset,
3025 VA.getLocMemOffset(),
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003026 Flags.getByValSize(),
Oliver Stannardd55e1152014-03-05 15:25:27 +00003027 true /*force mutable frames*/,
3028 ByValStoreOffset,
3029 TotalArgRegsSaveSize);
3030 ByValStoreOffset += Flags.getByValSize();
3031 ByValStoreOffset = std::min(ByValStoreOffset, 16U);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003032 InVals.push_back(DAG.getFrameIndex(FrameIndex, getPointerTy()));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003033 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003034 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003035 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003036 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003037 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003038
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003039 // Create load nodes to retrieve arguments from the stack.
3040 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3041 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
3042 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003043 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003044 }
3045 lastInsIndex = index;
3046 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003047 }
3048 }
3049
3050 // varargs
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003051 if (isVarArg)
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003052 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003053 CCInfo.getNextStackOffset(),
3054 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003055
Oliver Stannardb14c6252014-04-02 16:10:33 +00003056 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3057
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003058 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003059}
3060
3061/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003062static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003063 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003064 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003065 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003066 // Maybe this has already been legalized into the constant pool?
3067 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003068 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003069 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003070 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003071 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003072 }
3073 }
3074 return false;
3075}
3076
Evan Cheng10043e22007-01-19 07:51:42 +00003077/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3078/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003079SDValue
3080ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003081 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003082 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003083 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003084 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003085 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003086 // Constant does not fit, try adjusting it by one?
3087 switch (CC) {
3088 default: break;
3089 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003090 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003091 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003092 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003093 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003094 }
3095 break;
3096 case ISD::SETULT:
3097 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003098 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003099 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003100 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003101 }
3102 break;
3103 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003104 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003105 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003106 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003107 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003108 }
3109 break;
3110 case ISD::SETULE:
3111 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003112 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003113 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003114 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003115 }
3116 break;
3117 }
3118 }
3119 }
3120
3121 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003122 ARMISD::NodeType CompareType;
3123 switch (CondCode) {
3124 default:
3125 CompareType = ARMISD::CMP;
3126 break;
3127 case ARMCC::EQ:
3128 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003129 // Uses only Z Flag
3130 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003131 break;
3132 }
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003133 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003134 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003135}
3136
3137/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003138SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003139ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003140 SDLoc dl) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003141 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003142 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003143 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003144 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003145 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3146 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003147}
3148
Bob Wilson45acbd02011-03-08 01:17:20 +00003149/// duplicateCmp - Glue values can have only one use, so this function
3150/// duplicates a comparison node.
3151SDValue
3152ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3153 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003154 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003155 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3156 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3157
3158 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3159 Cmp = Cmp.getOperand(0);
3160 Opc = Cmp.getOpcode();
3161 if (Opc == ARMISD::CMPFP)
3162 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3163 else {
3164 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3165 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3166 }
3167 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3168}
3169
Bill Wendling6a981312010-08-11 08:43:16 +00003170SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3171 SDValue Cond = Op.getOperand(0);
3172 SDValue SelectTrue = Op.getOperand(1);
3173 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003174 SDLoc dl(Op);
Bill Wendling6a981312010-08-11 08:43:16 +00003175
3176 // Convert:
3177 //
3178 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3179 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3180 //
3181 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3182 const ConstantSDNode *CMOVTrue =
3183 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3184 const ConstantSDNode *CMOVFalse =
3185 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3186
3187 if (CMOVTrue && CMOVFalse) {
3188 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3189 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3190
3191 SDValue True;
3192 SDValue False;
3193 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3194 True = SelectTrue;
3195 False = SelectFalse;
3196 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3197 True = SelectFalse;
3198 False = SelectTrue;
3199 }
3200
3201 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003202 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003203 SDValue ARMcc = Cond.getOperand(2);
3204 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003205 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003206 assert(True.getValueType() == VT);
3207 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendling6a981312010-08-11 08:43:16 +00003208 }
3209 }
3210 }
3211
Dan Gohmand4a77c42012-02-24 00:09:36 +00003212 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3213 // undefined bits before doing a full-word comparison with zero.
3214 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
3215 DAG.getConstant(1, Cond.getValueType()));
3216
Bill Wendling6a981312010-08-11 08:43:16 +00003217 return DAG.getSelectCC(dl, Cond,
3218 DAG.getConstant(0, Cond.getValueType()),
3219 SelectTrue, SelectFalse, ISD::SETNE);
3220}
3221
Joey Gouly881eab52013-08-22 15:29:11 +00003222static ISD::CondCode getInverseCCForVSEL(ISD::CondCode CC) {
3223 if (CC == ISD::SETNE)
3224 return ISD::SETEQ;
Weiming Zhao63871d22013-12-18 22:25:17 +00003225 return ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003226}
3227
3228static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3229 bool &swpCmpOps, bool &swpVselOps) {
3230 // Start by selecting the GE condition code for opcodes that return true for
3231 // 'equality'
3232 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3233 CC == ISD::SETULE)
3234 CondCode = ARMCC::GE;
3235
3236 // and GT for opcodes that return false for 'equality'.
3237 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3238 CC == ISD::SETULT)
3239 CondCode = ARMCC::GT;
3240
3241 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3242 // to swap the compare operands.
3243 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3244 CC == ISD::SETULT)
3245 swpCmpOps = true;
3246
3247 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3248 // If we have an unordered opcode, we need to swap the operands to the VSEL
3249 // instruction (effectively negating the condition).
3250 //
3251 // This also has the effect of swapping which one of 'less' or 'greater'
3252 // returns true, so we also swap the compare operands. It also switches
3253 // whether we return true for 'equality', so we compensate by picking the
3254 // opposite condition code to our original choice.
3255 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3256 CC == ISD::SETUGT) {
3257 swpCmpOps = !swpCmpOps;
3258 swpVselOps = !swpVselOps;
3259 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3260 }
3261
3262 // 'ordered' is 'anything but unordered', so use the VS condition code and
3263 // swap the VSEL operands.
3264 if (CC == ISD::SETO) {
3265 CondCode = ARMCC::VS;
3266 swpVselOps = true;
3267 }
3268
3269 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3270 // code and swap the VSEL operands.
3271 if (CC == ISD::SETUNE) {
3272 CondCode = ARMCC::EQ;
3273 swpVselOps = true;
3274 }
3275}
3276
Dan Gohman21cea8a2010-04-17 15:26:15 +00003277SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003278 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003279 SDValue LHS = Op.getOperand(0);
3280 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003281 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003282 SDValue TrueVal = Op.getOperand(2);
3283 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003284 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003285
Owen Anderson9f944592009-08-11 20:47:22 +00003286 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003287 // Try to generate VSEL on ARMv8.
3288 // The VSEL instruction can't use all the usual ARM condition
3289 // codes: it only has two bits to select the condition code, so it's
3290 // constrained to use only GE, GT, VS and EQ.
3291 //
3292 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3293 // swap the operands of the previous compare instruction (effectively
3294 // inverting the compare condition, swapping 'less' and 'greater') and
3295 // sometimes need to swap the operands to the VSEL (which inverts the
3296 // condition in the sense of firing whenever the previous condition didn't)
Joey Goulyccd04892013-09-13 13:46:57 +00003297 if (getSubtarget()->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
Joey Gouly881eab52013-08-22 15:29:11 +00003298 TrueVal.getValueType() == MVT::f64)) {
3299 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3300 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3301 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
3302 CC = getInverseCCForVSEL(CC);
3303 std::swap(TrueVal, FalseVal);
3304 }
3305 }
3306
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003307 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003308 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003309 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Joey Gouly881eab52013-08-22 15:29:11 +00003310 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3311 Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003312 }
3313
3314 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003315 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003316
Joey Gouly881eab52013-08-22 15:29:11 +00003317 // Try to generate VSEL on ARMv8.
Joey Goulyccd04892013-09-13 13:46:57 +00003318 if (getSubtarget()->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
Joey Gouly881eab52013-08-22 15:29:11 +00003319 TrueVal.getValueType() == MVT::f64)) {
Joey Goulye3dd6842013-08-23 12:01:13 +00003320 // We can select VMAXNM/VMINNM from a compare followed by a select with the
3321 // same operands, as follows:
3322 // c = fcmp [ogt, olt, ugt, ult] a, b
3323 // select c, a, b
3324 // We only do this in unsafe-fp-math, because signed zeros and NaNs are
3325 // handled differently than the original code sequence.
3326 if (getTargetMachine().Options.UnsafeFPMath && LHS == TrueVal &&
3327 RHS == FalseVal) {
3328 if (CC == ISD::SETOGT || CC == ISD::SETUGT)
3329 return DAG.getNode(ARMISD::VMAXNM, dl, VT, TrueVal, FalseVal);
3330 if (CC == ISD::SETOLT || CC == ISD::SETULT)
3331 return DAG.getNode(ARMISD::VMINNM, dl, VT, TrueVal, FalseVal);
3332 }
3333
Joey Gouly881eab52013-08-22 15:29:11 +00003334 bool swpCmpOps = false;
3335 bool swpVselOps = false;
3336 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3337
3338 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3339 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3340 if (swpCmpOps)
3341 std::swap(LHS, RHS);
3342 if (swpVselOps)
3343 std::swap(TrueVal, FalseVal);
3344 }
3345 }
3346
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003347 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3348 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003349 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00003350 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003351 ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003352 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003353 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003354 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003355 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson7117a912009-03-20 22:42:55 +00003356 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003357 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Cheng10043e22007-01-19 07:51:42 +00003358 }
3359 return Result;
3360}
3361
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003362/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3363/// to morph to an integer compare sequence.
3364static bool canChangeToInt(SDValue Op, bool &SeenZero,
3365 const ARMSubtarget *Subtarget) {
3366 SDNode *N = Op.getNode();
3367 if (!N->hasOneUse())
3368 // Otherwise it requires moving the value from fp to integer registers.
3369 return false;
3370 if (!N->getNumValues())
3371 return false;
3372 EVT VT = Op.getValueType();
3373 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3374 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3375 // vmrs are very slow, e.g. cortex-a8.
3376 return false;
3377
3378 if (isFloatingPointZero(Op)) {
3379 SeenZero = true;
3380 return true;
3381 }
3382 return ISD::isNormalLoad(N);
3383}
3384
3385static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3386 if (isFloatingPointZero(Op))
3387 return DAG.getConstant(0, MVT::i32);
3388
3389 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003390 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003391 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003392 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003393 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003394
3395 llvm_unreachable("Unknown VFP cmp argument!");
3396}
3397
3398static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3399 SDValue &RetVal1, SDValue &RetVal2) {
3400 if (isFloatingPointZero(Op)) {
3401 RetVal1 = DAG.getConstant(0, MVT::i32);
3402 RetVal2 = DAG.getConstant(0, MVT::i32);
3403 return;
3404 }
3405
3406 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3407 SDValue Ptr = Ld->getBasePtr();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003408 RetVal1 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003409 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003410 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003411 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003412 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003413
3414 EVT PtrType = Ptr.getValueType();
3415 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003416 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003417 PtrType, Ptr, DAG.getConstant(4, PtrType));
Andrew Trickef9de2a2013-05-25 02:42:55 +00003418 RetVal2 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003419 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003420 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003421 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003422 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003423 return;
3424 }
3425
3426 llvm_unreachable("Unknown VFP cmp argument!");
3427}
3428
3429/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3430/// f32 and even f64 comparisons to integer ones.
3431SDValue
3432ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3433 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003434 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003435 SDValue LHS = Op.getOperand(2);
3436 SDValue RHS = Op.getOperand(3);
3437 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003438 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003439
Evan Chengd12af5d2012-03-01 23:27:13 +00003440 bool LHSSeenZero = false;
3441 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3442 bool RHSSeenZero = false;
3443 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3444 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003445 // If unsafe fp math optimization is enabled and there are no other uses of
3446 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003447 // to an integer comparison.
3448 if (CC == ISD::SETOEQ)
3449 CC = ISD::SETEQ;
3450 else if (CC == ISD::SETUNE)
3451 CC = ISD::SETNE;
3452
Evan Chengd12af5d2012-03-01 23:27:13 +00003453 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003454 SDValue ARMcc;
3455 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003456 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3457 bitcastf32Toi32(LHS, DAG), Mask);
3458 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3459 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003460 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3461 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3462 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3463 Chain, Dest, ARMcc, CCR, Cmp);
3464 }
3465
3466 SDValue LHS1, LHS2;
3467 SDValue RHS1, RHS2;
3468 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3469 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003470 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3471 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003472 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3473 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003474 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003475 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3476 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3477 }
3478
3479 return SDValue();
3480}
3481
3482SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3483 SDValue Chain = Op.getOperand(0);
3484 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3485 SDValue LHS = Op.getOperand(2);
3486 SDValue RHS = Op.getOperand(3);
3487 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003488 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003489
Owen Anderson9f944592009-08-11 20:47:22 +00003490 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003491 SDValue ARMcc;
3492 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003493 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003494 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003495 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003496 }
3497
Owen Anderson9f944592009-08-11 20:47:22 +00003498 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003499
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003500 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003501 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3502 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3503 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3504 if (Result.getNode())
3505 return Result;
3506 }
3507
Evan Cheng10043e22007-01-19 07:51:42 +00003508 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003509 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003510
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003511 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3512 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003513 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003514 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003515 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesen400dc2e2009-02-06 21:50:26 +00003516 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Cheng10043e22007-01-19 07:51:42 +00003517 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003518 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3519 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesen400dc2e2009-02-06 21:50:26 +00003520 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Cheng10043e22007-01-19 07:51:42 +00003521 }
3522 return Res;
3523}
3524
Dan Gohman21cea8a2010-04-17 15:26:15 +00003525SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003526 SDValue Chain = Op.getOperand(0);
3527 SDValue Table = Op.getOperand(1);
3528 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003529 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003530
Owen Anderson53aa7a92009-08-10 22:56:29 +00003531 EVT PTy = getPointerTy();
Evan Cheng10043e22007-01-19 07:51:42 +00003532 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3533 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3f17aee2009-07-14 18:44:34 +00003534 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003535 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson9f944592009-08-11 20:47:22 +00003536 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chengc8bed032009-07-28 20:53:24 +00003537 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3538 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003539 if (Subtarget->isThumb2()) {
3540 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3541 // which does another jump to the destination. This also makes it easier
3542 // to translate it to TBB / TBH later.
3543 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00003544 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Chengc6d70ae2009-07-29 02:18:14 +00003545 Addr, Op.getOperand(2), JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003546 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00003547 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003548 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattner7727d052010-09-21 06:44:06 +00003549 MachinePointerInfo::getJumpTable(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003550 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003551 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00003552 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson9f944592009-08-11 20:47:22 +00003553 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003554 } else {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003555 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003556 MachinePointerInfo::getJumpTable(),
3557 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003558 Chain = Addr.getValue(1);
Owen Anderson9f944592009-08-11 20:47:22 +00003559 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003560 }
Evan Cheng10043e22007-01-19 07:51:42 +00003561}
3562
Eli Friedman2d4055b2011-11-09 23:36:02 +00003563static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00003564 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003565 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003566
James Molloy547d4c02012-02-20 09:24:05 +00003567 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3568 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3569 return Op;
3570 return DAG.UnrollVectorOp(Op.getNode());
3571 }
3572
3573 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3574 "Invalid type for custom lowering!");
3575 if (VT != MVT::v4i16)
3576 return DAG.UnrollVectorOp(Op.getNode());
3577
3578 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3579 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003580}
3581
Bob Wilsone4191e72010-03-19 22:51:32 +00003582static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman2d4055b2011-11-09 23:36:02 +00003583 EVT VT = Op.getValueType();
3584 if (VT.isVector())
3585 return LowerVectorFP_TO_INT(Op, DAG);
3586
Andrew Trickef9de2a2013-05-25 02:42:55 +00003587 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003588 unsigned Opc;
3589
3590 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003591 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003592 case ISD::FP_TO_SINT:
3593 Opc = ARMISD::FTOSI;
3594 break;
3595 case ISD::FP_TO_UINT:
3596 Opc = ARMISD::FTOUI;
3597 break;
3598 }
3599 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peck527da1b2010-11-23 03:31:01 +00003600 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003601}
3602
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003603static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3604 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003605 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003606
Eli Friedman2d4055b2011-11-09 23:36:02 +00003607 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3608 if (VT.getVectorElementType() == MVT::f32)
3609 return Op;
3610 return DAG.UnrollVectorOp(Op.getNode());
3611 }
3612
Duncan Sandsa41634e2011-08-12 14:54:45 +00003613 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3614 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003615 if (VT != MVT::v4f32)
3616 return DAG.UnrollVectorOp(Op.getNode());
3617
3618 unsigned CastOpc;
3619 unsigned Opc;
3620 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003621 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003622 case ISD::SINT_TO_FP:
3623 CastOpc = ISD::SIGN_EXTEND;
3624 Opc = ISD::SINT_TO_FP;
3625 break;
3626 case ISD::UINT_TO_FP:
3627 CastOpc = ISD::ZERO_EXTEND;
3628 Opc = ISD::UINT_TO_FP;
3629 break;
3630 }
3631
3632 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3633 return DAG.getNode(Opc, dl, VT, Op);
3634}
3635
Bob Wilsone4191e72010-03-19 22:51:32 +00003636static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3637 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003638 if (VT.isVector())
3639 return LowerVectorINT_TO_FP(Op, DAG);
3640
Andrew Trickef9de2a2013-05-25 02:42:55 +00003641 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003642 unsigned Opc;
3643
3644 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003645 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003646 case ISD::SINT_TO_FP:
3647 Opc = ARMISD::SITOF;
3648 break;
3649 case ISD::UINT_TO_FP:
3650 Opc = ARMISD::UITOF;
3651 break;
3652 }
3653
Wesley Peck527da1b2010-11-23 03:31:01 +00003654 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilsone4191e72010-03-19 22:51:32 +00003655 return DAG.getNode(Opc, dl, VT, Op);
3656}
3657
Evan Cheng25f93642010-07-08 02:08:50 +00003658SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00003659 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003660 SDValue Tmp0 = Op.getOperand(0);
3661 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003662 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003663 EVT VT = Op.getValueType();
3664 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00003665 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3666 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3667 bool UseNEON = !InGPR && Subtarget->hasNEON();
3668
3669 if (UseNEON) {
3670 // Use VBSL to copy the sign bit.
3671 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3672 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3673 DAG.getTargetConstant(EncodedVal, MVT::i32));
3674 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3675 if (VT == MVT::f64)
3676 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3677 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3678 DAG.getConstant(32, MVT::i32));
3679 else /*if (VT == MVT::f32)*/
3680 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3681 if (SrcVT == MVT::f32) {
3682 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3683 if (VT == MVT::f64)
3684 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3685 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3686 DAG.getConstant(32, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00003687 } else if (VT == MVT::f32)
3688 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3689 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3690 DAG.getConstant(32, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00003691 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3692 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3693
3694 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3695 MVT::i32);
3696 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3697 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3698 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00003699
Evan Chengd6b641e2011-02-23 02:24:55 +00003700 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3701 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3702 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00003703 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00003704 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3705 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3706 DAG.getConstant(0, MVT::i32));
3707 } else {
3708 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3709 }
3710
3711 return Res;
3712 }
Evan Cheng2da1c952011-02-11 02:28:55 +00003713
3714 // Bitcast operand 1 to i32.
3715 if (SrcVT == MVT::f64)
3716 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3717 &Tmp1, 1).getValue(1);
3718 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3719
Evan Chengd6b641e2011-02-23 02:24:55 +00003720 // Or in the signbit with integer operations.
3721 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3722 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3723 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3724 if (VT == MVT::f32) {
3725 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3726 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3727 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3728 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00003729 }
3730
Evan Chengd6b641e2011-02-23 02:24:55 +00003731 // f64: Or the high part with signbit and then combine two parts.
3732 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3733 &Tmp0, 1);
3734 SDValue Lo = Tmp0.getValue(0);
3735 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3736 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3737 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00003738}
3739
Evan Cheng168ced92010-05-22 01:47:14 +00003740SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3741 MachineFunction &MF = DAG.getMachineFunction();
3742 MachineFrameInfo *MFI = MF.getFrameInfo();
3743 MFI->setReturnAddressIsTaken(true);
3744
Bill Wendling908bf812014-01-06 00:43:20 +00003745 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00003746 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00003747
Evan Cheng168ced92010-05-22 01:47:14 +00003748 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003749 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00003750 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3751 if (Depth) {
3752 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3753 SDValue Offset = DAG.getConstant(4, MVT::i32);
3754 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3755 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003756 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00003757 }
3758
3759 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00003760 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00003761 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3762}
3763
Dan Gohman21cea8a2010-04-17 15:26:15 +00003764SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003765 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3766 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00003767
Owen Anderson53aa7a92009-08-10 22:56:29 +00003768 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003769 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003770 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Tim Northoverd6a729b2014-01-06 14:28:05 +00003771 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetMachO())
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003772 ? ARM::R7 : ARM::R11;
3773 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3774 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00003775 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3776 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003777 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003778 return FrameAddr;
3779}
3780
Wesley Peck527da1b2010-11-23 03:31:01 +00003781/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00003782/// expand a bit convert where either the source or destination type is i64 to
3783/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3784/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3785/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00003786static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00003787 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003788 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003789 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00003790
Bob Wilson59b70ea2010-04-17 05:30:19 +00003791 // This function is only supposed to be called for i64 types, either as the
3792 // source or destination of the bit convert.
3793 EVT SrcVT = Op.getValueType();
3794 EVT DstVT = N->getValueType(0);
3795 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00003796 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00003797
Bob Wilson59b70ea2010-04-17 05:30:19 +00003798 // Turn i64->f64 into VMOVDRR.
3799 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson9f944592009-08-11 20:47:22 +00003800 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3801 DAG.getConstant(0, MVT::i32));
3802 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3803 DAG.getConstant(1, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00003804 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00003805 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00003806 }
Bob Wilson7117a912009-03-20 22:42:55 +00003807
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00003808 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00003809 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3810 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3811 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3812 // Merge the pieces into a single i64 value.
3813 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3814 }
Bob Wilson7117a912009-03-20 22:42:55 +00003815
Bob Wilson59b70ea2010-04-17 05:30:19 +00003816 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00003817}
3818
Bob Wilson2e076c42009-06-22 23:27:02 +00003819/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00003820/// Zero vectors are used to represent vector negation and in those cases
3821/// will be implemented with the NEON VNEG instruction. However, VNEG does
3822/// not support i64 elements, so sometimes the zero vectors will need to be
3823/// explicitly constructed. Regardless, use a canonical VMOV to create the
3824/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003825static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003826 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00003827 // The canonical modified immediate encoding of a zero vector is....0!
3828 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3829 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3830 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00003831 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00003832}
3833
Jim Grosbach624fcb22009-10-31 21:00:56 +00003834/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3835/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00003836SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3837 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00003838 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3839 EVT VT = Op.getValueType();
3840 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003841 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00003842 SDValue ShOpLo = Op.getOperand(0);
3843 SDValue ShOpHi = Op.getOperand(1);
3844 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003845 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00003846 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00003847
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00003848 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3849
Jim Grosbach624fcb22009-10-31 21:00:56 +00003850 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3851 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3852 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3853 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3854 DAG.getConstant(VTBits, MVT::i32));
3855 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3856 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00003857 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00003858
3859 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3860 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003861 ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00003862 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003863 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00003864 CCR, Cmp);
3865
3866 SDValue Ops[2] = { Lo, Hi };
3867 return DAG.getMergeValues(Ops, 2, dl);
3868}
3869
Jim Grosbach5d994042009-10-31 19:38:01 +00003870/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3871/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00003872SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3873 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00003874 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3875 EVT VT = Op.getValueType();
3876 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003877 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00003878 SDValue ShOpLo = Op.getOperand(0);
3879 SDValue ShOpHi = Op.getOperand(1);
3880 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003881 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00003882
3883 assert(Op.getOpcode() == ISD::SHL_PARTS);
3884 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3885 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3886 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3887 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3888 DAG.getConstant(VTBits, MVT::i32));
3889 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3890 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3891
3892 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3893 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3894 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003895 ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00003896 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003897 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00003898 CCR, Cmp);
3899
3900 SDValue Ops[2] = { Lo, Hi };
3901 return DAG.getMergeValues(Ops, 2, dl);
3902}
3903
Jim Grosbach535d3b42010-09-08 03:54:02 +00003904SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00003905 SelectionDAG &DAG) const {
3906 // The rounding mode is in bits 23:22 of the FPSCR.
3907 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3908 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3909 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003910 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00003911 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3912 DAG.getConstant(Intrinsic::arm_get_fpscr,
3913 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00003914 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemanb69b1822010-08-03 21:31:55 +00003915 DAG.getConstant(1U << 22, MVT::i32));
3916 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3917 DAG.getConstant(22, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00003918 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemanb69b1822010-08-03 21:31:55 +00003919 DAG.getConstant(3, MVT::i32));
3920}
3921
Jim Grosbach8546ec92010-01-18 19:58:49 +00003922static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3923 const ARMSubtarget *ST) {
3924 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003925 SDLoc dl(N);
Jim Grosbach8546ec92010-01-18 19:58:49 +00003926
3927 if (!ST->hasV6T2Ops())
3928 return SDValue();
3929
3930 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3931 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3932}
3933
Evan Chengb4eae132012-12-04 22:41:50 +00003934/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
3935/// for each 16-bit element from operand, repeated. The basic idea is to
3936/// leverage vcnt to get the 8-bit counts, gather and add the results.
3937///
3938/// Trace for v4i16:
3939/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
3940/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
3941/// 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 +00003942/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00003943/// [b0 b1 b2 b3 b4 b5 b6 b7]
3944/// +[b1 b0 b3 b2 b5 b4 b7 b6]
3945/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
3946/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
3947static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
3948 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003949 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00003950
3951 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
3952 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
3953 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
3954 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
3955 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
3956 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
3957}
3958
3959/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
3960/// bit-count for each 16-bit element from the operand. We need slightly
3961/// different sequencing for v4i16 and v8i16 to stay within NEON's available
3962/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00003963///
Evan Chengb4eae132012-12-04 22:41:50 +00003964/// Trace for v4i16:
3965/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
3966/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
3967/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
3968/// v4i16:Extracted = [k0 k1 k2 k3 ]
3969static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
3970 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003971 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00003972
3973 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
3974 if (VT.is64BitVector()) {
3975 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
3976 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
3977 DAG.getIntPtrConstant(0));
3978 } else {
3979 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
3980 BitCounts, DAG.getIntPtrConstant(0));
3981 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
3982 }
3983}
3984
3985/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
3986/// bit-count for each 32-bit element from the operand. The idea here is
3987/// to split the vector into 16-bit elements, leverage the 16-bit count
3988/// routine, and then combine the results.
3989///
3990/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
3991/// input = [v0 v1 ] (vi: 32-bit elements)
3992/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
3993/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00003994/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00003995/// [k0 k1 k2 k3 ]
3996/// N1 =+[k1 k0 k3 k2 ]
3997/// [k0 k2 k1 k3 ]
3998/// N2 =+[k1 k3 k0 k2 ]
3999/// [k0 k2 k1 k3 ]
4000/// Extended =+[k1 k3 k0 k2 ]
4001/// [k0 k2 ]
4002/// Extracted=+[k1 k3 ]
4003///
4004static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4005 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004006 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004007
4008 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4009
4010 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4011 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4012 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4013 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4014 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4015
4016 if (VT.is64BitVector()) {
4017 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4018 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
4019 DAG.getIntPtrConstant(0));
4020 } else {
4021 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
4022 DAG.getIntPtrConstant(0));
4023 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4024 }
4025}
4026
4027static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4028 const ARMSubtarget *ST) {
4029 EVT VT = N->getValueType(0);
4030
4031 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004032 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4033 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004034 "Unexpected type for custom ctpop lowering");
4035
4036 if (VT.getVectorElementType() == MVT::i32)
4037 return lowerCTPOP32BitElements(N, DAG);
4038 else
4039 return lowerCTPOP16BitElements(N, DAG);
4040}
4041
Bob Wilson2e076c42009-06-22 23:27:02 +00004042static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4043 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004044 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004045 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004046
Bob Wilson7d471332010-11-18 21:16:28 +00004047 if (!VT.isVector())
4048 return SDValue();
4049
Bob Wilson2e076c42009-06-22 23:27:02 +00004050 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004051 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004052
Bob Wilson7d471332010-11-18 21:16:28 +00004053 // Left shifts translate directly to the vshiftu intrinsic.
4054 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004055 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilson7d471332010-11-18 21:16:28 +00004056 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
4057 N->getOperand(0), N->getOperand(1));
4058
4059 assert((N->getOpcode() == ISD::SRA ||
4060 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4061
4062 // NEON uses the same intrinsics for both left and right shifts. For
4063 // right shifts, the shift amounts are negative, so negate the vector of
4064 // shift amounts.
4065 EVT ShiftVT = N->getOperand(1).getValueType();
4066 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4067 getZeroVector(ShiftVT, DAG, dl),
4068 N->getOperand(1));
4069 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4070 Intrinsic::arm_neon_vshifts :
4071 Intrinsic::arm_neon_vshiftu);
4072 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4073 DAG.getConstant(vshiftInt, MVT::i32),
4074 N->getOperand(0), NegatedCount);
4075}
4076
4077static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4078 const ARMSubtarget *ST) {
4079 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004080 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004081
Eli Friedman682d8c12009-08-22 03:13:10 +00004082 // We can get here for a node like i32 = ISD::SHL i32, i64
4083 if (VT != MVT::i64)
4084 return SDValue();
4085
4086 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004087 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004088
Chris Lattnerf81d5882007-11-24 07:07:01 +00004089 // We only lower SRA, SRL of 1 here, all others use generic lowering.
4090 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmaneffb8942008-09-12 16:56:44 +00004091 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands6ed40142008-12-01 11:39:25 +00004092 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004093
Chris Lattnerf81d5882007-11-24 07:07:01 +00004094 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004095 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004096
Chris Lattnerf81d5882007-11-24 07:07:01 +00004097 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004098 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004099 DAG.getConstant(0, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004100 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004101 DAG.getConstant(1, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004102
Chris Lattnerf81d5882007-11-24 07:07:01 +00004103 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4104 // captures the result into a carry flag.
4105 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004106 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson7117a912009-03-20 22:42:55 +00004107
Chris Lattnerf81d5882007-11-24 07:07:01 +00004108 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004109 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004110
Chris Lattnerf81d5882007-11-24 07:07:01 +00004111 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004112 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004113}
4114
Bob Wilson2e076c42009-06-22 23:27:02 +00004115static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4116 SDValue TmpOp0, TmpOp1;
4117 bool Invert = false;
4118 bool Swap = false;
4119 unsigned Opc = 0;
4120
4121 SDValue Op0 = Op.getOperand(0);
4122 SDValue Op1 = Op.getOperand(1);
4123 SDValue CC = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004124 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004125 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004126 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004127
4128 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
4129 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004130 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004131 case ISD::SETUNE:
4132 case ISD::SETNE: Invert = true; // Fallthrough
4133 case ISD::SETOEQ:
4134 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4135 case ISD::SETOLT:
4136 case ISD::SETLT: Swap = true; // Fallthrough
4137 case ISD::SETOGT:
4138 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4139 case ISD::SETOLE:
4140 case ISD::SETLE: Swap = true; // Fallthrough
4141 case ISD::SETOGE:
4142 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4143 case ISD::SETUGE: Swap = true; // Fallthrough
4144 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4145 case ISD::SETUGT: Swap = true; // Fallthrough
4146 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4147 case ISD::SETUEQ: Invert = true; // Fallthrough
4148 case ISD::SETONE:
4149 // Expand this to (OLT | OGT).
4150 TmpOp0 = Op0;
4151 TmpOp1 = Op1;
4152 Opc = ISD::OR;
4153 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
4154 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
4155 break;
4156 case ISD::SETUO: Invert = true; // Fallthrough
4157 case ISD::SETO:
4158 // Expand this to (OLT | OGE).
4159 TmpOp0 = Op0;
4160 TmpOp1 = Op1;
4161 Opc = ISD::OR;
4162 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
4163 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
4164 break;
4165 }
4166 } else {
4167 // Integer comparisons.
4168 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004169 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004170 case ISD::SETNE: Invert = true;
4171 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4172 case ISD::SETLT: Swap = true;
4173 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4174 case ISD::SETLE: Swap = true;
4175 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4176 case ISD::SETULT: Swap = true;
4177 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4178 case ISD::SETULE: Swap = true;
4179 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4180 }
4181
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004182 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004183 if (Opc == ARMISD::VCEQ) {
4184
4185 SDValue AndOp;
4186 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4187 AndOp = Op0;
4188 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4189 AndOp = Op1;
4190
4191 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004192 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004193 AndOp = AndOp.getOperand(0);
4194
4195 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4196 Opc = ARMISD::VTST;
Wesley Peck527da1b2010-11-23 03:31:01 +00004197 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
4198 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004199 Invert = !Invert;
4200 }
4201 }
4202 }
4203
4204 if (Swap)
4205 std::swap(Op0, Op1);
4206
Owen Andersonc7baee32010-11-08 23:21:22 +00004207 // If one of the operands is a constant vector zero, attempt to fold the
4208 // comparison to a specialized compare-against-zero form.
4209 SDValue SingleOp;
4210 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4211 SingleOp = Op0;
4212 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4213 if (Opc == ARMISD::VCGE)
4214 Opc = ARMISD::VCLEZ;
4215 else if (Opc == ARMISD::VCGT)
4216 Opc = ARMISD::VCLTZ;
4217 SingleOp = Op1;
4218 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004219
Owen Andersonc7baee32010-11-08 23:21:22 +00004220 SDValue Result;
4221 if (SingleOp.getNode()) {
4222 switch (Opc) {
4223 case ARMISD::VCEQ:
4224 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
4225 case ARMISD::VCGE:
4226 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
4227 case ARMISD::VCLEZ:
4228 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
4229 case ARMISD::VCGT:
4230 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
4231 case ARMISD::VCLTZ:
4232 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
4233 default:
4234 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
4235 }
4236 } else {
4237 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
4238 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004239
4240 if (Invert)
4241 Result = DAG.getNOT(dl, Result, VT);
4242
4243 return Result;
4244}
4245
Bob Wilson5b2b5042010-06-14 22:19:57 +00004246/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4247/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004248/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004249static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4250 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Andersona4076922010-11-05 21:57:54 +00004251 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004252 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004253
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004254 // SplatBitSize is set to the smallest size that splats the vector, so a
4255 // zero vector will always have SplatBitSize == 8. However, NEON modified
4256 // immediate instructions others than VMOV do not support the 8-bit encoding
4257 // of a zero vector, and the default encoding of zero is supposed to be the
4258 // 32-bit version.
4259 if (SplatBits == 0)
4260 SplatBitSize = 32;
4261
Bob Wilson2e076c42009-06-22 23:27:02 +00004262 switch (SplatBitSize) {
4263 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004264 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004265 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004266 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004267 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004268 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004269 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004270 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004271 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004272
4273 case 16:
4274 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004275 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004276 if ((SplatBits & ~0xff) == 0) {
4277 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004278 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004279 Imm = SplatBits;
4280 break;
4281 }
4282 if ((SplatBits & ~0xff00) == 0) {
4283 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004284 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004285 Imm = SplatBits >> 8;
4286 break;
4287 }
4288 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004289
4290 case 32:
4291 // NEON's 32-bit VMOV supports splat values where:
4292 // * only one byte is nonzero, or
4293 // * the least significant byte is 0xff and the second byte is nonzero, or
4294 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004295 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004296 if ((SplatBits & ~0xff) == 0) {
4297 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004298 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004299 Imm = SplatBits;
4300 break;
4301 }
4302 if ((SplatBits & ~0xff00) == 0) {
4303 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004304 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004305 Imm = SplatBits >> 8;
4306 break;
4307 }
4308 if ((SplatBits & ~0xff0000) == 0) {
4309 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004310 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004311 Imm = SplatBits >> 16;
4312 break;
4313 }
4314 if ((SplatBits & ~0xff000000) == 0) {
4315 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004316 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004317 Imm = SplatBits >> 24;
4318 break;
4319 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004320
Owen Andersona4076922010-11-05 21:57:54 +00004321 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4322 if (type == OtherModImm) return SDValue();
4323
Bob Wilson2e076c42009-06-22 23:27:02 +00004324 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004325 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4326 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004327 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004328 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004329 break;
4330 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004331
4332 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004333 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4334 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004335 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004336 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004337 break;
4338 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004339
4340 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4341 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4342 // VMOV.I32. A (very) minor optimization would be to replicate the value
4343 // and fall through here to test for a valid 64-bit splat. But, then the
4344 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004345 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004346
4347 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004348 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004349 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004350 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00004351 uint64_t BitMask = 0xff;
4352 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004353 unsigned ImmMask = 1;
4354 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00004355 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00004356 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004357 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00004358 Imm |= ImmMask;
4359 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004360 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004361 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004362 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004363 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00004364 }
Bob Wilson6eae5202010-06-11 21:34:50 +00004365 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004366 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00004367 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00004368 break;
4369 }
4370
Bob Wilson6eae5202010-06-11 21:34:50 +00004371 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00004372 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00004373 }
4374
Bob Wilsona3f19012010-07-13 21:16:48 +00004375 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
4376 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00004377}
4378
Lang Hames591cdaf2012-03-29 21:56:11 +00004379SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4380 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004381 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00004382 return SDValue();
4383
Tim Northoverf79c3a52013-08-20 08:57:11 +00004384 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00004385 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004386
4387 // Try splatting with a VMOV.f32...
4388 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00004389 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
4390
Lang Hames591cdaf2012-03-29 21:56:11 +00004391 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004392 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
4393 // We have code in place to select a valid ConstantFP already, no need to
4394 // do any mangling.
4395 return Op;
4396 }
4397
4398 // It's a float and we are trying to use NEON operations where
4399 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004400 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004401 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
4402 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4403 NewVal);
4404 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
4405 DAG.getConstant(0, MVT::i32));
4406 }
4407
Tim Northoverf79c3a52013-08-20 08:57:11 +00004408 // The rest of our options are NEON only, make sure that's allowed before
4409 // proceeding..
4410 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
4411 return SDValue();
4412
Lang Hames591cdaf2012-03-29 21:56:11 +00004413 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00004414 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
4415
4416 // It wouldn't really be worth bothering for doubles except for one very
4417 // important value, which does happen to match: 0.0. So make sure we don't do
4418 // anything stupid.
4419 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
4420 return SDValue();
4421
4422 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
4423 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4424 false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004425 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004426 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004427 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4428 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004429 if (IsDouble)
4430 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4431
4432 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004433 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4434 VecConstant);
4435 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4436 DAG.getConstant(0, MVT::i32));
4437 }
4438
4439 // Finally, try a VMVN.i32
Tim Northoverf79c3a52013-08-20 08:57:11 +00004440 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4441 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004442 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004443 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004444 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004445
4446 if (IsDouble)
4447 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4448
4449 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004450 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4451 VecConstant);
4452 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4453 DAG.getConstant(0, MVT::i32));
4454 }
4455
4456 return SDValue();
4457}
4458
Quentin Colombet8e1fe842012-11-02 21:32:17 +00004459// check if an VEXT instruction can handle the shuffle mask when the
4460// vector sources of the shuffle are the same.
4461static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4462 unsigned NumElts = VT.getVectorNumElements();
4463
4464 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4465 if (M[0] < 0)
4466 return false;
4467
4468 Imm = M[0];
4469
4470 // If this is a VEXT shuffle, the immediate value is the index of the first
4471 // element. The other shuffle indices must be the successive elements after
4472 // the first one.
4473 unsigned ExpectedElt = Imm;
4474 for (unsigned i = 1; i < NumElts; ++i) {
4475 // Increment the expected index. If it wraps around, just follow it
4476 // back to index zero and keep going.
4477 ++ExpectedElt;
4478 if (ExpectedElt == NumElts)
4479 ExpectedElt = 0;
4480
4481 if (M[i] < 0) continue; // ignore UNDEF indices
4482 if (ExpectedElt != static_cast<unsigned>(M[i]))
4483 return false;
4484 }
4485
4486 return true;
4487}
4488
Lang Hames591cdaf2012-03-29 21:56:11 +00004489
Benjamin Kramer339ced42012-01-15 13:16:05 +00004490static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004491 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004492 unsigned NumElts = VT.getVectorNumElements();
4493 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00004494
4495 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4496 if (M[0] < 0)
4497 return false;
4498
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004499 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00004500
4501 // If this is a VEXT shuffle, the immediate value is the index of the first
4502 // element. The other shuffle indices must be the successive elements after
4503 // the first one.
4504 unsigned ExpectedElt = Imm;
4505 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004506 // Increment the expected index. If it wraps around, it may still be
4507 // a VEXT but the source vectors must be swapped.
4508 ExpectedElt += 1;
4509 if (ExpectedElt == NumElts * 2) {
4510 ExpectedElt = 0;
4511 ReverseVEXT = true;
4512 }
4513
Bob Wilson411dfad2010-08-17 05:54:34 +00004514 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004515 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00004516 return false;
4517 }
4518
4519 // Adjust the index value if the source operands will be swapped.
4520 if (ReverseVEXT)
4521 Imm -= NumElts;
4522
Bob Wilson32cd8552009-08-19 17:03:43 +00004523 return true;
4524}
4525
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004526/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4527/// instruction with the specified blocksize. (The order of the elements
4528/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00004529static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004530 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4531 "Only possible block sizes for VREV are: 16, 32, 64");
4532
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004533 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00004534 if (EltSz == 64)
4535 return false;
4536
4537 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004538 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00004539 // If the first shuffle index is UNDEF, be optimistic.
4540 if (M[0] < 0)
4541 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004542
4543 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4544 return false;
4545
4546 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004547 if (M[i] < 0) continue; // ignore UNDEF indices
4548 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004549 return false;
4550 }
4551
4552 return true;
4553}
4554
Benjamin Kramer339ced42012-01-15 13:16:05 +00004555static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00004556 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
4557 // range, then 0 is placed into the resulting vector. So pretty much any mask
4558 // of 8 elements can work here.
4559 return VT == MVT::v8i8 && M.size() == 8;
4560}
4561
Benjamin Kramer339ced42012-01-15 13:16:05 +00004562static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004563 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4564 if (EltSz == 64)
4565 return false;
4566
Bob Wilsona7062312009-08-21 20:54:19 +00004567 unsigned NumElts = VT.getVectorNumElements();
4568 WhichResult = (M[0] == 0 ? 0 : 1);
4569 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004570 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4571 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsona7062312009-08-21 20:54:19 +00004572 return false;
4573 }
4574 return true;
4575}
4576
Bob Wilson0bbd3072009-12-03 06:40:55 +00004577/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
4578/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4579/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004580static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004581 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4582 if (EltSz == 64)
4583 return false;
4584
4585 unsigned NumElts = VT.getVectorNumElements();
4586 WhichResult = (M[0] == 0 ? 0 : 1);
4587 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004588 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4589 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004590 return false;
4591 }
4592 return true;
4593}
4594
Benjamin Kramer339ced42012-01-15 13:16:05 +00004595static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004596 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4597 if (EltSz == 64)
4598 return false;
4599
Bob Wilsona7062312009-08-21 20:54:19 +00004600 unsigned NumElts = VT.getVectorNumElements();
4601 WhichResult = (M[0] == 0 ? 0 : 1);
4602 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004603 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsona7062312009-08-21 20:54:19 +00004604 if ((unsigned) M[i] != 2 * i + WhichResult)
4605 return false;
4606 }
4607
4608 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004609 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004610 return false;
4611
4612 return true;
4613}
4614
Bob Wilson0bbd3072009-12-03 06:40:55 +00004615/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4616/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4617/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00004618static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004619 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4620 if (EltSz == 64)
4621 return false;
4622
4623 unsigned Half = VT.getVectorNumElements() / 2;
4624 WhichResult = (M[0] == 0 ? 0 : 1);
4625 for (unsigned j = 0; j != 2; ++j) {
4626 unsigned Idx = WhichResult;
4627 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004628 int MIdx = M[i + j * Half];
4629 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson0bbd3072009-12-03 06:40:55 +00004630 return false;
4631 Idx += 2;
4632 }
4633 }
4634
4635 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4636 if (VT.is64BitVector() && EltSz == 32)
4637 return false;
4638
4639 return true;
4640}
4641
Benjamin Kramer339ced42012-01-15 13:16:05 +00004642static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004643 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4644 if (EltSz == 64)
4645 return false;
4646
Bob Wilsona7062312009-08-21 20:54:19 +00004647 unsigned NumElts = VT.getVectorNumElements();
4648 WhichResult = (M[0] == 0 ? 0 : 1);
4649 unsigned Idx = WhichResult * NumElts / 2;
4650 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004651 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4652 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsona7062312009-08-21 20:54:19 +00004653 return false;
4654 Idx += 1;
4655 }
4656
4657 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004658 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004659 return false;
4660
4661 return true;
4662}
4663
Bob Wilson0bbd3072009-12-03 06:40:55 +00004664/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4665/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4666/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004667static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004668 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4669 if (EltSz == 64)
4670 return false;
4671
4672 unsigned NumElts = VT.getVectorNumElements();
4673 WhichResult = (M[0] == 0 ? 0 : 1);
4674 unsigned Idx = WhichResult * NumElts / 2;
4675 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004676 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4677 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004678 return false;
4679 Idx += 1;
4680 }
4681
4682 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4683 if (VT.is64BitVector() && EltSz == 32)
4684 return false;
4685
4686 return true;
4687}
4688
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00004689/// \return true if this is a reverse operation on an vector.
4690static bool isReverseMask(ArrayRef<int> M, EVT VT) {
4691 unsigned NumElts = VT.getVectorNumElements();
4692 // Make sure the mask has the right size.
4693 if (NumElts != M.size())
4694 return false;
4695
4696 // Look for <15, ..., 3, -1, 1, 0>.
4697 for (unsigned i = 0; i != NumElts; ++i)
4698 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
4699 return false;
4700
4701 return true;
4702}
4703
Dale Johannesen2bff5052010-07-29 20:10:08 +00004704// If N is an integer constant that can be moved into a register in one
4705// instruction, return an SDValue of such a constant (will become a MOV
4706// instruction). Otherwise return null.
4707static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004708 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00004709 uint64_t Val;
4710 if (!isa<ConstantSDNode>(N))
4711 return SDValue();
4712 Val = cast<ConstantSDNode>(N)->getZExtValue();
4713
4714 if (ST->isThumb1Only()) {
4715 if (Val <= 255 || ~Val <= 255)
4716 return DAG.getConstant(Val, MVT::i32);
4717 } else {
4718 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4719 return DAG.getConstant(Val, MVT::i32);
4720 }
4721 return SDValue();
4722}
4723
Bob Wilson2e076c42009-06-22 23:27:02 +00004724// If this is a case we can't handle, return null and let the default
4725// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00004726SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4727 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00004728 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004729 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004730 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004731
4732 APInt SplatBits, SplatUndef;
4733 unsigned SplatBitSize;
4734 bool HasAnyUndefs;
4735 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00004736 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00004737 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00004738 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00004739 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00004740 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00004741 DAG, VmovVT, VT.is128BitVector(),
4742 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00004743 if (Val.getNode()) {
4744 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00004745 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00004746 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00004747
4748 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00004749 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004750 Val = isNEONModifiedImm(NegatedImm,
4751 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00004752 DAG, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00004753 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00004754 if (Val.getNode()) {
4755 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00004756 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00004757 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00004758
4759 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00004760 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00004761 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00004762 if (ImmVal != -1) {
4763 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4764 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4765 }
4766 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00004767 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00004768 }
4769
Bob Wilson91fdf682010-05-22 00:23:12 +00004770 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00004771 //
4772 // As an optimisation, even if more than one value is used it may be more
4773 // profitable to splat with one value then change some lanes.
4774 //
4775 // Heuristically we decide to do this if the vector has a "dominant" value,
4776 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00004777 unsigned NumElts = VT.getVectorNumElements();
4778 bool isOnlyLowElement = true;
4779 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00004780 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00004781 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00004782
4783 // Map of the number of times a particular SDValue appears in the
4784 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00004785 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00004786 SDValue Value;
4787 for (unsigned i = 0; i < NumElts; ++i) {
4788 SDValue V = Op.getOperand(i);
4789 if (V.getOpcode() == ISD::UNDEF)
4790 continue;
4791 if (i > 0)
4792 isOnlyLowElement = false;
4793 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4794 isConstant = false;
4795
James Molloy49bdbce2012-09-06 09:55:02 +00004796 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00004797 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00004798
James Molloy49bdbce2012-09-06 09:55:02 +00004799 // Is this value dominant? (takes up more than half of the lanes)
4800 if (++Count > (NumElts / 2)) {
4801 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00004802 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00004803 }
Bob Wilson91fdf682010-05-22 00:23:12 +00004804 }
James Molloy49bdbce2012-09-06 09:55:02 +00004805 if (ValueCounts.size() != 1)
4806 usesOnlyOneValue = false;
4807 if (!Value.getNode() && ValueCounts.size() > 0)
4808 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00004809
James Molloy49bdbce2012-09-06 09:55:02 +00004810 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00004811 return DAG.getUNDEF(VT);
4812
Quentin Colombet0f2fe742013-07-23 22:34:47 +00004813 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
4814 // Keep going if we are hitting this case.
4815 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00004816 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4817
Dale Johannesen2bff5052010-07-29 20:10:08 +00004818 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4819
Dale Johannesen710a2d92010-10-19 20:00:17 +00004820 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4821 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00004822 if (hasDominantValue && EltSize <= 32) {
4823 if (!isConstant) {
4824 SDValue N;
4825
4826 // If we are VDUPing a value that comes directly from a vector, that will
4827 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00004828 // just use VDUPLANE. We can only do this if the lane being extracted
4829 // is at a constant index, as the VDUP from lane instructions only have
4830 // constant-index forms.
4831 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4832 isa<ConstantSDNode>(Value->getOperand(1))) {
Silviu Barangab1409702012-10-15 09:41:32 +00004833 // We need to create a new undef vector to use for the VDUPLANE if the
4834 // size of the vector from which we get the value is different than the
4835 // size of the vector that we need to create. We will insert the element
4836 // such that the register coalescer will remove unnecessary copies.
4837 if (VT != Value->getOperand(0).getValueType()) {
4838 ConstantSDNode *constIndex;
4839 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
4840 assert(constIndex && "The index is not a constant!");
4841 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
4842 VT.getVectorNumElements();
4843 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
4844 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
4845 Value, DAG.getConstant(index, MVT::i32)),
4846 DAG.getConstant(index, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00004847 } else
Silviu Barangab1409702012-10-15 09:41:32 +00004848 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00004849 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00004850 } else
James Molloy49bdbce2012-09-06 09:55:02 +00004851 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4852
4853 if (!usesOnlyOneValue) {
4854 // The dominant value was splatted as 'N', but we now have to insert
4855 // all differing elements.
4856 for (unsigned I = 0; I < NumElts; ++I) {
4857 if (Op.getOperand(I) == Value)
4858 continue;
4859 SmallVector<SDValue, 3> Ops;
4860 Ops.push_back(N);
4861 Ops.push_back(Op.getOperand(I));
4862 Ops.push_back(DAG.getConstant(I, MVT::i32));
4863 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, &Ops[0], 3);
4864 }
4865 }
4866 return N;
4867 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00004868 if (VT.getVectorElementType().isFloatingPoint()) {
4869 SmallVector<SDValue, 8> Ops;
4870 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00004871 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00004872 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00004873 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4874 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesenff376752010-10-20 22:03:37 +00004875 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4876 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00004877 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00004878 }
James Molloy49bdbce2012-09-06 09:55:02 +00004879 if (usesOnlyOneValue) {
4880 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4881 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00004882 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00004883 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00004884 }
4885
4886 // If all elements are constants and the case above didn't get hit, fall back
4887 // to the default expansion, which will generate a load from the constant
4888 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00004889 if (isConstant)
4890 return SDValue();
4891
Bob Wilson6f2b8962011-01-07 21:37:30 +00004892 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4893 if (NumElts >= 4) {
4894 SDValue shuffle = ReconstructShuffle(Op, DAG);
4895 if (shuffle != SDValue())
4896 return shuffle;
4897 }
4898
Bob Wilson91fdf682010-05-22 00:23:12 +00004899 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00004900 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4901 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00004902 if (EltSize >= 32) {
4903 // Do the expansion with floating-point types, since that is what the VFP
4904 // registers are defined to use, and since i64 is not legal.
4905 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4906 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00004907 SmallVector<SDValue, 8> Ops;
4908 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00004909 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilsond8a9a042010-06-04 00:04:02 +00004910 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00004911 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00004912 }
4913
Jim Grosbach24e102a2013-07-08 18:18:52 +00004914 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
4915 // know the default expansion would otherwise fall back on something even
4916 // worse. For a vector with one or two non-undef values, that's
4917 // scalar_to_vector for the elements followed by a shuffle (provided the
4918 // shuffle is valid for the target) and materialization element by element
4919 // on the stack followed by a load for everything else.
4920 if (!isConstant && !usesOnlyOneValue) {
4921 SDValue Vec = DAG.getUNDEF(VT);
4922 for (unsigned i = 0 ; i < NumElts; ++i) {
4923 SDValue V = Op.getOperand(i);
4924 if (V.getOpcode() == ISD::UNDEF)
4925 continue;
4926 SDValue LaneIdx = DAG.getConstant(i, MVT::i32);
4927 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
4928 }
4929 return Vec;
4930 }
4931
Bob Wilson2e076c42009-06-22 23:27:02 +00004932 return SDValue();
4933}
4934
Bob Wilson6f2b8962011-01-07 21:37:30 +00004935// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00004936// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00004937SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4938 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004939 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00004940 EVT VT = Op.getValueType();
4941 unsigned NumElts = VT.getVectorNumElements();
4942
4943 SmallVector<SDValue, 2> SourceVecs;
4944 SmallVector<unsigned, 2> MinElts;
4945 SmallVector<unsigned, 2> MaxElts;
Andrew Trick5eb0a302011-01-19 02:26:13 +00004946
Bob Wilson6f2b8962011-01-07 21:37:30 +00004947 for (unsigned i = 0; i < NumElts; ++i) {
4948 SDValue V = Op.getOperand(i);
4949 if (V.getOpcode() == ISD::UNDEF)
4950 continue;
4951 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4952 // A shuffle can only come from building a vector from various
4953 // elements of other vectors.
4954 return SDValue();
Eli Friedman74d1da52011-10-14 23:58:49 +00004955 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4956 VT.getVectorElementType()) {
4957 // This code doesn't know how to handle shuffles where the vector
4958 // element types do not match (this happens because type legalization
4959 // promotes the return type of EXTRACT_VECTOR_ELT).
4960 // FIXME: It might be appropriate to extend this code to handle
4961 // mismatched types.
4962 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00004963 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00004964
Bob Wilson6f2b8962011-01-07 21:37:30 +00004965 // Record this extraction against the appropriate vector if possible...
4966 SDValue SourceVec = V.getOperand(0);
Jim Grosbach6df755c2012-07-25 17:02:47 +00004967 // If the element number isn't a constant, we can't effectively
4968 // analyze what's going on.
4969 if (!isa<ConstantSDNode>(V.getOperand(1)))
4970 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00004971 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4972 bool FoundSource = false;
4973 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4974 if (SourceVecs[j] == SourceVec) {
4975 if (MinElts[j] > EltNo)
4976 MinElts[j] = EltNo;
4977 if (MaxElts[j] < EltNo)
4978 MaxElts[j] = EltNo;
4979 FoundSource = true;
4980 break;
4981 }
4982 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00004983
Bob Wilson6f2b8962011-01-07 21:37:30 +00004984 // Or record a new source if not...
4985 if (!FoundSource) {
4986 SourceVecs.push_back(SourceVec);
4987 MinElts.push_back(EltNo);
4988 MaxElts.push_back(EltNo);
4989 }
4990 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00004991
Bob Wilson6f2b8962011-01-07 21:37:30 +00004992 // Currently only do something sane when at most two source vectors
4993 // involved.
4994 if (SourceVecs.size() > 2)
4995 return SDValue();
4996
4997 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4998 int VEXTOffsets[2] = {0, 0};
Andrew Trick5eb0a302011-01-19 02:26:13 +00004999
Bob Wilson6f2b8962011-01-07 21:37:30 +00005000 // This loop extracts the usage patterns of the source vectors
5001 // and prepares appropriate SDValues for a shuffle if possible.
5002 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
5003 if (SourceVecs[i].getValueType() == VT) {
5004 // No VEXT necessary
5005 ShuffleSrcs[i] = SourceVecs[i];
5006 VEXTOffsets[i] = 0;
5007 continue;
5008 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
5009 // It probably isn't worth padding out a smaller vector just to
5010 // break it down again in a shuffle.
5011 return SDValue();
5012 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005013
Bob Wilson6f2b8962011-01-07 21:37:30 +00005014 // Since only 64-bit and 128-bit vectors are legal on ARM and
5015 // we've eliminated the other cases...
Bob Wilson3fa9c062011-01-07 23:40:46 +00005016 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
5017 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick5eb0a302011-01-19 02:26:13 +00005018
Bob Wilson6f2b8962011-01-07 21:37:30 +00005019 if (MaxElts[i] - MinElts[i] >= NumElts) {
5020 // Span too large for a VEXT to cope
5021 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005022 }
5023
Bob Wilson6f2b8962011-01-07 21:37:30 +00005024 if (MinElts[i] >= NumElts) {
5025 // The extraction can just take the second half
5026 VEXTOffsets[i] = NumElts;
Eric Christopher2af95512011-01-14 23:50:53 +00005027 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5028 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005029 DAG.getIntPtrConstant(NumElts));
5030 } else if (MaxElts[i] < NumElts) {
5031 // The extraction can just take the first half
5032 VEXTOffsets[i] = 0;
Eric Christopher2af95512011-01-14 23:50:53 +00005033 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5034 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005035 DAG.getIntPtrConstant(0));
5036 } else {
5037 // An actual VEXT is needed
5038 VEXTOffsets[i] = MinElts[i];
Eric Christopher2af95512011-01-14 23:50:53 +00005039 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5040 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005041 DAG.getIntPtrConstant(0));
Eric Christopher2af95512011-01-14 23:50:53 +00005042 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5043 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005044 DAG.getIntPtrConstant(NumElts));
5045 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
5046 DAG.getConstant(VEXTOffsets[i], MVT::i32));
5047 }
5048 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005049
Bob Wilson6f2b8962011-01-07 21:37:30 +00005050 SmallVector<int, 8> Mask;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005051
Bob Wilson6f2b8962011-01-07 21:37:30 +00005052 for (unsigned i = 0; i < NumElts; ++i) {
5053 SDValue Entry = Op.getOperand(i);
5054 if (Entry.getOpcode() == ISD::UNDEF) {
5055 Mask.push_back(-1);
5056 continue;
5057 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005058
Bob Wilson6f2b8962011-01-07 21:37:30 +00005059 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher2af95512011-01-14 23:50:53 +00005060 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
5061 .getOperand(1))->getSExtValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005062 if (ExtractVec == SourceVecs[0]) {
5063 Mask.push_back(ExtractElt - VEXTOffsets[0]);
5064 } else {
5065 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
5066 }
5067 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005068
Bob Wilson6f2b8962011-01-07 21:37:30 +00005069 // Final check before we try to produce nonsense...
5070 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher2af95512011-01-14 23:50:53 +00005071 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
5072 &Mask[0]);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005073
Bob Wilson6f2b8962011-01-07 21:37:30 +00005074 return SDValue();
5075}
5076
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005077/// isShuffleMaskLegal - Targets can use this to indicate that they only
5078/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5079/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5080/// are assumed to be legal.
5081bool
5082ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5083 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005084 if (VT.getVectorNumElements() == 4 &&
5085 (VT.is128BitVector() || VT.is64BitVector())) {
5086 unsigned PFIndexes[4];
5087 for (unsigned i = 0; i != 4; ++i) {
5088 if (M[i] < 0)
5089 PFIndexes[i] = 8;
5090 else
5091 PFIndexes[i] = M[i];
5092 }
5093
5094 // Compute the index in the perfect shuffle table.
5095 unsigned PFTableIndex =
5096 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5097 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5098 unsigned Cost = (PFEntry >> 30);
5099
5100 if (Cost <= 4)
5101 return true;
5102 }
5103
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005104 bool ReverseVEXT;
Bob Wilsona7062312009-08-21 20:54:19 +00005105 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005106
Bob Wilson846bd792010-06-07 23:53:38 +00005107 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5108 return (EltSize >= 32 ||
5109 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005110 isVREVMask(M, VT, 64) ||
5111 isVREVMask(M, VT, 32) ||
5112 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005113 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005114 isVTBLMask(M, VT) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005115 isVTRNMask(M, VT, WhichResult) ||
5116 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson0bbd3072009-12-03 06:40:55 +00005117 isVZIPMask(M, VT, WhichResult) ||
5118 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
5119 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005120 isVZIP_v_undef_Mask(M, VT, WhichResult) ||
5121 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005122}
5123
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005124/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5125/// the specified operations to build the shuffle.
5126static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5127 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005128 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005129 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5130 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5131 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5132
5133 enum {
5134 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5135 OP_VREV,
5136 OP_VDUP0,
5137 OP_VDUP1,
5138 OP_VDUP2,
5139 OP_VDUP3,
5140 OP_VEXT1,
5141 OP_VEXT2,
5142 OP_VEXT3,
5143 OP_VUZPL, // VUZP, left result
5144 OP_VUZPR, // VUZP, right result
5145 OP_VZIPL, // VZIP, left result
5146 OP_VZIPR, // VZIP, right result
5147 OP_VTRNL, // VTRN, left result
5148 OP_VTRNR // VTRN, right result
5149 };
5150
5151 if (OpNum == OP_COPY) {
5152 if (LHSID == (1*9+2)*9+3) return LHS;
5153 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5154 return RHS;
5155 }
5156
5157 SDValue OpLHS, OpRHS;
5158 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5159 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5160 EVT VT = OpLHS.getValueType();
5161
5162 switch (OpNum) {
5163 default: llvm_unreachable("Unknown shuffle opcode!");
5164 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00005165 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00005166 if (VT.getVectorElementType() == MVT::i32 ||
5167 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00005168 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
5169 // vrev <4 x i16> -> VREV32
5170 if (VT.getVectorElementType() == MVT::i16)
5171 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
5172 // vrev <4 x i8> -> VREV16
5173 assert(VT.getVectorElementType() == MVT::i8);
5174 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005175 case OP_VDUP0:
5176 case OP_VDUP1:
5177 case OP_VDUP2:
5178 case OP_VDUP3:
5179 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005180 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005181 case OP_VEXT1:
5182 case OP_VEXT2:
5183 case OP_VEXT3:
5184 return DAG.getNode(ARMISD::VEXT, dl, VT,
5185 OpLHS, OpRHS,
5186 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
5187 case OP_VUZPL:
5188 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005189 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005190 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
5191 case OP_VZIPL:
5192 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005193 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005194 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
5195 case OP_VTRNL:
5196 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005197 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5198 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005199 }
5200}
5201
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005202static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005203 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005204 SelectionDAG &DAG) {
5205 // Check to see if we can use the VTBL instruction.
5206 SDValue V1 = Op.getOperand(0);
5207 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005208 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005209
5210 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00005211 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005212 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
5213 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
5214
5215 if (V2.getNode()->getOpcode() == ISD::UNDEF)
5216 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
5217 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
5218 &VTBLMask[0], 8));
Bill Wendlingebecb332011-03-15 20:47:26 +00005219
Owen Anderson77aa2662011-04-05 21:48:57 +00005220 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlingebecb332011-03-15 20:47:26 +00005221 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
5222 &VTBLMask[0], 8));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005223}
5224
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005225static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
5226 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005227 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005228 SDValue OpLHS = Op.getOperand(0);
5229 EVT VT = OpLHS.getValueType();
5230
5231 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
5232 "Expect an v8i16/v16i8 type");
5233 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
5234 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
5235 // extract the first 8 bytes into the top double word and the last 8 bytes
5236 // into the bottom double word. The v8i16 case is similar.
5237 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
5238 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
5239 DAG.getConstant(ExtractNum, MVT::i32));
5240}
5241
Bob Wilson2e076c42009-06-22 23:27:02 +00005242static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005243 SDValue V1 = Op.getOperand(0);
5244 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005245 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00005246 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005247 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00005248
Bob Wilsonc6800b52009-08-13 02:13:04 +00005249 // Convert shuffles that are directly supported on NEON to target-specific
5250 // DAG nodes, instead of keeping them as shuffles and matching them again
5251 // during code selection. This is more efficient and avoids the possibility
5252 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00005253 // FIXME: floating-point vectors should be canonicalized to integer vectors
5254 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005255 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005256
Bob Wilson846bd792010-06-07 23:53:38 +00005257 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5258 if (EltSize <= 32) {
5259 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
5260 int Lane = SVN->getSplatIndex();
5261 // If this is undef splat, generate it via "just" vdup, if possible.
5262 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00005263
Dan Gohman198b7ff2011-11-03 21:49:52 +00005264 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00005265 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5266 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5267 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005268 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
5269 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
5270 // reaches it).
5271 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
5272 !isa<ConstantSDNode>(V1.getOperand(0))) {
5273 bool IsScalarToVector = true;
5274 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
5275 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
5276 IsScalarToVector = false;
5277 break;
5278 }
5279 if (IsScalarToVector)
5280 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5281 }
Bob Wilson846bd792010-06-07 23:53:38 +00005282 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
5283 DAG.getConstant(Lane, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00005284 }
Bob Wilson846bd792010-06-07 23:53:38 +00005285
5286 bool ReverseVEXT;
5287 unsigned Imm;
5288 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
5289 if (ReverseVEXT)
5290 std::swap(V1, V2);
5291 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
5292 DAG.getConstant(Imm, MVT::i32));
5293 }
5294
5295 if (isVREVMask(ShuffleMask, VT, 64))
5296 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
5297 if (isVREVMask(ShuffleMask, VT, 32))
5298 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
5299 if (isVREVMask(ShuffleMask, VT, 16))
5300 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
5301
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005302 if (V2->getOpcode() == ISD::UNDEF &&
5303 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
5304 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
5305 DAG.getConstant(Imm, MVT::i32));
5306 }
5307
Bob Wilson846bd792010-06-07 23:53:38 +00005308 // Check for Neon shuffles that modify both input vectors in place.
5309 // If both results are used, i.e., if there are two shuffles with the same
5310 // source operands and with masks corresponding to both results of one of
5311 // these operations, DAG memoization will ensure that a single node is
5312 // used for both shuffles.
5313 unsigned WhichResult;
5314 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5315 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5316 V1, V2).getValue(WhichResult);
5317 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5318 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5319 V1, V2).getValue(WhichResult);
5320 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5321 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5322 V1, V2).getValue(WhichResult);
5323
5324 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5325 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5326 V1, V1).getValue(WhichResult);
5327 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5328 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5329 V1, V1).getValue(WhichResult);
5330 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5331 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5332 V1, V1).getValue(WhichResult);
Bob Wilsoncce31f62009-08-14 05:08:32 +00005333 }
Bob Wilson32cd8552009-08-19 17:03:43 +00005334
Bob Wilsona7062312009-08-21 20:54:19 +00005335 // If the shuffle is not directly supported and it has 4 elements, use
5336 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00005337 unsigned NumElts = VT.getVectorNumElements();
5338 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005339 unsigned PFIndexes[4];
5340 for (unsigned i = 0; i != 4; ++i) {
5341 if (ShuffleMask[i] < 0)
5342 PFIndexes[i] = 8;
5343 else
5344 PFIndexes[i] = ShuffleMask[i];
5345 }
5346
5347 // Compute the index in the perfect shuffle table.
5348 unsigned PFTableIndex =
5349 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005350 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5351 unsigned Cost = (PFEntry >> 30);
5352
5353 if (Cost <= 4)
5354 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
5355 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00005356
Bob Wilsond8a9a042010-06-04 00:04:02 +00005357 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00005358 if (EltSize >= 32) {
5359 // Do the expansion with floating-point types, since that is what the VFP
5360 // registers are defined to use, and since i64 is not legal.
5361 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5362 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00005363 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
5364 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005365 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00005366 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00005367 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00005368 Ops.push_back(DAG.getUNDEF(EltVT));
5369 else
5370 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
5371 ShuffleMask[i] < (int)NumElts ? V1 : V2,
5372 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
5373 MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00005374 }
Bob Wilsond8a9a042010-06-04 00:04:02 +00005375 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00005376 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00005377 }
5378
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005379 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
5380 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
5381
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005382 if (VT == MVT::v8i8) {
5383 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
5384 if (NewOp.getNode())
5385 return NewOp;
5386 }
5387
Bob Wilson6f34e272009-08-14 05:16:33 +00005388 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005389}
5390
Eli Friedmana5e244c2011-10-24 23:08:52 +00005391static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
5392 // INSERT_VECTOR_ELT is legal only for immediate indexes.
5393 SDValue Lane = Op.getOperand(2);
5394 if (!isa<ConstantSDNode>(Lane))
5395 return SDValue();
5396
5397 return Op;
5398}
5399
Bob Wilson2e076c42009-06-22 23:27:02 +00005400static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00005401 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00005402 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00005403 if (!isa<ConstantSDNode>(Lane))
5404 return SDValue();
5405
5406 SDValue Vec = Op.getOperand(0);
5407 if (Op.getValueType() == MVT::i32 &&
5408 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005409 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00005410 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
5411 }
5412
5413 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00005414}
5415
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005416static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5417 // The only time a CONCAT_VECTORS operation can have legal types is when
5418 // two 64-bit vectors are concatenated to a 128-bit vector.
5419 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
5420 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005421 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005422 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005423 SDValue Op0 = Op.getOperand(0);
5424 SDValue Op1 = Op.getOperand(1);
5425 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005426 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005427 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005428 DAG.getIntPtrConstant(0));
5429 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005430 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005431 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005432 DAG.getIntPtrConstant(1));
Wesley Peck527da1b2010-11-23 03:31:01 +00005433 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005434}
5435
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005436/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
5437/// element has been zero/sign-extended, depending on the isSigned parameter,
5438/// from an integer type half its size.
5439static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
5440 bool isSigned) {
5441 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
5442 EVT VT = N->getValueType(0);
5443 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
5444 SDNode *BVN = N->getOperand(0).getNode();
5445 if (BVN->getValueType(0) != MVT::v4i32 ||
5446 BVN->getOpcode() != ISD::BUILD_VECTOR)
5447 return false;
5448 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5449 unsigned HiElt = 1 - LoElt;
5450 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
5451 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
5452 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
5453 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
5454 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
5455 return false;
5456 if (isSigned) {
5457 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
5458 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
5459 return true;
5460 } else {
5461 if (Hi0->isNullValue() && Hi1->isNullValue())
5462 return true;
5463 }
5464 return false;
5465 }
5466
5467 if (N->getOpcode() != ISD::BUILD_VECTOR)
5468 return false;
5469
5470 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5471 SDNode *Elt = N->getOperand(i).getNode();
5472 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
5473 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5474 unsigned HalfSize = EltSize / 2;
5475 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005476 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005477 return false;
5478 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005479 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005480 return false;
5481 }
5482 continue;
5483 }
5484 return false;
5485 }
5486
5487 return true;
5488}
5489
5490/// isSignExtended - Check if a node is a vector value that is sign-extended
5491/// or a constant BUILD_VECTOR with sign-extended elements.
5492static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
5493 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
5494 return true;
5495 if (isExtendedBUILD_VECTOR(N, DAG, true))
5496 return true;
5497 return false;
5498}
5499
5500/// isZeroExtended - Check if a node is a vector value that is zero-extended
5501/// or a constant BUILD_VECTOR with zero-extended elements.
5502static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
5503 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
5504 return true;
5505 if (isExtendedBUILD_VECTOR(N, DAG, false))
5506 return true;
5507 return false;
5508}
5509
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005510static EVT getExtensionTo64Bits(const EVT &OrigVT) {
5511 if (OrigVT.getSizeInBits() >= 64)
5512 return OrigVT;
5513
5514 assert(OrigVT.isSimple() && "Expecting a simple value type");
5515
5516 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
5517 switch (OrigSimpleTy) {
5518 default: llvm_unreachable("Unexpected Vector Type");
5519 case MVT::v2i8:
5520 case MVT::v2i16:
5521 return MVT::v2i32;
5522 case MVT::v4i8:
5523 return MVT::v4i16;
5524 }
5525}
5526
Sebastian Popa204f722012-11-30 19:08:04 +00005527/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
5528/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
5529/// We insert the required extension here to get the vector to fill a D register.
5530static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
5531 const EVT &OrigTy,
5532 const EVT &ExtTy,
5533 unsigned ExtOpcode) {
5534 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
5535 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
5536 // 64-bits we need to insert a new extension so that it will be 64-bits.
5537 assert(ExtTy.is128BitVector() && "Unexpected extension size");
5538 if (OrigTy.getSizeInBits() >= 64)
5539 return N;
5540
5541 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005542 EVT NewVT = getExtensionTo64Bits(OrigTy);
5543
Andrew Trickef9de2a2013-05-25 02:42:55 +00005544 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00005545}
5546
5547/// SkipLoadExtensionForVMULL - return a load of the original vector size that
5548/// does not do any sign/zero extension. If the original vector is less
5549/// than 64 bits, an appropriate extension will be added after the load to
5550/// reach a total size of 64 bits. We have to add the extension separately
5551/// because ARM does not have a sign/zero extending load for vectors.
5552static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005553 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
5554
5555 // The load already has the right type.
5556 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00005557 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00005558 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
5559 LD->isNonTemporal(), LD->isInvariant(),
5560 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005561
5562 // We need to create a zextload/sextload. We cannot just create a load
5563 // followed by a zext/zext node because LowerMUL is also run during normal
5564 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005565 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005566 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
5567 LD->getMemoryVT(), LD->isVolatile(),
5568 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00005569}
5570
5571/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
5572/// extending load, or BUILD_VECTOR with extended elements, return the
5573/// unextended value. The unextended vector should be 64 bits so that it can
5574/// be used as an operand to a VMULL instruction. If the original vector size
5575/// before extension is less than 64 bits we add a an extension to resize
5576/// the vector to 64 bits.
5577static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00005578 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00005579 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
5580 N->getOperand(0)->getValueType(0),
5581 N->getValueType(0),
5582 N->getOpcode());
5583
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005584 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00005585 return SkipLoadExtensionForVMULL(LD, DAG);
5586
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005587 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
5588 // have been legalized as a BITCAST from v4i32.
5589 if (N->getOpcode() == ISD::BITCAST) {
5590 SDNode *BVN = N->getOperand(0).getNode();
5591 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
5592 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
5593 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00005594 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005595 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
5596 }
5597 // Construct a new BUILD_VECTOR with elements truncated to half the size.
5598 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
5599 EVT VT = N->getValueType(0);
5600 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
5601 unsigned NumElts = VT.getVectorNumElements();
5602 MVT TruncVT = MVT::getIntegerVT(EltSize);
5603 SmallVector<SDValue, 8> Ops;
5604 for (unsigned i = 0; i != NumElts; ++i) {
5605 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
5606 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00005607 // Element types smaller than 32 bits are not legal, so use i32 elements.
5608 // The values are implicitly truncated so sext vs. zext doesn't matter.
5609 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005610 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005611 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005612 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilson38ab35a2010-09-01 23:50:19 +00005613}
5614
Evan Chenge2086e72011-03-29 01:56:09 +00005615static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
5616 unsigned Opcode = N->getOpcode();
5617 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5618 SDNode *N0 = N->getOperand(0).getNode();
5619 SDNode *N1 = N->getOperand(1).getNode();
5620 return N0->hasOneUse() && N1->hasOneUse() &&
5621 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
5622 }
5623 return false;
5624}
5625
5626static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
5627 unsigned Opcode = N->getOpcode();
5628 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5629 SDNode *N0 = N->getOperand(0).getNode();
5630 SDNode *N1 = N->getOperand(1).getNode();
5631 return N0->hasOneUse() && N1->hasOneUse() &&
5632 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
5633 }
5634 return false;
5635}
5636
Bob Wilson38ab35a2010-09-01 23:50:19 +00005637static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
5638 // Multiplications are only custom-lowered for 128-bit vectors so that
5639 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
5640 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00005641 assert(VT.is128BitVector() && VT.isInteger() &&
5642 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00005643 SDNode *N0 = Op.getOperand(0).getNode();
5644 SDNode *N1 = Op.getOperand(1).getNode();
5645 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00005646 bool isMLA = false;
5647 bool isN0SExt = isSignExtended(N0, DAG);
5648 bool isN1SExt = isSignExtended(N1, DAG);
5649 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00005650 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00005651 else {
5652 bool isN0ZExt = isZeroExtended(N0, DAG);
5653 bool isN1ZExt = isZeroExtended(N1, DAG);
5654 if (isN0ZExt && isN1ZExt)
5655 NewOpc = ARMISD::VMULLu;
5656 else if (isN1SExt || isN1ZExt) {
5657 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
5658 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
5659 if (isN1SExt && isAddSubSExt(N0, DAG)) {
5660 NewOpc = ARMISD::VMULLs;
5661 isMLA = true;
5662 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
5663 NewOpc = ARMISD::VMULLu;
5664 isMLA = true;
5665 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
5666 std::swap(N0, N1);
5667 NewOpc = ARMISD::VMULLu;
5668 isMLA = true;
5669 }
5670 }
5671
5672 if (!NewOpc) {
5673 if (VT == MVT::v2i64)
5674 // Fall through to expand this. It is not legal.
5675 return SDValue();
5676 else
5677 // Other vector multiplications are legal.
5678 return Op;
5679 }
5680 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00005681
5682 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005683 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00005684 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00005685 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005686 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00005687 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005688 assert(Op0.getValueType().is64BitVector() &&
5689 Op1.getValueType().is64BitVector() &&
5690 "unexpected types for extended operands to VMULL");
5691 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
5692 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00005693
Evan Chenge2086e72011-03-29 01:56:09 +00005694 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
5695 // isel lowering to take advantage of no-stall back to back vmul + vmla.
5696 // vmull q0, d4, d6
5697 // vmlal q0, d5, d6
5698 // is faster than
5699 // vaddl q0, d4, d5
5700 // vmovl q1, d6
5701 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00005702 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
5703 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005704 EVT Op1VT = Op1.getValueType();
5705 return DAG.getNode(N0->getOpcode(), DL, VT,
5706 DAG.getNode(NewOpc, DL, VT,
5707 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
5708 DAG.getNode(NewOpc, DL, VT,
5709 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00005710}
5711
Owen Anderson77aa2662011-04-05 21:48:57 +00005712static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005713LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00005714 // Convert to float
5715 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
5716 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
5717 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
5718 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
5719 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
5720 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
5721 // Get reciprocal estimate.
5722 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00005723 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005724 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
5725 // Because char has a smaller range than uchar, we can actually get away
5726 // without any newton steps. This requires that we use a weird bias
5727 // of 0xb000, however (again, this has been exhaustively tested).
5728 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
5729 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
5730 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
5731 Y = DAG.getConstant(0xb000, MVT::i32);
5732 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
5733 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
5734 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
5735 // Convert back to short.
5736 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
5737 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
5738 return X;
5739}
5740
Owen Anderson77aa2662011-04-05 21:48:57 +00005741static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005742LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00005743 SDValue N2;
5744 // Convert to float.
5745 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
5746 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
5747 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
5748 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
5749 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
5750 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005751
Nate Begemanfa62d502011-02-11 20:53:29 +00005752 // Use reciprocal estimate and one refinement step.
5753 // float4 recip = vrecpeq_f32(yf);
5754 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00005755 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005756 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005757 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005758 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
5759 N1, N2);
5760 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5761 // Because short has a smaller range than ushort, we can actually get away
5762 // with only a single newton step. This requires that we use a weird bias
5763 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005764 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00005765 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5766 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005767 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00005768 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5769 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5770 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5771 // Convert back to integer and return.
5772 // return vmovn_s32(vcvt_s32_f32(result));
5773 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5774 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5775 return N0;
5776}
5777
5778static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
5779 EVT VT = Op.getValueType();
5780 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5781 "unexpected type for custom-lowering ISD::SDIV");
5782
Andrew Trickef9de2a2013-05-25 02:42:55 +00005783 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00005784 SDValue N0 = Op.getOperand(0);
5785 SDValue N1 = Op.getOperand(1);
5786 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00005787
Nate Begemanfa62d502011-02-11 20:53:29 +00005788 if (VT == MVT::v8i8) {
5789 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
5790 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005791
Nate Begemanfa62d502011-02-11 20:53:29 +00005792 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5793 DAG.getIntPtrConstant(4));
5794 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00005795 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00005796 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5797 DAG.getIntPtrConstant(0));
5798 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5799 DAG.getIntPtrConstant(0));
5800
5801 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5802 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5803
5804 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5805 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00005806
Nate Begemanfa62d502011-02-11 20:53:29 +00005807 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5808 return N0;
5809 }
5810 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5811}
5812
5813static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5814 EVT VT = Op.getValueType();
5815 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5816 "unexpected type for custom-lowering ISD::UDIV");
5817
Andrew Trickef9de2a2013-05-25 02:42:55 +00005818 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00005819 SDValue N0 = Op.getOperand(0);
5820 SDValue N1 = Op.getOperand(1);
5821 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00005822
Nate Begemanfa62d502011-02-11 20:53:29 +00005823 if (VT == MVT::v8i8) {
5824 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5825 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005826
Nate Begemanfa62d502011-02-11 20:53:29 +00005827 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5828 DAG.getIntPtrConstant(4));
5829 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00005830 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00005831 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5832 DAG.getIntPtrConstant(0));
5833 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5834 DAG.getIntPtrConstant(0));
Owen Anderson77aa2662011-04-05 21:48:57 +00005835
Nate Begemanfa62d502011-02-11 20:53:29 +00005836 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5837 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00005838
Nate Begemanfa62d502011-02-11 20:53:29 +00005839 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5840 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00005841
5842 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begemanfa62d502011-02-11 20:53:29 +00005843 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5844 N0);
5845 return N0;
5846 }
Owen Anderson77aa2662011-04-05 21:48:57 +00005847
Nate Begemanfa62d502011-02-11 20:53:29 +00005848 // v4i16 sdiv ... Convert to float.
5849 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5850 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5851 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5852 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5853 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005854 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00005855
5856 // Use reciprocal estimate and two refinement steps.
5857 // float4 recip = vrecpeq_f32(yf);
5858 // recip *= vrecpsq_f32(yf, recip);
5859 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00005860 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005861 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005862 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005863 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005864 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00005865 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00005866 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005867 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005868 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00005869 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5870 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5871 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5872 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005873 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00005874 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5875 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5876 N1 = DAG.getConstant(2, MVT::i32);
5877 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5878 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5879 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5880 // Convert back to integer and return.
5881 // return vmovn_u32(vcvt_s32_f32(result));
5882 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5883 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5884 return N0;
5885}
5886
Evan Chenge8916542011-08-30 01:34:54 +00005887static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5888 EVT VT = Op.getNode()->getValueType(0);
5889 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5890
5891 unsigned Opc;
5892 bool ExtraOp = false;
5893 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00005894 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00005895 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5896 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5897 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5898 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5899 }
5900
5901 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005902 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00005903 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00005904 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00005905 Op.getOperand(1), Op.getOperand(2));
5906}
5907
Bob Wilsone7dde0c2013-11-03 06:14:38 +00005908SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
5909 assert(Subtarget->isTargetDarwin());
5910
5911 // For iOS, we want to call an alternative entry point: __sincos_stret,
5912 // return values are passed via sret.
5913 SDLoc dl(Op);
5914 SDValue Arg = Op.getOperand(0);
5915 EVT ArgVT = Arg.getValueType();
5916 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
5917
5918 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
5919 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5920
5921 // Pair of floats / doubles used to pass the result.
5922 StructType *RetTy = StructType::get(ArgTy, ArgTy, NULL);
5923
5924 // Create stack object for sret.
5925 const uint64_t ByteSize = TLI.getDataLayout()->getTypeAllocSize(RetTy);
5926 const unsigned StackAlign = TLI.getDataLayout()->getPrefTypeAlignment(RetTy);
5927 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
5928 SDValue SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
5929
5930 ArgListTy Args;
5931 ArgListEntry Entry;
5932
5933 Entry.Node = SRet;
5934 Entry.Ty = RetTy->getPointerTo();
5935 Entry.isSExt = false;
5936 Entry.isZExt = false;
5937 Entry.isSRet = true;
5938 Args.push_back(Entry);
5939
5940 Entry.Node = Arg;
5941 Entry.Ty = ArgTy;
5942 Entry.isSExt = false;
5943 Entry.isZExt = false;
5944 Args.push_back(Entry);
5945
5946 const char *LibcallName = (ArgVT == MVT::f64)
5947 ? "__sincos_stret" : "__sincosf_stret";
5948 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
5949
5950 TargetLowering::
5951 CallLoweringInfo CLI(DAG.getEntryNode(), Type::getVoidTy(*DAG.getContext()),
5952 false, false, false, false, 0,
5953 CallingConv::C, /*isTaillCall=*/false,
5954 /*doesNotRet=*/false, /*isReturnValueUsed*/false,
5955 Callee, Args, DAG, dl);
5956 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
5957
5958 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
5959 MachinePointerInfo(), false, false, false, 0);
5960
5961 // Address of cos field.
5962 SDValue Add = DAG.getNode(ISD::ADD, dl, getPointerTy(), SRet,
5963 DAG.getIntPtrConstant(ArgVT.getStoreSize()));
5964 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
5965 MachinePointerInfo(), false, false, false, 0);
5966
5967 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
5968 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
5969 LoadSin.getValue(0), LoadCos.getValue(0));
5970}
5971
Eli Friedman10f9ce22011-09-15 22:26:18 +00005972static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00005973 // Monotonic load/store is legal for all targets
5974 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5975 return Op;
5976
Alp Tokercb402912014-01-24 17:20:08 +00005977 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00005978 // dmb or equivalent available.
5979 return SDValue();
5980}
5981
Tim Northoverbc933082013-05-23 19:11:20 +00005982static void ReplaceREADCYCLECOUNTER(SDNode *N,
5983 SmallVectorImpl<SDValue> &Results,
5984 SelectionDAG &DAG,
5985 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005986 SDLoc DL(N);
Tim Northoverbc933082013-05-23 19:11:20 +00005987 SDValue Cycles32, OutChain;
5988
5989 if (Subtarget->hasPerfMon()) {
5990 // Under Power Management extensions, the cycle-count is:
5991 // mrc p15, #0, <Rt>, c9, c13, #0
5992 SDValue Ops[] = { N->getOperand(0), // Chain
5993 DAG.getConstant(Intrinsic::arm_mrc, MVT::i32),
5994 DAG.getConstant(15, MVT::i32),
5995 DAG.getConstant(0, MVT::i32),
5996 DAG.getConstant(9, MVT::i32),
5997 DAG.getConstant(13, MVT::i32),
5998 DAG.getConstant(0, MVT::i32)
5999 };
6000
6001 Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
6002 DAG.getVTList(MVT::i32, MVT::Other), &Ops[0],
6003 array_lengthof(Ops));
6004 OutChain = Cycles32.getValue(1);
6005 } else {
6006 // Intrinsic is defined to return 0 on unsupported platforms. Technically
6007 // there are older ARM CPUs that have implementation-specific ways of
6008 // obtaining this information (FIXME!).
6009 Cycles32 = DAG.getConstant(0, MVT::i32);
6010 OutChain = DAG.getEntryNode();
6011 }
6012
6013
6014 SDValue Cycles64 = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64,
6015 Cycles32, DAG.getConstant(0, MVT::i32));
6016 Results.push_back(Cycles64);
6017 Results.push_back(OutChain);
6018}
6019
Dan Gohman21cea8a2010-04-17 15:26:15 +00006020SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006021 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006022 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Cheng10043e22007-01-19 07:51:42 +00006023 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006024 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006025 case ISD::GlobalAddress:
Tim Northoverd6a729b2014-01-06 14:28:05 +00006026 return Subtarget->isTargetMachO() ? LowerGlobalAddressDarwin(Op, DAG) :
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006027 LowerGlobalAddressELF(Op, DAG);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006028 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006029 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006030 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6031 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006032 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006033 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006034 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006035 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006036 case ISD::SINT_TO_FP:
6037 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6038 case ISD::FP_TO_SINT:
6039 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006040 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006041 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006042 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006043 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006044 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006045 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006046 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6047 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006048 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006049 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006050 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006051 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng15b80e42009-11-12 07:13:11 +00006052 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00006053 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00006054 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach8546ec92010-01-18 19:58:49 +00006055 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00006056 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00006057 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00006058 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006059 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00006060 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00006061 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006062 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006063 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00006064 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006065 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begemanfa62d502011-02-11 20:53:29 +00006066 case ISD::SDIV: return LowerSDIV(Op, DAG);
6067 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00006068 case ISD::ADDC:
6069 case ISD::ADDE:
6070 case ISD::SUBC:
6071 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00006072 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00006073 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006074 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00006075 case ISD::SDIVREM:
6076 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006077 }
Evan Cheng10043e22007-01-19 07:51:42 +00006078}
6079
Duncan Sands6ed40142008-12-01 11:39:25 +00006080/// ReplaceNodeResults - Replace the results of node with an illegal result
6081/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00006082void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
6083 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006084 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00006085 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006086 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006087 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00006088 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peck527da1b2010-11-23 03:31:01 +00006089 case ISD::BITCAST:
6090 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006091 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006092 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00006093 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00006094 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006095 break;
Tim Northoverbc933082013-05-23 19:11:20 +00006096 case ISD::READCYCLECOUNTER:
6097 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
6098 return;
Duncan Sands6ed40142008-12-01 11:39:25 +00006099 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00006100 if (Res.getNode())
6101 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00006102}
Chris Lattnerf81d5882007-11-24 07:07:01 +00006103
Evan Cheng10043e22007-01-19 07:51:42 +00006104//===----------------------------------------------------------------------===//
6105// ARM Scheduler Hooks
6106//===----------------------------------------------------------------------===//
6107
Bill Wendling030b58e2011-10-06 22:18:16 +00006108/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6109/// registers the function context.
6110void ARMTargetLowering::
6111SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6112 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendling374ee192011-10-03 21:25:38 +00006113 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6114 DebugLoc dl = MI->getDebugLoc();
6115 MachineFunction *MF = MBB->getParent();
6116 MachineRegisterInfo *MRI = &MF->getRegInfo();
6117 MachineConstantPool *MCP = MF->getConstantPool();
6118 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6119 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00006120
Bill Wendling374ee192011-10-03 21:25:38 +00006121 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006122 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00006123
Bill Wendling374ee192011-10-03 21:25:38 +00006124 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006125 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00006126 ARMConstantPoolValue *CPV =
6127 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6128 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6129
Craig Topperc7242e02012-04-20 07:30:17 +00006130 const TargetRegisterClass *TRC = isThumb ?
6131 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6132 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00006133
Bill Wendling030b58e2011-10-06 22:18:16 +00006134 // Grab constant pool and fixed stack memory operands.
6135 MachineMemOperand *CPMMO =
6136 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6137 MachineMemOperand::MOLoad, 4, 4);
6138
6139 MachineMemOperand *FIMMOSt =
6140 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6141 MachineMemOperand::MOStore, 4, 4);
6142
6143 // Load the address of the dispatch MBB into the jump buffer.
6144 if (isThumb2) {
6145 // Incoming value: jbuf
6146 // ldr.n r5, LCPI1_1
6147 // orr r5, r5, #1
6148 // add r5, pc
6149 // str r5, [$jbuf, #+4] ; &jbuf[1]
6150 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6151 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6152 .addConstantPoolIndex(CPI)
6153 .addMemOperand(CPMMO));
6154 // Set the low bit because of thumb mode.
6155 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6156 AddDefaultCC(
6157 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6158 .addReg(NewVReg1, RegState::Kill)
6159 .addImm(0x01)));
6160 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6161 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6162 .addReg(NewVReg2, RegState::Kill)
6163 .addImm(PCLabelId);
6164 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6165 .addReg(NewVReg3, RegState::Kill)
6166 .addFrameIndex(FI)
6167 .addImm(36) // &jbuf[1] :: pc
6168 .addMemOperand(FIMMOSt));
6169 } else if (isThumb) {
6170 // Incoming value: jbuf
6171 // ldr.n r1, LCPI1_4
6172 // add r1, pc
6173 // mov r2, #1
6174 // orrs r1, r2
6175 // add r2, $jbuf, #+4 ; &jbuf[1]
6176 // str r1, [r2]
6177 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6178 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6179 .addConstantPoolIndex(CPI)
6180 .addMemOperand(CPMMO));
6181 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6182 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6183 .addReg(NewVReg1, RegState::Kill)
6184 .addImm(PCLabelId);
6185 // Set the low bit because of thumb mode.
6186 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6187 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6188 .addReg(ARM::CPSR, RegState::Define)
6189 .addImm(1));
6190 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6191 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6192 .addReg(ARM::CPSR, RegState::Define)
6193 .addReg(NewVReg2, RegState::Kill)
6194 .addReg(NewVReg3, RegState::Kill));
6195 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6196 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
6197 .addFrameIndex(FI)
6198 .addImm(36)); // &jbuf[1] :: pc
6199 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6200 .addReg(NewVReg4, RegState::Kill)
6201 .addReg(NewVReg5, RegState::Kill)
6202 .addImm(0)
6203 .addMemOperand(FIMMOSt));
6204 } else {
6205 // Incoming value: jbuf
6206 // ldr r1, LCPI1_1
6207 // add r1, pc, r1
6208 // str r1, [$jbuf, #+4] ; &jbuf[1]
6209 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6210 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6211 .addConstantPoolIndex(CPI)
6212 .addImm(0)
6213 .addMemOperand(CPMMO));
6214 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6215 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6216 .addReg(NewVReg1, RegState::Kill)
6217 .addImm(PCLabelId));
6218 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6219 .addReg(NewVReg2, RegState::Kill)
6220 .addFrameIndex(FI)
6221 .addImm(36) // &jbuf[1] :: pc
6222 .addMemOperand(FIMMOSt));
6223 }
6224}
6225
6226MachineBasicBlock *ARMTargetLowering::
6227EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
6228 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6229 DebugLoc dl = MI->getDebugLoc();
6230 MachineFunction *MF = MBB->getParent();
6231 MachineRegisterInfo *MRI = &MF->getRegInfo();
6232 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6233 MachineFrameInfo *MFI = MF->getFrameInfo();
6234 int FI = MFI->getFunctionContextIndex();
6235
Craig Topperc7242e02012-04-20 07:30:17 +00006236 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
6237 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen691ae332012-05-20 06:38:47 +00006238 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00006239
Bill Wendling362c1b02011-10-06 21:29:56 +00006240 // Get a mapping of the call site numbers to all of the landing pads they're
6241 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00006242 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6243 unsigned MaxCSNum = 0;
6244 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00006245 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6246 ++BB) {
Bill Wendling202803e2011-10-05 00:02:33 +00006247 if (!BB->isLandingPad()) continue;
6248
6249 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6250 // pad.
6251 for (MachineBasicBlock::iterator
6252 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6253 if (!II->isEHLabel()) continue;
6254
6255 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006256 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00006257
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006258 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6259 for (SmallVectorImpl<unsigned>::iterator
6260 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6261 CSI != CSE; ++CSI) {
6262 CallSiteNumToLPad[*CSI].push_back(BB);
6263 MaxCSNum = std::max(MaxCSNum, *CSI);
6264 }
Bill Wendling202803e2011-10-05 00:02:33 +00006265 break;
6266 }
6267 }
6268
6269 // Get an ordered list of the machine basic blocks for the jump table.
6270 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling883ec972011-10-07 23:18:02 +00006271 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00006272 LPadList.reserve(CallSiteNumToLPad.size());
6273 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6274 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6275 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006276 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00006277 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00006278 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6279 }
Bill Wendling202803e2011-10-05 00:02:33 +00006280 }
6281
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006282 assert(!LPadList.empty() &&
6283 "No landing pad destinations for the dispatch jump table!");
6284
Bill Wendling362c1b02011-10-06 21:29:56 +00006285 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00006286 MachineJumpTableInfo *JTI =
6287 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6288 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
6289 unsigned UId = AFI->createJumpTableUId();
Chad Rosier96603432013-03-01 18:30:38 +00006290 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00006291
Bill Wendling362c1b02011-10-06 21:29:56 +00006292 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00006293
6294 // Shove the dispatch's address into the return slot in the function context.
6295 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6296 DispatchBB->setIsLandingPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00006297
Bill Wendling324be982011-10-05 00:39:32 +00006298 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006299 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00006300 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006301 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00006302 else
6303 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
6304
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006305 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00006306 DispatchBB->addSuccessor(TrapBB);
6307
6308 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6309 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00006310
Bill Wendling510fbcd2011-10-17 21:32:56 +00006311 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00006312 MF->insert(MF->end(), DispatchBB);
6313 MF->insert(MF->end(), DispContBB);
6314 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00006315
Bill Wendling030b58e2011-10-06 22:18:16 +00006316 // Insert code into the entry block that creates and registers the function
6317 // context.
6318 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6319
Bill Wendling030b58e2011-10-06 22:18:16 +00006320 MachineMemOperand *FIMMOLd =
Bill Wendling362c1b02011-10-06 21:29:56 +00006321 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendlingb3d46782011-10-06 23:37:36 +00006322 MachineMemOperand::MOLoad |
6323 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00006324
Chad Rosier1ec8e402012-11-06 23:05:24 +00006325 MachineInstrBuilder MIB;
6326 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6327
6328 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6329 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6330
6331 // Add a register mask with no preserved registers. This results in all
6332 // registers being marked as clobbered.
6333 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00006334
Bill Wendling85833f72011-10-18 22:49:07 +00006335 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00006336 if (Subtarget->isThumb2()) {
6337 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6338 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6339 .addFrameIndex(FI)
6340 .addImm(4)
6341 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006342
Bill Wendling85833f72011-10-18 22:49:07 +00006343 if (NumLPads < 256) {
6344 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6345 .addReg(NewVReg1)
6346 .addImm(LPadList.size()));
6347 } else {
6348 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6349 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006350 .addImm(NumLPads & 0xFFFF));
6351
6352 unsigned VReg2 = VReg1;
6353 if ((NumLPads & 0xFFFF0000) != 0) {
6354 VReg2 = MRI->createVirtualRegister(TRC);
6355 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6356 .addReg(VReg1)
6357 .addImm(NumLPads >> 16));
6358 }
6359
Bill Wendling85833f72011-10-18 22:49:07 +00006360 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6361 .addReg(NewVReg1)
6362 .addReg(VReg2));
6363 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006364
Bill Wendling5626c662011-10-06 22:53:00 +00006365 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6366 .addMBB(TrapBB)
6367 .addImm(ARMCC::HI)
6368 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00006369
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006370 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6371 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006372 .addJumpTableIndex(MJTI)
6373 .addImm(UId));
Bill Wendling202803e2011-10-05 00:02:33 +00006374
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006375 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006376 AddDefaultCC(
6377 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006378 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6379 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00006380 .addReg(NewVReg1)
6381 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6382
6383 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006384 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00006385 .addReg(NewVReg1)
Bill Wendling5626c662011-10-06 22:53:00 +00006386 .addJumpTableIndex(MJTI)
6387 .addImm(UId);
6388 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00006389 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6390 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6391 .addFrameIndex(FI)
6392 .addImm(1)
6393 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00006394
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006395 if (NumLPads < 256) {
6396 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6397 .addReg(NewVReg1)
6398 .addImm(NumLPads));
6399 } else {
6400 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00006401 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6402 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6403
6404 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006405 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006406 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006407 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006408 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006409
6410 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6411 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6412 .addReg(VReg1, RegState::Define)
6413 .addConstantPoolIndex(Idx));
6414 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6415 .addReg(NewVReg1)
6416 .addReg(VReg1));
6417 }
6418
Bill Wendlingb3d46782011-10-06 23:37:36 +00006419 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6420 .addMBB(TrapBB)
6421 .addImm(ARMCC::HI)
6422 .addReg(ARM::CPSR);
6423
6424 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6425 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6426 .addReg(ARM::CPSR, RegState::Define)
6427 .addReg(NewVReg1)
6428 .addImm(2));
6429
6430 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00006431 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendlingb3d46782011-10-06 23:37:36 +00006432 .addJumpTableIndex(MJTI)
6433 .addImm(UId));
6434
6435 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6436 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6437 .addReg(ARM::CPSR, RegState::Define)
6438 .addReg(NewVReg2, RegState::Kill)
6439 .addReg(NewVReg3));
6440
6441 MachineMemOperand *JTMMOLd =
6442 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6443 MachineMemOperand::MOLoad, 4, 4);
6444
6445 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6446 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6447 .addReg(NewVReg4, RegState::Kill)
6448 .addImm(0)
6449 .addMemOperand(JTMMOLd));
6450
Chad Rosier96603432013-03-01 18:30:38 +00006451 unsigned NewVReg6 = NewVReg5;
6452 if (RelocM == Reloc::PIC_) {
6453 NewVReg6 = MRI->createVirtualRegister(TRC);
6454 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6455 .addReg(ARM::CPSR, RegState::Define)
6456 .addReg(NewVReg5, RegState::Kill)
6457 .addReg(NewVReg3));
6458 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00006459
6460 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6461 .addReg(NewVReg6, RegState::Kill)
6462 .addJumpTableIndex(MJTI)
6463 .addImm(UId);
Bill Wendling5626c662011-10-06 22:53:00 +00006464 } else {
6465 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6466 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6467 .addFrameIndex(FI)
6468 .addImm(4)
6469 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00006470
Bill Wendling4969dcd2011-10-18 22:52:20 +00006471 if (NumLPads < 256) {
6472 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6473 .addReg(NewVReg1)
6474 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00006475 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00006476 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6477 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006478 .addImm(NumLPads & 0xFFFF));
6479
6480 unsigned VReg2 = VReg1;
6481 if ((NumLPads & 0xFFFF0000) != 0) {
6482 VReg2 = MRI->createVirtualRegister(TRC);
6483 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6484 .addReg(VReg1)
6485 .addImm(NumLPads >> 16));
6486 }
6487
Bill Wendling4969dcd2011-10-18 22:52:20 +00006488 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6489 .addReg(NewVReg1)
6490 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00006491 } else {
6492 MachineConstantPool *ConstantPool = MF->getConstantPool();
6493 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6494 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6495
6496 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006497 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006498 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006499 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006500 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6501
6502 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6503 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6504 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00006505 .addConstantPoolIndex(Idx)
6506 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00006507 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6508 .addReg(NewVReg1)
6509 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00006510 }
6511
Bill Wendling5626c662011-10-06 22:53:00 +00006512 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6513 .addMBB(TrapBB)
6514 .addImm(ARMCC::HI)
6515 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00006516
Bill Wendling973c8172011-10-18 22:11:18 +00006517 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006518 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00006519 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006520 .addReg(NewVReg1)
6521 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00006522 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6523 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006524 .addJumpTableIndex(MJTI)
6525 .addImm(UId));
6526
6527 MachineMemOperand *JTMMOLd =
6528 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6529 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00006530 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006531 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00006532 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6533 .addReg(NewVReg3, RegState::Kill)
6534 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006535 .addImm(0)
6536 .addMemOperand(JTMMOLd));
6537
Chad Rosier96603432013-03-01 18:30:38 +00006538 if (RelocM == Reloc::PIC_) {
6539 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
6540 .addReg(NewVReg5, RegState::Kill)
6541 .addReg(NewVReg4)
6542 .addJumpTableIndex(MJTI)
6543 .addImm(UId);
6544 } else {
6545 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
6546 .addReg(NewVReg5, RegState::Kill)
6547 .addJumpTableIndex(MJTI)
6548 .addImm(UId);
6549 }
Bill Wendling5626c662011-10-06 22:53:00 +00006550 }
Bill Wendling202803e2011-10-05 00:02:33 +00006551
Bill Wendling324be982011-10-05 00:39:32 +00006552 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006553 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00006554 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006555 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6556 MachineBasicBlock *CurMBB = *I;
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006557 if (SeenMBBs.insert(CurMBB))
Bill Wendling883ec972011-10-07 23:18:02 +00006558 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006559 }
6560
Bill Wendling26d27802011-10-17 05:25:09 +00006561 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00006562 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00006563 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling883ec972011-10-07 23:18:02 +00006564 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6565 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6566 MachineBasicBlock *BB = *I;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006567
6568 // Remove the landing pad successor from the invoke block and replace it
6569 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00006570 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6571 BB->succ_end());
6572 while (!Successors.empty()) {
6573 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling883ec972011-10-07 23:18:02 +00006574 if (SMBB->isLandingPad()) {
6575 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00006576 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006577 }
6578 }
6579
6580 BB->addSuccessor(DispatchBB);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006581
6582 // Find the invoke call and mark all of the callee-saved registers as
6583 // 'implicit defined' so that they're spilled. This prevents code from
6584 // moving instructions to before the EH block, where they will never be
6585 // executed.
6586 for (MachineBasicBlock::reverse_iterator
6587 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00006588 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006589
6590 DenseMap<unsigned, bool> DefRegs;
6591 for (MachineInstr::mop_iterator
6592 OI = II->operands_begin(), OE = II->operands_end();
6593 OI != OE; ++OI) {
6594 if (!OI->isReg()) continue;
6595 DefRegs[OI->getReg()] = true;
6596 }
6597
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00006598 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006599
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006600 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00006601 unsigned Reg = SavedRegs[i];
6602 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00006603 !ARM::tGPRRegClass.contains(Reg) &&
6604 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006605 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006606 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006607 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006608 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006609 continue;
6610 if (!DefRegs[Reg])
6611 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006612 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006613
6614 break;
6615 }
Bill Wendling883ec972011-10-07 23:18:02 +00006616 }
Bill Wendling324be982011-10-05 00:39:32 +00006617
Bill Wendling617075f2011-10-18 18:30:49 +00006618 // Mark all former landing pads as non-landing pads. The dispatch is the only
6619 // landing pad now.
6620 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6621 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6622 (*I)->setIsLandingPad(false);
6623
Bill Wendling324be982011-10-05 00:39:32 +00006624 // The instruction is gone now.
6625 MI->eraseFromParent();
6626
Bill Wendling374ee192011-10-03 21:25:38 +00006627 return MBB;
6628}
6629
Evan Cheng0cc4ad92010-07-13 19:27:42 +00006630static
6631MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6632 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6633 E = MBB->succ_end(); I != E; ++I)
6634 if (*I != Succ)
6635 return *I;
6636 llvm_unreachable("Expecting a BB with two successors!");
6637}
6638
Manman Renb504f492013-10-29 22:27:32 +00006639/// Return the load opcode for a given load size. If load size >= 8,
6640/// neon opcode will be returned.
6641static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
6642 if (LdSize >= 8)
6643 return LdSize == 16 ? ARM::VLD1q32wb_fixed
6644 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
6645 if (IsThumb1)
6646 return LdSize == 4 ? ARM::tLDRi
6647 : LdSize == 2 ? ARM::tLDRHi
6648 : LdSize == 1 ? ARM::tLDRBi : 0;
6649 if (IsThumb2)
6650 return LdSize == 4 ? ARM::t2LDR_POST
6651 : LdSize == 2 ? ARM::t2LDRH_POST
6652 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
6653 return LdSize == 4 ? ARM::LDR_POST_IMM
6654 : LdSize == 2 ? ARM::LDRH_POST
6655 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
6656}
6657
6658/// Return the store opcode for a given store size. If store size >= 8,
6659/// neon opcode will be returned.
6660static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
6661 if (StSize >= 8)
6662 return StSize == 16 ? ARM::VST1q32wb_fixed
6663 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
6664 if (IsThumb1)
6665 return StSize == 4 ? ARM::tSTRi
6666 : StSize == 2 ? ARM::tSTRHi
6667 : StSize == 1 ? ARM::tSTRBi : 0;
6668 if (IsThumb2)
6669 return StSize == 4 ? ARM::t2STR_POST
6670 : StSize == 2 ? ARM::t2STRH_POST
6671 : StSize == 1 ? ARM::t2STRB_POST : 0;
6672 return StSize == 4 ? ARM::STR_POST_IMM
6673 : StSize == 2 ? ARM::STRH_POST
6674 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
6675}
6676
6677/// Emit a post-increment load operation with given size. The instructions
6678/// will be added to BB at Pos.
6679static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
6680 const TargetInstrInfo *TII, DebugLoc dl,
6681 unsigned LdSize, unsigned Data, unsigned AddrIn,
6682 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
6683 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
6684 assert(LdOpc != 0 && "Should have a load opcode");
6685 if (LdSize >= 8) {
6686 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6687 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6688 .addImm(0));
6689 } else if (IsThumb1) {
6690 // load + update AddrIn
6691 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6692 .addReg(AddrIn).addImm(0));
6693 MachineInstrBuilder MIB =
6694 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
6695 MIB = AddDefaultT1CC(MIB);
6696 MIB.addReg(AddrIn).addImm(LdSize);
6697 AddDefaultPred(MIB);
6698 } else if (IsThumb2) {
6699 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6700 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6701 .addImm(LdSize));
6702 } else { // arm
6703 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6704 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6705 .addReg(0).addImm(LdSize));
6706 }
6707}
6708
6709/// Emit a post-increment store operation with given size. The instructions
6710/// will be added to BB at Pos.
6711static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
6712 const TargetInstrInfo *TII, DebugLoc dl,
6713 unsigned StSize, unsigned Data, unsigned AddrIn,
6714 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
6715 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
6716 assert(StOpc != 0 && "Should have a store opcode");
6717 if (StSize >= 8) {
6718 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6719 .addReg(AddrIn).addImm(0).addReg(Data));
6720 } else if (IsThumb1) {
6721 // store + update AddrIn
6722 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
6723 .addReg(AddrIn).addImm(0));
6724 MachineInstrBuilder MIB =
6725 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
6726 MIB = AddDefaultT1CC(MIB);
6727 MIB.addReg(AddrIn).addImm(StSize);
6728 AddDefaultPred(MIB);
6729 } else if (IsThumb2) {
6730 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6731 .addReg(Data).addReg(AddrIn).addImm(StSize));
6732 } else { // arm
6733 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6734 .addReg(Data).addReg(AddrIn).addReg(0)
6735 .addImm(StSize));
6736 }
6737}
6738
David Peixottoc32e24a2013-10-17 19:49:22 +00006739MachineBasicBlock *
6740ARMTargetLowering::EmitStructByval(MachineInstr *MI,
6741 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00006742 // This pseudo instruction has 3 operands: dst, src, size
6743 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6744 // Otherwise, we will generate unrolled scalar copies.
6745 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6746 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6747 MachineFunction::iterator It = BB;
6748 ++It;
6749
6750 unsigned dest = MI->getOperand(0).getReg();
6751 unsigned src = MI->getOperand(1).getReg();
6752 unsigned SizeVal = MI->getOperand(2).getImm();
6753 unsigned Align = MI->getOperand(3).getImm();
6754 DebugLoc dl = MI->getDebugLoc();
6755
Manman Rene8735522012-06-01 19:33:18 +00006756 MachineFunction *MF = BB->getParent();
6757 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00006758 unsigned UnitSize = 0;
David Peixottob0653e532013-10-24 16:39:36 +00006759 const TargetRegisterClass *TRC = 0;
6760 const TargetRegisterClass *VecTRC = 0;
6761
6762 bool IsThumb1 = Subtarget->isThumb1Only();
6763 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00006764
6765 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00006766 UnitSize = 1;
6767 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00006768 UnitSize = 2;
6769 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00006770 // Check whether we can use NEON instructions.
Bill Wendling698e84f2012-12-30 10:32:01 +00006771 if (!MF->getFunction()->getAttributes().
6772 hasAttribute(AttributeSet::FunctionIndex,
6773 Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00006774 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00006775 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00006776 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00006777 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00006778 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00006779 }
6780 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00006781 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00006782 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00006783 }
Manman Ren6e1fd462012-06-18 22:23:48 +00006784
David Peixottob0653e532013-10-24 16:39:36 +00006785 // Select the correct opcode and register class for unit size load/store
6786 bool IsNeon = UnitSize >= 8;
6787 TRC = (IsThumb1 || IsThumb2) ? (const TargetRegisterClass *)&ARM::tGPRRegClass
6788 : (const TargetRegisterClass *)&ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00006789 if (IsNeon)
David Peixottob0653e532013-10-24 16:39:36 +00006790 VecTRC = UnitSize == 16
6791 ? (const TargetRegisterClass *)&ARM::DPairRegClass
6792 : UnitSize == 8
6793 ? (const TargetRegisterClass *)&ARM::DPRRegClass
6794 : 0;
David Peixottob0653e532013-10-24 16:39:36 +00006795
Manman Rene8735522012-06-01 19:33:18 +00006796 unsigned BytesLeft = SizeVal % UnitSize;
6797 unsigned LoopSize = SizeVal - BytesLeft;
6798
6799 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6800 // Use LDR and STR to copy.
6801 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6802 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6803 unsigned srcIn = src;
6804 unsigned destIn = dest;
6805 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00006806 unsigned srcOut = MRI.createVirtualRegister(TRC);
6807 unsigned destOut = MRI.createVirtualRegister(TRC);
6808 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00006809 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
6810 IsThumb1, IsThumb2);
6811 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
6812 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00006813 srcIn = srcOut;
6814 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00006815 }
6816
6817 // Handle the leftover bytes with LDRB and STRB.
6818 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6819 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00006820 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00006821 unsigned srcOut = MRI.createVirtualRegister(TRC);
6822 unsigned destOut = MRI.createVirtualRegister(TRC);
6823 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00006824 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
6825 IsThumb1, IsThumb2);
6826 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
6827 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00006828 srcIn = srcOut;
6829 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00006830 }
6831 MI->eraseFromParent(); // The instruction is gone now.
6832 return BB;
6833 }
6834
6835 // Expand the pseudo op to a loop.
6836 // thisMBB:
6837 // ...
6838 // movw varEnd, # --> with thumb2
6839 // movt varEnd, #
6840 // ldrcp varEnd, idx --> without thumb2
6841 // fallthrough --> loopMBB
6842 // loopMBB:
6843 // PHI varPhi, varEnd, varLoop
6844 // PHI srcPhi, src, srcLoop
6845 // PHI destPhi, dst, destLoop
6846 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6847 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
6848 // subs varLoop, varPhi, #UnitSize
6849 // bne loopMBB
6850 // fallthrough --> exitMBB
6851 // exitMBB:
6852 // epilogue to handle left-over bytes
6853 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6854 // [destOut] = STRB_POST(scratch, destLoop, 1)
6855 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6856 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6857 MF->insert(It, loopMBB);
6858 MF->insert(It, exitMBB);
6859
6860 // Transfer the remainder of BB and its successor edges to exitMBB.
6861 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006862 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00006863 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6864
6865 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00006866 unsigned varEnd = MRI.createVirtualRegister(TRC);
6867 if (IsThumb2) {
6868 unsigned Vtmp = varEnd;
6869 if ((LoopSize & 0xFFFF0000) != 0)
6870 Vtmp = MRI.createVirtualRegister(TRC);
6871 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), Vtmp)
6872 .addImm(LoopSize & 0xFFFF));
6873
6874 if ((LoopSize & 0xFFFF0000) != 0)
6875 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
6876 .addReg(Vtmp).addImm(LoopSize >> 16));
6877 } else {
6878 MachineConstantPool *ConstantPool = MF->getConstantPool();
6879 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6880 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
6881
6882 // MachineConstantPool wants an explicit alignment.
6883 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
6884 if (Align == 0)
6885 Align = getDataLayout()->getTypeAllocSize(C->getType());
6886 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6887
6888 if (IsThumb1)
6889 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
6890 varEnd, RegState::Define).addConstantPoolIndex(Idx));
6891 else
6892 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
6893 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
6894 }
Manman Rene8735522012-06-01 19:33:18 +00006895 BB->addSuccessor(loopMBB);
6896
6897 // Generate the loop body:
6898 // varPhi = PHI(varLoop, varEnd)
6899 // srcPhi = PHI(srcLoop, src)
6900 // destPhi = PHI(destLoop, dst)
6901 MachineBasicBlock *entryBB = BB;
6902 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00006903 unsigned varLoop = MRI.createVirtualRegister(TRC);
6904 unsigned varPhi = MRI.createVirtualRegister(TRC);
6905 unsigned srcLoop = MRI.createVirtualRegister(TRC);
6906 unsigned srcPhi = MRI.createVirtualRegister(TRC);
6907 unsigned destLoop = MRI.createVirtualRegister(TRC);
6908 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00006909
6910 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
6911 .addReg(varLoop).addMBB(loopMBB)
6912 .addReg(varEnd).addMBB(entryBB);
6913 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
6914 .addReg(srcLoop).addMBB(loopMBB)
6915 .addReg(src).addMBB(entryBB);
6916 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
6917 .addReg(destLoop).addMBB(loopMBB)
6918 .addReg(dest).addMBB(entryBB);
6919
6920 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6921 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00006922 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00006923 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
6924 IsThumb1, IsThumb2);
6925 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
6926 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00006927
6928 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00006929 if (IsThumb1) {
6930 MachineInstrBuilder MIB =
6931 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
6932 MIB = AddDefaultT1CC(MIB);
6933 MIB.addReg(varPhi).addImm(UnitSize);
6934 AddDefaultPred(MIB);
6935 } else {
6936 MachineInstrBuilder MIB =
6937 BuildMI(*BB, BB->end(), dl,
6938 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
6939 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
6940 MIB->getOperand(5).setReg(ARM::CPSR);
6941 MIB->getOperand(5).setIsDef(true);
6942 }
6943 BuildMI(*BB, BB->end(), dl,
6944 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
6945 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00006946
6947 // loopMBB can loop back to loopMBB or fall through to exitMBB.
6948 BB->addSuccessor(loopMBB);
6949 BB->addSuccessor(exitMBB);
6950
6951 // Add epilogue to handle BytesLeft.
6952 BB = exitMBB;
6953 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00006954
6955 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6956 // [destOut] = STRB_POST(scratch, destLoop, 1)
6957 unsigned srcIn = srcLoop;
6958 unsigned destIn = destLoop;
6959 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00006960 unsigned srcOut = MRI.createVirtualRegister(TRC);
6961 unsigned destOut = MRI.createVirtualRegister(TRC);
6962 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00006963 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
6964 IsThumb1, IsThumb2);
6965 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
6966 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00006967 srcIn = srcOut;
6968 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00006969 }
6970
6971 MI->eraseFromParent(); // The instruction is gone now.
6972 return BB;
6973}
6974
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00006975MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00006976ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00006977 MachineBasicBlock *BB) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006978 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00006979 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00006980 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00006981 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00006982 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00006983 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00006984 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00006985 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00006986 // The Thumb2 pre-indexed stores have the same MI operands, they just
6987 // define them differently in the .td files from the isel patterns, so
6988 // they need pseudos.
6989 case ARM::t2STR_preidx:
6990 MI->setDesc(TII->get(ARM::t2STR_PRE));
6991 return BB;
6992 case ARM::t2STRB_preidx:
6993 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6994 return BB;
6995 case ARM::t2STRH_preidx:
6996 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6997 return BB;
6998
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00006999 case ARM::STRi_preidx:
7000 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00007001 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007002 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7003 // Decode the offset.
7004 unsigned Offset = MI->getOperand(4).getImm();
7005 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7006 Offset = ARM_AM::getAM2Offset(Offset);
7007 if (isSub)
7008 Offset = -Offset;
7009
Jim Grosbachf402f692011-08-12 21:02:34 +00007010 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00007011 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007012 .addOperand(MI->getOperand(0)) // Rn_wb
7013 .addOperand(MI->getOperand(1)) // Rt
7014 .addOperand(MI->getOperand(2)) // Rn
7015 .addImm(Offset) // offset (skip GPR==zero_reg)
7016 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00007017 .addOperand(MI->getOperand(6))
7018 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007019 MI->eraseFromParent();
7020 return BB;
7021 }
7022 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00007023 case ARM::STRBr_preidx:
7024 case ARM::STRH_preidx: {
7025 unsigned NewOpc;
7026 switch (MI->getOpcode()) {
7027 default: llvm_unreachable("unexpected opcode!");
7028 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7029 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7030 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7031 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007032 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7033 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7034 MIB.addOperand(MI->getOperand(i));
7035 MI->eraseFromParent();
7036 return BB;
7037 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00007038
Evan Chengbb2af352009-08-12 05:17:19 +00007039 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00007040 // To "insert" a SELECT_CC instruction, we actually have to insert the
7041 // diamond control-flow pattern. The incoming instruction knows the
7042 // destination vreg to set, the condition code register to branch on, the
7043 // true/false values to select between, and a branch opcode to use.
7044 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00007045 MachineFunction::iterator It = BB;
Evan Cheng10043e22007-01-19 07:51:42 +00007046 ++It;
7047
7048 // thisMBB:
7049 // ...
7050 // TrueVal = ...
7051 // cmpTY ccX, r1, r2
7052 // bCC copy1MBB
7053 // fallthrough --> copy0MBB
7054 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00007055 MachineFunction *F = BB->getParent();
7056 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7057 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00007058 F->insert(It, copy0MBB);
7059 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007060
7061 // Transfer the remainder of BB and its successor edges to sinkMBB.
7062 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007063 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007064 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7065
Dan Gohmanf4f04102010-07-06 15:49:48 +00007066 BB->addSuccessor(copy0MBB);
7067 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00007068
Dan Gohman34396292010-07-06 20:24:04 +00007069 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7070 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7071
Evan Cheng10043e22007-01-19 07:51:42 +00007072 // copy0MBB:
7073 // %FalseValue = ...
7074 // # fallthrough to sinkMBB
7075 BB = copy0MBB;
7076
7077 // Update machine-CFG edges
7078 BB->addSuccessor(sinkMBB);
7079
7080 // sinkMBB:
7081 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7082 // ...
7083 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007084 BuildMI(*BB, BB->begin(), dl,
7085 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00007086 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7087 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7088
Dan Gohman34396292010-07-06 20:24:04 +00007089 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00007090 return BB;
7091 }
Evan Chengb972e562009-08-07 00:34:42 +00007092
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007093 case ARM::BCCi64:
7094 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00007095 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007096 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00007097
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007098 // Compare both parts that make up the double comparison separately for
7099 // equality.
7100 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7101
7102 unsigned LHS1 = MI->getOperand(1).getReg();
7103 unsigned LHS2 = MI->getOperand(2).getReg();
7104 if (RHSisZero) {
7105 AddDefaultPred(BuildMI(BB, dl,
7106 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7107 .addReg(LHS1).addImm(0));
7108 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7109 .addReg(LHS2).addImm(0)
7110 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7111 } else {
7112 unsigned RHS1 = MI->getOperand(3).getReg();
7113 unsigned RHS2 = MI->getOperand(4).getReg();
7114 AddDefaultPred(BuildMI(BB, dl,
7115 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7116 .addReg(LHS1).addReg(RHS1));
7117 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7118 .addReg(LHS2).addReg(RHS2)
7119 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7120 }
7121
7122 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7123 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7124 if (MI->getOperand(0).getImm() == ARMCC::NE)
7125 std::swap(destMBB, exitMBB);
7126
7127 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7128 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00007129 if (isThumb2)
7130 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7131 else
7132 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007133
7134 MI->eraseFromParent(); // The pseudo instruction is gone now.
7135 return BB;
7136 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007137
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007138 case ARM::Int_eh_sjlj_setjmp:
7139 case ARM::Int_eh_sjlj_setjmp_nofp:
7140 case ARM::tInt_eh_sjlj_setjmp:
7141 case ARM::t2Int_eh_sjlj_setjmp:
7142 case ARM::t2Int_eh_sjlj_setjmp_nofp:
7143 EmitSjLjDispatchBlock(MI, BB);
7144 return BB;
7145
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007146 case ARM::ABS:
7147 case ARM::t2ABS: {
7148 // To insert an ABS instruction, we have to insert the
7149 // diamond control-flow pattern. The incoming instruction knows the
7150 // source vreg to test against 0, the destination vreg to set,
7151 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00007152 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007153 // It transforms
7154 // V1 = ABS V0
7155 // into
7156 // V2 = MOVS V0
7157 // BCC (branch to SinkBB if V0 >= 0)
7158 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00007159 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007160 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7161 MachineFunction::iterator BBI = BB;
7162 ++BBI;
7163 MachineFunction *Fn = BB->getParent();
7164 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7165 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7166 Fn->insert(BBI, RSBBB);
7167 Fn->insert(BBI, SinkBB);
7168
7169 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7170 unsigned int ABSDstReg = MI->getOperand(0).getReg();
7171 bool isThumb2 = Subtarget->isThumb2();
7172 MachineRegisterInfo &MRI = Fn->getRegInfo();
7173 // In Thumb mode S must not be specified if source register is the SP or
7174 // PC and if destination register is the SP, so restrict register class
Craig Topperc7242e02012-04-20 07:30:17 +00007175 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
7176 (const TargetRegisterClass*)&ARM::rGPRRegClass :
7177 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007178
7179 // Transfer the remainder of BB and its successor edges to sinkMBB.
7180 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007181 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007182 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7183
7184 BB->addSuccessor(RSBBB);
7185 BB->addSuccessor(SinkBB);
7186
7187 // fall through to SinkMBB
7188 RSBBB->addSuccessor(SinkBB);
7189
Manman Rene0763c72012-06-15 21:32:12 +00007190 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00007191 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00007192 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7193 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007194
7195 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00007196 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007197 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7198 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7199
7200 // insert rsbri in RSBBB
7201 // Note: BCC and rsbri will be converted into predicated rsbmi
7202 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00007203 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007204 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Rene0763c72012-06-15 21:32:12 +00007205 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007206 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7207
Andrew Trick3f07c422011-10-18 18:40:53 +00007208 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007209 // reuse ABSDstReg to not change uses of ABS instruction
7210 BuildMI(*SinkBB, SinkBB->begin(), dl,
7211 TII->get(ARM::PHI), ABSDstReg)
7212 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00007213 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007214
7215 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00007216 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007217
7218 // return last added BB
7219 return SinkBB;
7220 }
Manman Rene8735522012-06-01 19:33:18 +00007221 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00007222 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00007223 return EmitStructByval(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00007224 }
7225}
7226
Evan Chenge6fba772011-08-30 19:09:48 +00007227void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7228 SDNode *Node) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007229 if (!MI->hasPostISelHook()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007230 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
7231 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
7232 return;
7233 }
7234
Evan Cheng7f8e5632011-12-07 07:15:52 +00007235 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00007236 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7237 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7238 // operand is still set to noreg. If needed, set the optional operand's
7239 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00007240 //
Andrew Trick88b24502011-10-18 19:18:52 +00007241 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00007242
Andrew Trick924123a2011-09-21 02:20:46 +00007243 // Rename pseudo opcodes.
7244 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7245 if (NewOpc) {
7246 const ARMBaseInstrInfo *TII =
7247 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick88b24502011-10-18 19:18:52 +00007248 MCID = &TII->get(NewOpc);
7249
7250 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7251 "converted opcode should be the same except for cc_out");
7252
7253 MI->setDesc(*MCID);
7254
7255 // Add the optional cc_out operand
7256 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00007257 }
Andrew Trick88b24502011-10-18 19:18:52 +00007258 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00007259
7260 // Any ARM instruction that sets the 's' bit should specify an optional
7261 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00007262 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007263 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007264 return;
7265 }
Andrew Trick924123a2011-09-21 02:20:46 +00007266 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7267 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007268 bool definesCPSR = false;
7269 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00007270 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00007271 i != e; ++i) {
7272 const MachineOperand &MO = MI->getOperand(i);
7273 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7274 definesCPSR = true;
7275 if (MO.isDead())
7276 deadCPSR = true;
7277 MI->RemoveOperand(i);
7278 break;
Evan Chenge6fba772011-08-30 19:09:48 +00007279 }
7280 }
Andrew Trick8586e622011-09-20 03:17:40 +00007281 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00007282 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007283 return;
7284 }
7285 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00007286 if (deadCPSR) {
7287 assert(!MI->getOperand(ccOutIdx).getReg() &&
7288 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00007289 return;
Andrew Trick924123a2011-09-21 02:20:46 +00007290 }
Andrew Trick8586e622011-09-20 03:17:40 +00007291
Andrew Trick924123a2011-09-21 02:20:46 +00007292 // If this instruction was defined with an optional CPSR def and its dag node
7293 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007294 MachineOperand &MO = MI->getOperand(ccOutIdx);
7295 MO.setReg(ARM::CPSR);
7296 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00007297}
7298
Evan Cheng10043e22007-01-19 07:51:42 +00007299//===----------------------------------------------------------------------===//
7300// ARM Optimization Hooks
7301//===----------------------------------------------------------------------===//
7302
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007303// Helper function that checks if N is a null or all ones constant.
7304static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7305 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7306 if (!C)
7307 return false;
7308 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7309}
7310
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007311// Return true if N is conditionally 0 or all ones.
7312// Detects these expressions where cc is an i1 value:
7313//
7314// (select cc 0, y) [AllOnes=0]
7315// (select cc y, 0) [AllOnes=0]
7316// (zext cc) [AllOnes=0]
7317// (sext cc) [AllOnes=0/1]
7318// (select cc -1, y) [AllOnes=1]
7319// (select cc y, -1) [AllOnes=1]
7320//
7321// Invert is set when N is the null/all ones constant when CC is false.
7322// OtherOp is set to the alternative value of N.
7323static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7324 SDValue &CC, bool &Invert,
7325 SDValue &OtherOp,
7326 SelectionDAG &DAG) {
7327 switch (N->getOpcode()) {
7328 default: return false;
7329 case ISD::SELECT: {
7330 CC = N->getOperand(0);
7331 SDValue N1 = N->getOperand(1);
7332 SDValue N2 = N->getOperand(2);
7333 if (isZeroOrAllOnes(N1, AllOnes)) {
7334 Invert = false;
7335 OtherOp = N2;
7336 return true;
7337 }
7338 if (isZeroOrAllOnes(N2, AllOnes)) {
7339 Invert = true;
7340 OtherOp = N1;
7341 return true;
7342 }
7343 return false;
7344 }
7345 case ISD::ZERO_EXTEND:
7346 // (zext cc) can never be the all ones value.
7347 if (AllOnes)
7348 return false;
7349 // Fall through.
7350 case ISD::SIGN_EXTEND: {
7351 EVT VT = N->getValueType(0);
7352 CC = N->getOperand(0);
7353 if (CC.getValueType() != MVT::i1)
7354 return false;
7355 Invert = !AllOnes;
7356 if (AllOnes)
7357 // When looking for an AllOnes constant, N is an sext, and the 'other'
7358 // value is 0.
7359 OtherOp = DAG.getConstant(0, VT);
7360 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7361 // When looking for a 0 constant, N can be zext or sext.
7362 OtherOp = DAG.getConstant(1, VT);
7363 else
7364 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7365 return true;
7366 }
7367 }
7368}
7369
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007370// Combine a constant select operand into its use:
7371//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007372// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7373// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7374// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7375// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7376// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007377//
7378// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007379// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007380//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007381// Also recognize sext/zext from i1:
7382//
7383// (add (zext cc), x) -> (select cc (add x, 1), x)
7384// (add (sext cc), x) -> (select cc (add x, -1), x)
7385//
7386// These transformations eventually create predicated instructions.
7387//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007388// @param N The node to transform.
7389// @param Slct The N operand that is a select.
7390// @param OtherOp The other N operand (x above).
7391// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007392// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007393// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00007394static
7395SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007396 TargetLowering::DAGCombinerInfo &DCI,
7397 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00007398 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00007399 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007400 SDValue NonConstantVal;
7401 SDValue CCOp;
7402 bool SwapSelectOps;
7403 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7404 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007405 return SDValue();
7406
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007407 // Slct is now know to be the desired identity constant when CC is true.
7408 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007409 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007410 OtherOp, NonConstantVal);
7411 // Unless SwapSelectOps says CC should be false.
7412 if (SwapSelectOps)
7413 std::swap(TrueVal, FalseVal);
7414
Andrew Trickef9de2a2013-05-25 02:42:55 +00007415 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007416 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00007417}
7418
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007419// Attempt combineSelectAndUse on each operand of a commutative operator N.
7420static
7421SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7422 TargetLowering::DAGCombinerInfo &DCI) {
7423 SDValue N0 = N->getOperand(0);
7424 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007425 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007426 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7427 if (Result.getNode())
7428 return Result;
7429 }
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007430 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007431 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7432 if (Result.getNode())
7433 return Result;
7434 }
7435 return SDValue();
7436}
7437
Eric Christopher1b8b94192011-06-29 21:10:36 +00007438// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00007439// (only after legalization).
7440static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7441 TargetLowering::DAGCombinerInfo &DCI,
7442 const ARMSubtarget *Subtarget) {
7443
7444 // Only perform optimization if after legalize, and if NEON is available. We
7445 // also expected both operands to be BUILD_VECTORs.
7446 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7447 || N0.getOpcode() != ISD::BUILD_VECTOR
7448 || N1.getOpcode() != ISD::BUILD_VECTOR)
7449 return SDValue();
7450
7451 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7452 EVT VT = N->getValueType(0);
7453 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7454 return SDValue();
7455
7456 // Check that the vector operands are of the right form.
7457 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7458 // operands, where N is the size of the formed vector.
7459 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7460 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007461
7462 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00007463 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00007464 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00007465 SDValue Vec = N0->getOperand(0)->getOperand(0);
7466 SDNode *V = Vec.getNode();
7467 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00007468
Eric Christopher1b8b94192011-06-29 21:10:36 +00007469 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007470 // check to see if each of their operands are an EXTRACT_VECTOR with
7471 // the same vector and appropriate index.
7472 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7473 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7474 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00007475
Tanya Lattnere9e67052011-06-14 23:48:48 +00007476 SDValue ExtVec0 = N0->getOperand(i);
7477 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007478
Tanya Lattnere9e67052011-06-14 23:48:48 +00007479 // First operand is the vector, verify its the same.
7480 if (V != ExtVec0->getOperand(0).getNode() ||
7481 V != ExtVec1->getOperand(0).getNode())
7482 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00007483
Tanya Lattnere9e67052011-06-14 23:48:48 +00007484 // Second is the constant, verify its correct.
7485 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7486 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00007487
Tanya Lattnere9e67052011-06-14 23:48:48 +00007488 // For the constant, we want to see all the even or all the odd.
7489 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7490 || C1->getZExtValue() != nextIndex+1)
7491 return SDValue();
7492
7493 // Increment index.
7494 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00007495 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00007496 return SDValue();
7497 }
7498
7499 // Create VPADDL node.
7500 SelectionDAG &DAG = DCI.DAG;
7501 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00007502
7503 // Build operand list.
7504 SmallVector<SDValue, 8> Ops;
7505 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7506 TLI.getPointerTy()));
7507
7508 // Input is the vector.
7509 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007510
Tanya Lattnere9e67052011-06-14 23:48:48 +00007511 // Get widened type and narrowed type.
7512 MVT widenType;
7513 unsigned numElem = VT.getVectorNumElements();
Silviu Barangaa3106e62014-04-03 10:44:27 +00007514
7515 EVT inputLaneType = Vec.getValueType().getVectorElementType();
7516 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00007517 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7518 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7519 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7520 default:
Craig Toppere55c5562012-02-07 02:50:20 +00007521 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00007522 }
7523
Andrew Trickef9de2a2013-05-25 02:42:55 +00007524 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
Tanya Lattnere9e67052011-06-14 23:48:48 +00007525 widenType, &Ops[0], Ops.size());
Silviu Barangaa3106e62014-04-03 10:44:27 +00007526 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
7527 return DAG.getNode(ExtOp, SDLoc(N), VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00007528}
7529
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007530static SDValue findMUL_LOHI(SDValue V) {
7531 if (V->getOpcode() == ISD::UMUL_LOHI ||
7532 V->getOpcode() == ISD::SMUL_LOHI)
7533 return V;
7534 return SDValue();
7535}
7536
7537static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7538 TargetLowering::DAGCombinerInfo &DCI,
7539 const ARMSubtarget *Subtarget) {
7540
7541 if (Subtarget->isThumb1Only()) return SDValue();
7542
7543 // Only perform the checks after legalize when the pattern is available.
7544 if (DCI.isBeforeLegalize()) return SDValue();
7545
7546 // Look for multiply add opportunities.
7547 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7548 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7549 // a glue link from the first add to the second add.
7550 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
7551 // a S/UMLAL instruction.
7552 // loAdd UMUL_LOHI
7553 // \ / :lo \ :hi
7554 // \ / \ [no multiline comment]
7555 // ADDC | hiAdd
7556 // \ :glue / /
7557 // \ / /
7558 // ADDE
7559 //
7560 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
7561 SDValue AddcOp0 = AddcNode->getOperand(0);
7562 SDValue AddcOp1 = AddcNode->getOperand(1);
7563
7564 // Check if the two operands are from the same mul_lohi node.
7565 if (AddcOp0.getNode() == AddcOp1.getNode())
7566 return SDValue();
7567
7568 assert(AddcNode->getNumValues() == 2 &&
7569 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00007570 "Expect ADDC with two result values. First: i32");
7571
7572 // Check that we have a glued ADDC node.
7573 if (AddcNode->getValueType(1) != MVT::Glue)
7574 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007575
7576 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
7577 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
7578 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
7579 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
7580 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
7581 return SDValue();
7582
7583 // Look for the glued ADDE.
7584 SDNode* AddeNode = AddcNode->getGluedUser();
7585 if (AddeNode == NULL)
7586 return SDValue();
7587
7588 // Make sure it is really an ADDE.
7589 if (AddeNode->getOpcode() != ISD::ADDE)
7590 return SDValue();
7591
7592 assert(AddeNode->getNumOperands() == 3 &&
7593 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
7594 "ADDE node has the wrong inputs");
7595
7596 // Check for the triangle shape.
7597 SDValue AddeOp0 = AddeNode->getOperand(0);
7598 SDValue AddeOp1 = AddeNode->getOperand(1);
7599
7600 // Make sure that the ADDE operands are not coming from the same node.
7601 if (AddeOp0.getNode() == AddeOp1.getNode())
7602 return SDValue();
7603
7604 // Find the MUL_LOHI node walking up ADDE's operands.
7605 bool IsLeftOperandMUL = false;
7606 SDValue MULOp = findMUL_LOHI(AddeOp0);
7607 if (MULOp == SDValue())
7608 MULOp = findMUL_LOHI(AddeOp1);
7609 else
7610 IsLeftOperandMUL = true;
7611 if (MULOp == SDValue())
7612 return SDValue();
7613
7614 // Figure out the right opcode.
7615 unsigned Opc = MULOp->getOpcode();
7616 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
7617
7618 // Figure out the high and low input values to the MLAL node.
7619 SDValue* HiMul = &MULOp;
7620 SDValue* HiAdd = NULL;
7621 SDValue* LoMul = NULL;
7622 SDValue* LowAdd = NULL;
7623
7624 if (IsLeftOperandMUL)
7625 HiAdd = &AddeOp1;
7626 else
7627 HiAdd = &AddeOp0;
7628
7629
7630 if (AddcOp0->getOpcode() == Opc) {
7631 LoMul = &AddcOp0;
7632 LowAdd = &AddcOp1;
7633 }
7634 if (AddcOp1->getOpcode() == Opc) {
7635 LoMul = &AddcOp1;
7636 LowAdd = &AddcOp0;
7637 }
7638
7639 if (LoMul == NULL)
7640 return SDValue();
7641
7642 if (LoMul->getNode() != HiMul->getNode())
7643 return SDValue();
7644
7645 // Create the merged node.
7646 SelectionDAG &DAG = DCI.DAG;
7647
7648 // Build operand list.
7649 SmallVector<SDValue, 8> Ops;
7650 Ops.push_back(LoMul->getOperand(0));
7651 Ops.push_back(LoMul->getOperand(1));
7652 Ops.push_back(*LowAdd);
7653 Ops.push_back(*HiAdd);
7654
Andrew Trickef9de2a2013-05-25 02:42:55 +00007655 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007656 DAG.getVTList(MVT::i32, MVT::i32),
7657 &Ops[0], Ops.size());
7658
7659 // Replace the ADDs' nodes uses by the MLA node's values.
7660 SDValue HiMLALResult(MLALNode.getNode(), 1);
7661 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
7662
7663 SDValue LoMLALResult(MLALNode.getNode(), 0);
7664 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
7665
7666 // Return original node to notify the driver to stop replacing.
7667 SDValue resNode(AddcNode, 0);
7668 return resNode;
7669}
7670
7671/// PerformADDCCombine - Target-specific dag combine transform from
7672/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
7673static SDValue PerformADDCCombine(SDNode *N,
7674 TargetLowering::DAGCombinerInfo &DCI,
7675 const ARMSubtarget *Subtarget) {
7676
7677 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
7678
7679}
7680
Bob Wilson728eb292010-07-29 20:34:14 +00007681/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7682/// operands N0 and N1. This is a helper for PerformADDCombine that is
7683/// called with the default operands, and if that fails, with commuted
7684/// operands.
7685static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007686 TargetLowering::DAGCombinerInfo &DCI,
7687 const ARMSubtarget *Subtarget){
7688
7689 // Attempt to create vpaddl for this add.
7690 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7691 if (Result.getNode())
7692 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00007693
Chris Lattner4147f082009-03-12 06:52:53 +00007694 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007695 if (N0.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00007696 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7697 if (Result.getNode()) return Result;
7698 }
Chris Lattner4147f082009-03-12 06:52:53 +00007699 return SDValue();
7700}
7701
Bob Wilson728eb292010-07-29 20:34:14 +00007702/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7703///
7704static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007705 TargetLowering::DAGCombinerInfo &DCI,
7706 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00007707 SDValue N0 = N->getOperand(0);
7708 SDValue N1 = N->getOperand(1);
7709
7710 // First try with the default operand order.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007711 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00007712 if (Result.getNode())
7713 return Result;
7714
7715 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007716 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00007717}
7718
Chris Lattner4147f082009-03-12 06:52:53 +00007719/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00007720///
Chris Lattner4147f082009-03-12 06:52:53 +00007721static SDValue PerformSUBCombine(SDNode *N,
7722 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00007723 SDValue N0 = N->getOperand(0);
7724 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00007725
Chris Lattner4147f082009-03-12 06:52:53 +00007726 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007727 if (N1.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00007728 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7729 if (Result.getNode()) return Result;
7730 }
Bob Wilson7117a912009-03-20 22:42:55 +00007731
Chris Lattner4147f082009-03-12 06:52:53 +00007732 return SDValue();
7733}
7734
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007735/// PerformVMULCombine
7736/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7737/// special multiplier accumulator forwarding.
7738/// vmul d3, d0, d2
7739/// vmla d3, d1, d2
7740/// is faster than
7741/// vadd d3, d0, d1
7742/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00007743// However, for (A + B) * (A + B),
7744// vadd d2, d0, d1
7745// vmul d3, d0, d2
7746// vmla d3, d1, d2
7747// is slower than
7748// vadd d2, d0, d1
7749// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007750static SDValue PerformVMULCombine(SDNode *N,
7751 TargetLowering::DAGCombinerInfo &DCI,
7752 const ARMSubtarget *Subtarget) {
7753 if (!Subtarget->hasVMLxForwarding())
7754 return SDValue();
7755
7756 SelectionDAG &DAG = DCI.DAG;
7757 SDValue N0 = N->getOperand(0);
7758 SDValue N1 = N->getOperand(1);
7759 unsigned Opcode = N0.getOpcode();
7760 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7761 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00007762 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007763 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7764 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7765 return SDValue();
7766 std::swap(N0, N1);
7767 }
7768
Weiming Zhao2052f482013-09-25 23:12:06 +00007769 if (N0 == N1)
7770 return SDValue();
7771
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007772 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00007773 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007774 SDValue N00 = N0->getOperand(0);
7775 SDValue N01 = N0->getOperand(1);
7776 return DAG.getNode(Opcode, DL, VT,
7777 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7778 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7779}
7780
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007781static SDValue PerformMULCombine(SDNode *N,
7782 TargetLowering::DAGCombinerInfo &DCI,
7783 const ARMSubtarget *Subtarget) {
7784 SelectionDAG &DAG = DCI.DAG;
7785
7786 if (Subtarget->isThumb1Only())
7787 return SDValue();
7788
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007789 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7790 return SDValue();
7791
7792 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007793 if (VT.is64BitVector() || VT.is128BitVector())
7794 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007795 if (VT != MVT::i32)
7796 return SDValue();
7797
7798 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
7799 if (!C)
7800 return SDValue();
7801
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00007802 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00007803 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00007804
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007805 ShiftAmt = ShiftAmt & (32 - 1);
7806 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00007807 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007808
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00007809 SDValue Res;
7810 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00007811
7812 if (MulAmt >= 0) {
7813 if (isPowerOf2_32(MulAmt - 1)) {
7814 // (mul x, 2^N + 1) => (add (shl x, N), x)
7815 Res = DAG.getNode(ISD::ADD, DL, VT,
7816 V,
7817 DAG.getNode(ISD::SHL, DL, VT,
7818 V,
7819 DAG.getConstant(Log2_32(MulAmt - 1),
7820 MVT::i32)));
7821 } else if (isPowerOf2_32(MulAmt + 1)) {
7822 // (mul x, 2^N - 1) => (sub (shl x, N), x)
7823 Res = DAG.getNode(ISD::SUB, DL, VT,
7824 DAG.getNode(ISD::SHL, DL, VT,
7825 V,
7826 DAG.getConstant(Log2_32(MulAmt + 1),
7827 MVT::i32)),
7828 V);
7829 } else
7830 return SDValue();
7831 } else {
7832 uint64_t MulAmtAbs = -MulAmt;
7833 if (isPowerOf2_32(MulAmtAbs + 1)) {
7834 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
7835 Res = DAG.getNode(ISD::SUB, DL, VT,
7836 V,
7837 DAG.getNode(ISD::SHL, DL, VT,
7838 V,
7839 DAG.getConstant(Log2_32(MulAmtAbs + 1),
7840 MVT::i32)));
7841 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
7842 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
7843 Res = DAG.getNode(ISD::ADD, DL, VT,
7844 V,
7845 DAG.getNode(ISD::SHL, DL, VT,
7846 V,
7847 DAG.getConstant(Log2_32(MulAmtAbs-1),
7848 MVT::i32)));
7849 Res = DAG.getNode(ISD::SUB, DL, VT,
7850 DAG.getConstant(0, MVT::i32),Res);
7851
7852 } else
7853 return SDValue();
7854 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00007855
7856 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00007857 Res = DAG.getNode(ISD::SHL, DL, VT,
7858 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007859
7860 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00007861 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007862 return SDValue();
7863}
7864
Owen Anderson30c48922010-11-05 19:27:46 +00007865static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00007866 TargetLowering::DAGCombinerInfo &DCI,
7867 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00007868
Owen Anderson30c48922010-11-05 19:27:46 +00007869 // Attempt to use immediate-form VBIC
7870 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00007871 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00007872 EVT VT = N->getValueType(0);
7873 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00007874
Tanya Lattner266792a2011-04-07 15:24:20 +00007875 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7876 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00007877
Owen Anderson30c48922010-11-05 19:27:46 +00007878 APInt SplatBits, SplatUndef;
7879 unsigned SplatBitSize;
7880 bool HasAnyUndefs;
7881 if (BVN &&
7882 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7883 if (SplatBitSize <= 64) {
7884 EVT VbicVT;
7885 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
7886 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00007887 DAG, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00007888 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00007889 if (Val.getNode()) {
7890 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00007891 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00007892 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00007893 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00007894 }
7895 }
7896 }
Wesley Peck527da1b2010-11-23 03:31:01 +00007897
Evan Chenge87681c2012-02-23 01:19:06 +00007898 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007899 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
7900 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
7901 if (Result.getNode())
7902 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00007903 }
7904
Owen Anderson30c48922010-11-05 19:27:46 +00007905 return SDValue();
7906}
7907
Jim Grosbach11013ed2010-07-16 23:05:05 +00007908/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
7909static SDValue PerformORCombine(SDNode *N,
7910 TargetLowering::DAGCombinerInfo &DCI,
7911 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00007912 // Attempt to use immediate-form VORR
7913 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00007914 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00007915 EVT VT = N->getValueType(0);
7916 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00007917
Tanya Lattner266792a2011-04-07 15:24:20 +00007918 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7919 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00007920
Owen Andersonbc9b31c2010-11-03 23:15:26 +00007921 APInt SplatBits, SplatUndef;
7922 unsigned SplatBitSize;
7923 bool HasAnyUndefs;
7924 if (BVN && Subtarget->hasNEON() &&
7925 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7926 if (SplatBitSize <= 64) {
7927 EVT VorrVT;
7928 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
7929 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00007930 DAG, VorrVT, VT.is128BitVector(),
7931 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00007932 if (Val.getNode()) {
7933 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00007934 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00007935 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00007936 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00007937 }
7938 }
7939 }
7940
Evan Chenge87681c2012-02-23 01:19:06 +00007941 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007942 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7943 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
7944 if (Result.getNode())
7945 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00007946 }
7947
Nadav Rotem3a94c542012-08-13 18:52:44 +00007948 // The code below optimizes (or (and X, Y), Z).
7949 // The AND operand needs to have a single user to make these optimizations
7950 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00007951 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00007952 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00007953 return SDValue();
7954 SDValue N1 = N->getOperand(1);
7955
7956 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
7957 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
7958 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
7959 APInt SplatUndef;
7960 unsigned SplatBitSize;
7961 bool HasAnyUndefs;
7962
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00007963 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00007964 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00007965 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
7966 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00007967 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00007968 HasAnyUndefs) && !HasAnyUndefs) {
7969 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
7970 HasAnyUndefs) && !HasAnyUndefs) {
7971 // Ensure that the bit width of the constants are the same and that
7972 // the splat arguments are logical inverses as per the pattern we
7973 // are trying to simplify.
7974 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
7975 SplatBits0 == ~SplatBits1) {
7976 // Canonicalize the vector type to make instruction selection
7977 // simpler.
7978 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
7979 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
7980 N0->getOperand(1),
7981 N0->getOperand(0),
7982 N1->getOperand(0));
7983 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
7984 }
7985 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00007986 }
7987 }
7988
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00007989 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
7990 // reasonable.
7991
Jim Grosbach11013ed2010-07-16 23:05:05 +00007992 // BFI is only available on V6T2+
7993 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
7994 return SDValue();
7995
Andrew Trickef9de2a2013-05-25 02:42:55 +00007996 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00007997 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00007998 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00007999 //
8000 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008001 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008002 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008003 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008004 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008005 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008006
Jim Grosbach11013ed2010-07-16 23:05:05 +00008007 if (VT != MVT::i32)
8008 return SDValue();
8009
Evan Cheng2e51bb42010-12-13 20:32:54 +00008010 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008011
Jim Grosbach11013ed2010-07-16 23:05:05 +00008012 // The value and the mask need to be constants so we can verify this is
8013 // actually a bitfield set. If the mask is 0xffff, we can do better
8014 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00008015 SDValue MaskOp = N0.getOperand(1);
8016 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8017 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008018 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008019 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008020 if (Mask == 0xffff)
8021 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008022 SDValue Res;
8023 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008024 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8025 if (N1C) {
8026 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00008027 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008028 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008029
Evan Cheng34345752010-12-11 04:11:38 +00008030 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008031 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00008032
Evan Cheng2e51bb42010-12-13 20:32:54 +00008033 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Cheng34345752010-12-11 04:11:38 +00008034 DAG.getConstant(Val, MVT::i32),
8035 DAG.getConstant(Mask, MVT::i32));
8036
8037 // Do not add new nodes to DAG combiner worklist.
8038 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008039 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00008040 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008041 } else if (N1.getOpcode() == ISD::AND) {
8042 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008043 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8044 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008045 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008046 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008047
Eric Christopherd5530962011-03-26 01:21:03 +00008048 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8049 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008050 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008051 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008052 // The pack halfword instruction works better for masks that fit it,
8053 // so use that when it's available.
8054 if (Subtarget->hasT2ExtractPack() &&
8055 (Mask == 0xffff || Mask == 0xffff0000))
8056 return SDValue();
8057 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008058 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008059 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopherd5530962011-03-26 01:21:03 +00008060 DAG.getConstant(amt, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008061 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008062 DAG.getConstant(Mask, MVT::i32));
8063 // Do not add new nodes to DAG combiner worklist.
8064 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008065 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008066 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008067 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008068 // The pack halfword instruction works better for masks that fit it,
8069 // so use that when it's available.
8070 if (Subtarget->hasT2ExtractPack() &&
8071 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8072 return SDValue();
8073 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008074 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008075 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008076 DAG.getConstant(lsb, MVT::i32));
8077 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopherd5530962011-03-26 01:21:03 +00008078 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008079 // Do not add new nodes to DAG combiner worklist.
8080 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008081 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008082 }
8083 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008084
Evan Cheng2e51bb42010-12-13 20:32:54 +00008085 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8086 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8087 ARM::isBitFieldInvertedMask(~Mask)) {
8088 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8089 // where lsb(mask) == #shamt and masked bits of B are known zero.
8090 SDValue ShAmt = N00.getOperand(1);
8091 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008092 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008093 if (ShAmtC != LSB)
8094 return SDValue();
8095
8096 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
8097 DAG.getConstant(~Mask, MVT::i32));
8098
8099 // Do not add new nodes to DAG combiner worklist.
8100 DCI.CombineTo(N, Res, false);
8101 }
8102
Jim Grosbach11013ed2010-07-16 23:05:05 +00008103 return SDValue();
8104}
8105
Evan Chenge87681c2012-02-23 01:19:06 +00008106static SDValue PerformXORCombine(SDNode *N,
8107 TargetLowering::DAGCombinerInfo &DCI,
8108 const ARMSubtarget *Subtarget) {
8109 EVT VT = N->getValueType(0);
8110 SelectionDAG &DAG = DCI.DAG;
8111
8112 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8113 return SDValue();
8114
8115 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008116 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8117 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8118 if (Result.getNode())
8119 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008120 }
8121
8122 return SDValue();
8123}
8124
Evan Cheng6d02d902011-06-15 01:12:31 +00008125/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8126/// the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00008127static SDValue PerformBFICombine(SDNode *N,
8128 TargetLowering::DAGCombinerInfo &DCI) {
8129 SDValue N1 = N->getOperand(1);
8130 if (N1.getOpcode() == ISD::AND) {
8131 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8132 if (!N11C)
8133 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008134 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008135 unsigned LSB = countTrailingZeros(~InvMask);
8136 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Evan Cheng6d02d902011-06-15 01:12:31 +00008137 unsigned Mask = (1 << Width)-1;
Evan Chengc1778132010-12-14 03:22:07 +00008138 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008139 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008140 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00008141 N->getOperand(0), N1.getOperand(0),
8142 N->getOperand(2));
8143 }
8144 return SDValue();
8145}
8146
Bob Wilson22806742010-09-22 22:09:21 +00008147/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8148/// ARMISD::VMOVRRD.
8149static SDValue PerformVMOVRRDCombine(SDNode *N,
8150 TargetLowering::DAGCombinerInfo &DCI) {
8151 // vmovrrd(vmovdrr x, y) -> x,y
8152 SDValue InDouble = N->getOperand(0);
8153 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
8154 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008155
8156 // vmovrrd(load f64) -> (load i32), (load i32)
8157 SDNode *InNode = InDouble.getNode();
8158 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8159 InNode->getValueType(0) == MVT::f64 &&
8160 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8161 !cast<LoadSDNode>(InNode)->isVolatile()) {
8162 // TODO: Should this be done for non-FrameIndex operands?
8163 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8164
8165 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008166 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008167 SDValue BasePtr = LD->getBasePtr();
8168 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8169 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008170 LD->isNonTemporal(), LD->isInvariant(),
8171 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008172
8173 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8174 DAG.getConstant(4, MVT::i32));
8175 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8176 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008177 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008178 std::min(4U, LD->getAlignment() / 2));
8179
8180 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
8181 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
8182 DCI.RemoveFromWorklist(LD);
8183 DAG.DeleteNode(LD);
8184 return Result;
8185 }
8186
Bob Wilson22806742010-09-22 22:09:21 +00008187 return SDValue();
8188}
8189
8190/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8191/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8192static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8193 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8194 SDValue Op0 = N->getOperand(0);
8195 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00008196 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008197 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008198 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008199 Op1 = Op1.getOperand(0);
8200 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8201 Op0.getNode() == Op1.getNode() &&
8202 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008203 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00008204 N->getValueType(0), Op0.getOperand(0));
8205 return SDValue();
8206}
8207
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008208/// PerformSTORECombine - Target-specific dag combine xforms for
8209/// ISD::STORE.
8210static SDValue PerformSTORECombine(SDNode *N,
8211 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008212 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008213 if (St->isVolatile())
8214 return SDValue();
8215
Andrew Trickbc325162012-07-18 18:34:24 +00008216 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosiere0e38f62012-04-09 20:32:02 +00008217 // pack all of the elements in one place. Next, store to memory in fewer
8218 // chunks.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008219 SDValue StVal = St->getValue();
Chad Rosiere0e38f62012-04-09 20:32:02 +00008220 EVT VT = StVal.getValueType();
8221 if (St->isTruncatingStore() && VT.isVector()) {
8222 SelectionDAG &DAG = DCI.DAG;
8223 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8224 EVT StVT = St->getMemoryVT();
8225 unsigned NumElems = VT.getVectorNumElements();
8226 assert(StVT != VT && "Cannot truncate to the same type");
8227 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
8228 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
8229
8230 // From, To sizes and ElemCount must be pow of two
8231 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
8232
8233 // We are going to use the original vector elt for storing.
8234 // Accumulated smaller vector elements must be a multiple of the store size.
8235 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
8236
8237 unsigned SizeRatio = FromEltSz / ToEltSz;
8238 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
8239
8240 // Create a type on which we perform the shuffle.
8241 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
8242 NumElems*SizeRatio);
8243 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
8244
Andrew Trickef9de2a2013-05-25 02:42:55 +00008245 SDLoc DL(St);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008246 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
8247 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
8248 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
8249
8250 // Can't shuffle using an illegal type.
8251 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
8252
8253 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
8254 DAG.getUNDEF(WideVec.getValueType()),
8255 ShuffleVec.data());
8256 // At this point all of the data is stored at the bottom of the
8257 // register. We now need to save it to mem.
8258
8259 // Find the largest store unit
8260 MVT StoreType = MVT::i8;
8261 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
8262 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
8263 MVT Tp = (MVT::SimpleValueType)tp;
8264 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
8265 StoreType = Tp;
8266 }
8267 // Didn't find a legal store type.
8268 if (!TLI.isTypeLegal(StoreType))
8269 return SDValue();
8270
8271 // Bitcast the original vector into a vector of store-size units
8272 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
8273 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
8274 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
8275 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
8276 SmallVector<SDValue, 8> Chains;
8277 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
8278 TLI.getPointerTy());
8279 SDValue BasePtr = St->getBasePtr();
8280
8281 // Perform one or more big stores into memory.
8282 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
8283 for (unsigned I = 0; I < E; I++) {
8284 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
8285 StoreType, ShuffWide,
8286 DAG.getIntPtrConstant(I));
8287 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
8288 St->getPointerInfo(), St->isVolatile(),
8289 St->isNonTemporal(), St->getAlignment());
8290 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
8291 Increment);
8292 Chains.push_back(Ch);
8293 }
8294 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0],
8295 Chains.size());
8296 }
8297
8298 if (!ISD::isNormalStore(St))
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008299 return SDValue();
8300
Chad Rosier99cbde92012-04-09 19:38:15 +00008301 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
8302 // ARM stores of arguments in the same cache line.
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008303 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier99cbde92012-04-09 19:38:15 +00008304 StVal.getNode()->hasOneUse()) {
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008305 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008306 SDLoc DL(St);
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008307 SDValue BasePtr = St->getBasePtr();
8308 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
8309 StVal.getNode()->getOperand(0), BasePtr,
8310 St->getPointerInfo(), St->isVolatile(),
8311 St->isNonTemporal(), St->getAlignment());
8312
8313 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8314 DAG.getConstant(4, MVT::i32));
8315 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
8316 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
8317 St->isNonTemporal(),
8318 std::min(4U, St->getAlignment() / 2));
8319 }
8320
8321 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008322 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8323 return SDValue();
8324
Chad Rosier99cbde92012-04-09 19:38:15 +00008325 // Bitcast an i64 store extracted from a vector to f64.
8326 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008327 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008328 SDLoc dl(StVal);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008329 SDValue IntVec = StVal.getOperand(0);
8330 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8331 IntVec.getValueType().getVectorNumElements());
8332 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
8333 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
8334 Vec, StVal.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008335 dl = SDLoc(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008336 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
8337 // Make the DAGCombiner fold the bitcasts.
8338 DCI.AddToWorklist(Vec.getNode());
8339 DCI.AddToWorklist(ExtElt.getNode());
8340 DCI.AddToWorklist(V.getNode());
8341 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
8342 St->getPointerInfo(), St->isVolatile(),
8343 St->isNonTemporal(), St->getAlignment(),
8344 St->getTBAAInfo());
8345}
8346
8347/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8348/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8349/// i64 vector to have f64 elements, since the value can then be loaded
8350/// directly into a VFP register.
8351static bool hasNormalLoadOperand(SDNode *N) {
8352 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8353 for (unsigned i = 0; i < NumElts; ++i) {
8354 SDNode *Elt = N->getOperand(i).getNode();
8355 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8356 return true;
8357 }
8358 return false;
8359}
8360
Bob Wilsoncb6db982010-09-17 22:59:05 +00008361/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8362/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008363static SDValue PerformBUILD_VECTORCombine(SDNode *N,
8364 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilsoncb6db982010-09-17 22:59:05 +00008365 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8366 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8367 // into a pair of GPRs, which is fine when the value is used as a scalar,
8368 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008369 SelectionDAG &DAG = DCI.DAG;
8370 if (N->getNumOperands() == 2) {
8371 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8372 if (RV.getNode())
8373 return RV;
8374 }
Bob Wilsoncb6db982010-09-17 22:59:05 +00008375
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008376 // Load i64 elements as f64 values so that type legalization does not split
8377 // them up into i32 values.
8378 EVT VT = N->getValueType(0);
8379 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8380 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008381 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008382 SmallVector<SDValue, 8> Ops;
8383 unsigned NumElts = VT.getVectorNumElements();
8384 for (unsigned i = 0; i < NumElts; ++i) {
8385 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8386 Ops.push_back(V);
8387 // Make the DAGCombiner fold the bitcast.
8388 DCI.AddToWorklist(V.getNode());
8389 }
8390 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
8391 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
8392 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8393}
8394
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008395/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
8396static SDValue
8397PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8398 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
8399 // At that time, we may have inserted bitcasts from integer to float.
8400 // If these bitcasts have survived DAGCombine, change the lowering of this
8401 // BUILD_VECTOR in something more vector friendly, i.e., that does not
8402 // force to use floating point types.
8403
8404 // Make sure we can change the type of the vector.
8405 // This is possible iff:
8406 // 1. The vector is only used in a bitcast to a integer type. I.e.,
8407 // 1.1. Vector is used only once.
8408 // 1.2. Use is a bit convert to an integer type.
8409 // 2. The size of its operands are 32-bits (64-bits are not legal).
8410 EVT VT = N->getValueType(0);
8411 EVT EltVT = VT.getVectorElementType();
8412
8413 // Check 1.1. and 2.
8414 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
8415 return SDValue();
8416
8417 // By construction, the input type must be float.
8418 assert(EltVT == MVT::f32 && "Unexpected type!");
8419
8420 // Check 1.2.
8421 SDNode *Use = *N->use_begin();
8422 if (Use->getOpcode() != ISD::BITCAST ||
8423 Use->getValueType(0).isFloatingPoint())
8424 return SDValue();
8425
8426 // Check profitability.
8427 // Model is, if more than half of the relevant operands are bitcast from
8428 // i32, turn the build_vector into a sequence of insert_vector_elt.
8429 // Relevant operands are everything that is not statically
8430 // (i.e., at compile time) bitcasted.
8431 unsigned NumOfBitCastedElts = 0;
8432 unsigned NumElts = VT.getVectorNumElements();
8433 unsigned NumOfRelevantElts = NumElts;
8434 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
8435 SDValue Elt = N->getOperand(Idx);
8436 if (Elt->getOpcode() == ISD::BITCAST) {
8437 // Assume only bit cast to i32 will go away.
8438 if (Elt->getOperand(0).getValueType() == MVT::i32)
8439 ++NumOfBitCastedElts;
8440 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt))
8441 // Constants are statically casted, thus do not count them as
8442 // relevant operands.
8443 --NumOfRelevantElts;
8444 }
8445
8446 // Check if more than half of the elements require a non-free bitcast.
8447 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
8448 return SDValue();
8449
8450 SelectionDAG &DAG = DCI.DAG;
8451 // Create the new vector type.
8452 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
8453 // Check if the type is legal.
8454 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8455 if (!TLI.isTypeLegal(VecVT))
8456 return SDValue();
8457
8458 // Combine:
8459 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
8460 // => BITCAST INSERT_VECTOR_ELT
8461 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
8462 // (BITCAST EN), N.
8463 SDValue Vec = DAG.getUNDEF(VecVT);
8464 SDLoc dl(N);
8465 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
8466 SDValue V = N->getOperand(Idx);
8467 if (V.getOpcode() == ISD::UNDEF)
8468 continue;
8469 if (V.getOpcode() == ISD::BITCAST &&
8470 V->getOperand(0).getValueType() == MVT::i32)
8471 // Fold obvious case.
8472 V = V.getOperand(0);
8473 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00008474 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008475 // Make the DAGCombiner fold the bitcasts.
8476 DCI.AddToWorklist(V.getNode());
8477 }
8478 SDValue LaneIdx = DAG.getConstant(Idx, MVT::i32);
8479 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
8480 }
8481 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
8482 // Make the DAGCombiner fold the bitcasts.
8483 DCI.AddToWorklist(Vec.getNode());
8484 return Vec;
8485}
8486
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008487/// PerformInsertEltCombine - Target-specific dag combine xforms for
8488/// ISD::INSERT_VECTOR_ELT.
8489static SDValue PerformInsertEltCombine(SDNode *N,
8490 TargetLowering::DAGCombinerInfo &DCI) {
8491 // Bitcast an i64 load inserted into a vector to f64.
8492 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8493 EVT VT = N->getValueType(0);
8494 SDNode *Elt = N->getOperand(1).getNode();
8495 if (VT.getVectorElementType() != MVT::i64 ||
8496 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8497 return SDValue();
8498
8499 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008500 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008501 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8502 VT.getVectorNumElements());
8503 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8504 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8505 // Make the DAGCombiner fold the bitcasts.
8506 DCI.AddToWorklist(Vec.getNode());
8507 DCI.AddToWorklist(V.getNode());
8508 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8509 Vec, V, N->getOperand(2));
8510 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00008511}
8512
Bob Wilsonc7334a12010-10-27 20:38:28 +00008513/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8514/// ISD::VECTOR_SHUFFLE.
8515static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8516 // The LLVM shufflevector instruction does not require the shuffle mask
8517 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8518 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8519 // operands do not match the mask length, they are extended by concatenating
8520 // them with undef vectors. That is probably the right thing for other
8521 // targets, but for NEON it is better to concatenate two double-register
8522 // size vector operands into a single quad-register size vector. Do that
8523 // transformation here:
8524 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8525 // shuffle(concat(v1, v2), undef)
8526 SDValue Op0 = N->getOperand(0);
8527 SDValue Op1 = N->getOperand(1);
8528 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8529 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8530 Op0.getNumOperands() != 2 ||
8531 Op1.getNumOperands() != 2)
8532 return SDValue();
8533 SDValue Concat0Op1 = Op0.getOperand(1);
8534 SDValue Concat1Op1 = Op1.getOperand(1);
8535 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8536 Concat1Op1.getOpcode() != ISD::UNDEF)
8537 return SDValue();
8538 // Skip the transformation if any of the types are illegal.
8539 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8540 EVT VT = N->getValueType(0);
8541 if (!TLI.isTypeLegal(VT) ||
8542 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8543 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8544 return SDValue();
8545
Andrew Trickef9de2a2013-05-25 02:42:55 +00008546 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008547 Op0.getOperand(0), Op1.getOperand(0));
8548 // Translate the shuffle mask.
8549 SmallVector<int, 16> NewMask;
8550 unsigned NumElts = VT.getVectorNumElements();
8551 unsigned HalfElts = NumElts/2;
8552 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8553 for (unsigned n = 0; n < NumElts; ++n) {
8554 int MaskElt = SVN->getMaskElt(n);
8555 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00008556 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00008557 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00008558 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00008559 NewElt = HalfElts + MaskElt - NumElts;
8560 NewMask.push_back(NewElt);
8561 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00008562 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008563 DAG.getUNDEF(VT), NewMask.data());
8564}
8565
Bob Wilson06fce872011-02-07 17:43:21 +00008566/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
8567/// NEON load/store intrinsics to merge base address updates.
8568static SDValue CombineBaseUpdate(SDNode *N,
8569 TargetLowering::DAGCombinerInfo &DCI) {
8570 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8571 return SDValue();
8572
8573 SelectionDAG &DAG = DCI.DAG;
8574 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8575 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
8576 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
8577 SDValue Addr = N->getOperand(AddrOpIdx);
8578
8579 // Search for a use of the address operand that is an increment.
8580 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8581 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8582 SDNode *User = *UI;
8583 if (User->getOpcode() != ISD::ADD ||
8584 UI.getUse().getResNo() != Addr.getResNo())
8585 continue;
8586
8587 // Check that the add is independent of the load/store. Otherwise, folding
8588 // it would create a cycle.
8589 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8590 continue;
8591
8592 // Find the new opcode for the updating load/store.
8593 bool isLoad = true;
8594 bool isLaneOp = false;
8595 unsigned NewOpc = 0;
8596 unsigned NumVecs = 0;
8597 if (isIntrinsic) {
8598 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8599 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00008600 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008601 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8602 NumVecs = 1; break;
8603 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8604 NumVecs = 2; break;
8605 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8606 NumVecs = 3; break;
8607 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8608 NumVecs = 4; break;
8609 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8610 NumVecs = 2; isLaneOp = true; break;
8611 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8612 NumVecs = 3; isLaneOp = true; break;
8613 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8614 NumVecs = 4; isLaneOp = true; break;
8615 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
8616 NumVecs = 1; isLoad = false; break;
8617 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
8618 NumVecs = 2; isLoad = false; break;
8619 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8620 NumVecs = 3; isLoad = false; break;
8621 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8622 NumVecs = 4; isLoad = false; break;
8623 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8624 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8625 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8626 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8627 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8628 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8629 }
8630 } else {
8631 isLaneOp = true;
8632 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00008633 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008634 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8635 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8636 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8637 }
8638 }
8639
8640 // Find the size of memory referenced by the load/store.
8641 EVT VecTy;
8642 if (isLoad)
8643 VecTy = N->getValueType(0);
Owen Anderson77aa2662011-04-05 21:48:57 +00008644 else
Bob Wilson06fce872011-02-07 17:43:21 +00008645 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8646 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8647 if (isLaneOp)
8648 NumBytes /= VecTy.getVectorNumElements();
8649
8650 // If the increment is a constant, it must match the memory ref size.
8651 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8652 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8653 uint64_t IncVal = CInc->getZExtValue();
8654 if (IncVal != NumBytes)
8655 continue;
8656 } else if (NumBytes >= 3 * 16) {
8657 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8658 // separate instructions that make it harder to use a non-constant update.
8659 continue;
8660 }
8661
8662 // Create the new updating load/store node.
8663 EVT Tys[6];
8664 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8665 unsigned n;
8666 for (n = 0; n < NumResultVecs; ++n)
8667 Tys[n] = VecTy;
8668 Tys[n++] = MVT::i32;
8669 Tys[n] = MVT::Other;
Craig Topperabb4ac72014-04-16 06:10:51 +00008670 SDVTList SDTys = DAG.getVTList(ArrayRef<EVT>(Tys, NumResultVecs+2));
Bob Wilson06fce872011-02-07 17:43:21 +00008671 SmallVector<SDValue, 8> Ops;
8672 Ops.push_back(N->getOperand(0)); // incoming chain
8673 Ops.push_back(N->getOperand(AddrOpIdx));
8674 Ops.push_back(Inc);
8675 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8676 Ops.push_back(N->getOperand(i));
8677 }
8678 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008679 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, SDLoc(N), SDTys,
Bob Wilson06fce872011-02-07 17:43:21 +00008680 Ops.data(), Ops.size(),
8681 MemInt->getMemoryVT(),
8682 MemInt->getMemOperand());
8683
8684 // Update the uses.
8685 std::vector<SDValue> NewResults;
8686 for (unsigned i = 0; i < NumResultVecs; ++i) {
8687 NewResults.push_back(SDValue(UpdN.getNode(), i));
8688 }
8689 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8690 DCI.CombineTo(N, NewResults);
8691 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8692
8693 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00008694 }
Bob Wilson06fce872011-02-07 17:43:21 +00008695 return SDValue();
8696}
8697
Bob Wilson2d790df2010-11-28 06:51:26 +00008698/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8699/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8700/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8701/// return true.
8702static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8703 SelectionDAG &DAG = DCI.DAG;
8704 EVT VT = N->getValueType(0);
8705 // vldN-dup instructions only support 64-bit vectors for N > 1.
8706 if (!VT.is64BitVector())
8707 return false;
8708
8709 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8710 SDNode *VLD = N->getOperand(0).getNode();
8711 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8712 return false;
8713 unsigned NumVecs = 0;
8714 unsigned NewOpc = 0;
8715 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8716 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8717 NumVecs = 2;
8718 NewOpc = ARMISD::VLD2DUP;
8719 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8720 NumVecs = 3;
8721 NewOpc = ARMISD::VLD3DUP;
8722 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8723 NumVecs = 4;
8724 NewOpc = ARMISD::VLD4DUP;
8725 } else {
8726 return false;
8727 }
8728
8729 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8730 // numbers match the load.
8731 unsigned VLDLaneNo =
8732 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8733 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8734 UI != UE; ++UI) {
8735 // Ignore uses of the chain result.
8736 if (UI.getUse().getResNo() == NumVecs)
8737 continue;
8738 SDNode *User = *UI;
8739 if (User->getOpcode() != ARMISD::VDUPLANE ||
8740 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8741 return false;
8742 }
8743
8744 // Create the vldN-dup node.
8745 EVT Tys[5];
8746 unsigned n;
8747 for (n = 0; n < NumVecs; ++n)
8748 Tys[n] = VT;
8749 Tys[n] = MVT::Other;
Craig Topperabb4ac72014-04-16 06:10:51 +00008750 SDVTList SDTys = DAG.getVTList(ArrayRef<EVT>(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00008751 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8752 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008753 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Bob Wilson2d790df2010-11-28 06:51:26 +00008754 Ops, 2, VLDMemInt->getMemoryVT(),
8755 VLDMemInt->getMemOperand());
8756
8757 // Update the uses.
8758 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8759 UI != UE; ++UI) {
8760 unsigned ResNo = UI.getUse().getResNo();
8761 // Ignore uses of the chain result.
8762 if (ResNo == NumVecs)
8763 continue;
8764 SDNode *User = *UI;
8765 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8766 }
8767
8768 // Now the vldN-lane intrinsic is dead except for its chain result.
8769 // Update uses of the chain.
8770 std::vector<SDValue> VLDDupResults;
8771 for (unsigned n = 0; n < NumVecs; ++n)
8772 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8773 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8774 DCI.CombineTo(VLD, VLDDupResults);
8775
8776 return true;
8777}
8778
Bob Wilson103a0dc2010-07-14 01:22:12 +00008779/// PerformVDUPLANECombine - Target-specific dag combine xforms for
8780/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00008781static SDValue PerformVDUPLANECombine(SDNode *N,
8782 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00008783 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00008784
Bob Wilson2d790df2010-11-28 06:51:26 +00008785 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
8786 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
8787 if (CombineVLDDUP(N, DCI))
8788 return SDValue(N, 0);
8789
8790 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
8791 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00008792 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00008793 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00008794 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00008795 return SDValue();
8796
8797 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
8798 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
8799 // The canonical VMOV for a zero vector uses a 32-bit element size.
8800 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8801 unsigned EltBits;
8802 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
8803 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00008804 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00008805 if (EltSize > VT.getVectorElementType().getSizeInBits())
8806 return SDValue();
8807
Andrew Trickef9de2a2013-05-25 02:42:55 +00008808 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00008809}
8810
Eric Christopher1b8b94192011-06-29 21:10:36 +00008811// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosierfa8d8932011-06-24 19:23:04 +00008812// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
8813static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
8814{
Chad Rosier6b610b32011-06-28 17:26:57 +00008815 integerPart cN;
8816 integerPart c0 = 0;
Chad Rosierfa8d8932011-06-24 19:23:04 +00008817 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
8818 I != E; I++) {
8819 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
8820 if (!C)
8821 return false;
8822
Eric Christopher1b8b94192011-06-29 21:10:36 +00008823 bool isExact;
Chad Rosierfa8d8932011-06-24 19:23:04 +00008824 APFloat APF = C->getValueAPF();
8825 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
8826 != APFloat::opOK || !isExact)
8827 return false;
8828
8829 c0 = (I == 0) ? cN : c0;
8830 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
8831 return false;
8832 }
8833 C = c0;
8834 return true;
8835}
8836
8837/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
8838/// can replace combinations of VMUL and VCVT (floating-point to integer)
8839/// when the VMUL has a constant operand that is a power of 2.
8840///
8841/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8842/// vmul.f32 d16, d17, d16
8843/// vcvt.s32.f32 d16, d16
8844/// becomes:
8845/// vcvt.s32.f32 d16, d16, #3
8846static SDValue PerformVCVTCombine(SDNode *N,
8847 TargetLowering::DAGCombinerInfo &DCI,
8848 const ARMSubtarget *Subtarget) {
8849 SelectionDAG &DAG = DCI.DAG;
8850 SDValue Op = N->getOperand(0);
8851
8852 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
8853 Op.getOpcode() != ISD::FMUL)
8854 return SDValue();
8855
8856 uint64_t C;
8857 SDValue N0 = Op->getOperand(0);
8858 SDValue ConstVec = Op->getOperand(1);
8859 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
8860
Eric Christopher1b8b94192011-06-29 21:10:36 +00008861 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosierfa8d8932011-06-24 19:23:04 +00008862 !isConstVecPow2(ConstVec, isSigned, C))
8863 return SDValue();
8864
Tim Northover7cbc2152013-06-28 15:29:25 +00008865 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
8866 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
8867 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
8868 // These instructions only exist converting from f32 to i32. We can handle
8869 // smaller integers by generating an extra truncate, but larger ones would
8870 // be lossy.
8871 return SDValue();
8872 }
8873
Chad Rosierfa8d8932011-06-24 19:23:04 +00008874 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
8875 Intrinsic::arm_neon_vcvtfp2fxu;
Tim Northover7cbc2152013-06-28 15:29:25 +00008876 unsigned NumLanes = Op.getValueType().getVectorNumElements();
8877 SDValue FixConv = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
8878 NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
8879 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
8880 DAG.getConstant(Log2_64(C), MVT::i32));
8881
8882 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
8883 FixConv = DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), FixConv);
8884
8885 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +00008886}
8887
8888/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
8889/// can replace combinations of VCVT (integer to floating-point) and VDIV
8890/// when the VDIV has a constant operand that is a power of 2.
8891///
8892/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8893/// vcvt.f32.s32 d16, d16
8894/// vdiv.f32 d16, d17, d16
8895/// becomes:
8896/// vcvt.f32.s32 d16, d16, #3
8897static SDValue PerformVDIVCombine(SDNode *N,
8898 TargetLowering::DAGCombinerInfo &DCI,
8899 const ARMSubtarget *Subtarget) {
8900 SelectionDAG &DAG = DCI.DAG;
8901 SDValue Op = N->getOperand(0);
8902 unsigned OpOpcode = Op.getNode()->getOpcode();
8903
8904 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
8905 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
8906 return SDValue();
8907
8908 uint64_t C;
8909 SDValue ConstVec = N->getOperand(1);
8910 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
8911
8912 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
8913 !isConstVecPow2(ConstVec, isSigned, C))
8914 return SDValue();
8915
Tim Northover7cbc2152013-06-28 15:29:25 +00008916 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
8917 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
8918 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
8919 // These instructions only exist converting from i32 to f32. We can handle
8920 // smaller integers by generating an extra extend, but larger ones would
8921 // be lossy.
8922 return SDValue();
8923 }
8924
8925 SDValue ConvInput = Op.getOperand(0);
8926 unsigned NumLanes = Op.getValueType().getVectorNumElements();
8927 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
8928 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
8929 SDLoc(N), NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
8930 ConvInput);
8931
Eric Christopher1b8b94192011-06-29 21:10:36 +00008932 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +00008933 Intrinsic::arm_neon_vcvtfxu2fp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008934 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
Chad Rosierfa8d8932011-06-24 19:23:04 +00008935 Op.getValueType(),
Eric Christopher1b8b94192011-06-29 21:10:36 +00008936 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Tim Northover7cbc2152013-06-28 15:29:25 +00008937 ConvInput, DAG.getConstant(Log2_64(C), MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +00008938}
8939
8940/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +00008941/// operand of a vector shift operation, where all the elements of the
8942/// build_vector must have the same constant integer value.
8943static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
8944 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +00008945 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00008946 Op = Op.getOperand(0);
8947 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
8948 APInt SplatBits, SplatUndef;
8949 unsigned SplatBitSize;
8950 bool HasAnyUndefs;
8951 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
8952 HasAnyUndefs, ElementBits) ||
8953 SplatBitSize > ElementBits)
8954 return false;
8955 Cnt = SplatBits.getSExtValue();
8956 return true;
8957}
8958
8959/// isVShiftLImm - Check if this is a valid build_vector for the immediate
8960/// operand of a vector shift left operation. That value must be in the range:
8961/// 0 <= Value < ElementBits for a left shift; or
8962/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00008963static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +00008964 assert(VT.isVector() && "vector shift count is not a vector type");
8965 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8966 if (! getVShiftImm(Op, ElementBits, Cnt))
8967 return false;
8968 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
8969}
8970
8971/// isVShiftRImm - Check if this is a valid build_vector for the immediate
8972/// operand of a vector shift right operation. For a shift opcode, the value
8973/// is positive, but for an intrinsic the value count must be negative. The
8974/// absolute value must be in the range:
8975/// 1 <= |Value| <= ElementBits for a right shift; or
8976/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00008977static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +00008978 int64_t &Cnt) {
8979 assert(VT.isVector() && "vector shift count is not a vector type");
8980 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8981 if (! getVShiftImm(Op, ElementBits, Cnt))
8982 return false;
8983 if (isIntrinsic)
8984 Cnt = -Cnt;
8985 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
8986}
8987
8988/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
8989static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
8990 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
8991 switch (IntNo) {
8992 default:
8993 // Don't do anything for most intrinsics.
8994 break;
8995
8996 // Vector shifts: check for immediate versions and lower them.
8997 // Note: This is done during DAG combining instead of DAG legalizing because
8998 // the build_vectors for 64-bit vector element shift counts are generally
8999 // not legal, and it is hard to see their values after they get legalized to
9000 // loads from a constant pool.
9001 case Intrinsic::arm_neon_vshifts:
9002 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +00009003 case Intrinsic::arm_neon_vrshifts:
9004 case Intrinsic::arm_neon_vrshiftu:
9005 case Intrinsic::arm_neon_vrshiftn:
9006 case Intrinsic::arm_neon_vqshifts:
9007 case Intrinsic::arm_neon_vqshiftu:
9008 case Intrinsic::arm_neon_vqshiftsu:
9009 case Intrinsic::arm_neon_vqshiftns:
9010 case Intrinsic::arm_neon_vqshiftnu:
9011 case Intrinsic::arm_neon_vqshiftnsu:
9012 case Intrinsic::arm_neon_vqrshiftns:
9013 case Intrinsic::arm_neon_vqrshiftnu:
9014 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009015 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009016 int64_t Cnt;
9017 unsigned VShiftOpc = 0;
9018
9019 switch (IntNo) {
9020 case Intrinsic::arm_neon_vshifts:
9021 case Intrinsic::arm_neon_vshiftu:
9022 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
9023 VShiftOpc = ARMISD::VSHL;
9024 break;
9025 }
9026 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
9027 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
9028 ARMISD::VSHRs : ARMISD::VSHRu);
9029 break;
9030 }
9031 return SDValue();
9032
Bob Wilson2e076c42009-06-22 23:27:02 +00009033 case Intrinsic::arm_neon_vrshifts:
9034 case Intrinsic::arm_neon_vrshiftu:
9035 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9036 break;
9037 return SDValue();
9038
9039 case Intrinsic::arm_neon_vqshifts:
9040 case Intrinsic::arm_neon_vqshiftu:
9041 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9042 break;
9043 return SDValue();
9044
9045 case Intrinsic::arm_neon_vqshiftsu:
9046 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9047 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +00009048 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009049
Bob Wilson2e076c42009-06-22 23:27:02 +00009050 case Intrinsic::arm_neon_vrshiftn:
9051 case Intrinsic::arm_neon_vqshiftns:
9052 case Intrinsic::arm_neon_vqshiftnu:
9053 case Intrinsic::arm_neon_vqshiftnsu:
9054 case Intrinsic::arm_neon_vqrshiftns:
9055 case Intrinsic::arm_neon_vqrshiftnu:
9056 case Intrinsic::arm_neon_vqrshiftnsu:
9057 // Narrowing shifts require an immediate right shift.
9058 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9059 break;
Jim Grosbach84511e12010-06-02 21:53:11 +00009060 llvm_unreachable("invalid shift count for narrowing vector shift "
9061 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009062
9063 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00009064 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00009065 }
9066
9067 switch (IntNo) {
9068 case Intrinsic::arm_neon_vshifts:
9069 case Intrinsic::arm_neon_vshiftu:
9070 // Opcode already set above.
9071 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00009072 case Intrinsic::arm_neon_vrshifts:
9073 VShiftOpc = ARMISD::VRSHRs; break;
9074 case Intrinsic::arm_neon_vrshiftu:
9075 VShiftOpc = ARMISD::VRSHRu; break;
9076 case Intrinsic::arm_neon_vrshiftn:
9077 VShiftOpc = ARMISD::VRSHRN; break;
9078 case Intrinsic::arm_neon_vqshifts:
9079 VShiftOpc = ARMISD::VQSHLs; break;
9080 case Intrinsic::arm_neon_vqshiftu:
9081 VShiftOpc = ARMISD::VQSHLu; break;
9082 case Intrinsic::arm_neon_vqshiftsu:
9083 VShiftOpc = ARMISD::VQSHLsu; break;
9084 case Intrinsic::arm_neon_vqshiftns:
9085 VShiftOpc = ARMISD::VQSHRNs; break;
9086 case Intrinsic::arm_neon_vqshiftnu:
9087 VShiftOpc = ARMISD::VQSHRNu; break;
9088 case Intrinsic::arm_neon_vqshiftnsu:
9089 VShiftOpc = ARMISD::VQSHRNsu; break;
9090 case Intrinsic::arm_neon_vqrshiftns:
9091 VShiftOpc = ARMISD::VQRSHRNs; break;
9092 case Intrinsic::arm_neon_vqrshiftnu:
9093 VShiftOpc = ARMISD::VQRSHRNu; break;
9094 case Intrinsic::arm_neon_vqrshiftnsu:
9095 VShiftOpc = ARMISD::VQRSHRNsu; break;
9096 }
9097
Andrew Trickef9de2a2013-05-25 02:42:55 +00009098 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009099 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009100 }
9101
9102 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009103 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009104 int64_t Cnt;
9105 unsigned VShiftOpc = 0;
9106
9107 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9108 VShiftOpc = ARMISD::VSLI;
9109 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9110 VShiftOpc = ARMISD::VSRI;
9111 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009112 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009113 }
9114
Andrew Trickef9de2a2013-05-25 02:42:55 +00009115 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +00009116 N->getOperand(1), N->getOperand(2),
Owen Anderson9f944592009-08-11 20:47:22 +00009117 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009118 }
9119
9120 case Intrinsic::arm_neon_vqrshifts:
9121 case Intrinsic::arm_neon_vqrshiftu:
9122 // No immediate versions of these to check for.
9123 break;
9124 }
9125
9126 return SDValue();
9127}
9128
9129/// PerformShiftCombine - Checks for immediate versions of vector shifts and
9130/// lowers them. As with the vector shift intrinsics, this is done during DAG
9131/// combining instead of DAG legalizing because the build_vectors for 64-bit
9132/// vector element shift counts are generally not legal, and it is hard to see
9133/// their values after they get legalized to loads from a constant pool.
9134static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9135 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009136 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +00009137 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9138 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9139 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9140 SDValue N1 = N->getOperand(1);
9141 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9142 SDValue N0 = N->getOperand(0);
9143 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9144 DAG.MaskedValueIsZero(N0.getOperand(0),
9145 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009146 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +00009147 }
9148 }
Bob Wilson2e076c42009-06-22 23:27:02 +00009149
9150 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +00009151 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9152 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +00009153 return SDValue();
9154
9155 assert(ST->hasNEON() && "unexpected vector shift");
9156 int64_t Cnt;
9157
9158 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009159 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009160
9161 case ISD::SHL:
9162 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009163 return DAG.getNode(ARMISD::VSHL, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009164 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009165 break;
9166
9167 case ISD::SRA:
9168 case ISD::SRL:
9169 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
9170 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
9171 ARMISD::VSHRs : ARMISD::VSHRu);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009172 return DAG.getNode(VShiftOpc, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009173 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009174 }
9175 }
9176 return SDValue();
9177}
9178
9179/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
9180/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
9181static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
9182 const ARMSubtarget *ST) {
9183 SDValue N0 = N->getOperand(0);
9184
9185 // Check for sign- and zero-extensions of vector extract operations of 8-
9186 // and 16-bit vector elements. NEON supports these directly. They are
9187 // handled during DAG combining because type legalization will promote them
9188 // to 32-bit types and it is messy to recognize the operations after that.
9189 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9190 SDValue Vec = N0.getOperand(0);
9191 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00009192 EVT VT = N->getValueType(0);
9193 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009194 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9195
Owen Anderson9f944592009-08-11 20:47:22 +00009196 if (VT == MVT::i32 &&
9197 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +00009198 TLI.isTypeLegal(Vec.getValueType()) &&
9199 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009200
9201 unsigned Opc = 0;
9202 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009203 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009204 case ISD::SIGN_EXTEND:
9205 Opc = ARMISD::VGETLANEs;
9206 break;
9207 case ISD::ZERO_EXTEND:
9208 case ISD::ANY_EXTEND:
9209 Opc = ARMISD::VGETLANEu;
9210 break;
9211 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009212 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +00009213 }
9214 }
9215
9216 return SDValue();
9217}
9218
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009219/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9220/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9221static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9222 const ARMSubtarget *ST) {
9223 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng55f0c6b2010-07-15 22:07:12 +00009224 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009225 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9226 // a NaN; only do the transformation when it matches that behavior.
9227
9228 // For now only do this when using NEON for FP operations; if using VFP, it
9229 // is not obvious that the benefit outweighs the cost of switching to the
9230 // NEON pipeline.
9231 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9232 N->getValueType(0) != MVT::f32)
9233 return SDValue();
9234
9235 SDValue CondLHS = N->getOperand(0);
9236 SDValue CondRHS = N->getOperand(1);
9237 SDValue LHS = N->getOperand(2);
9238 SDValue RHS = N->getOperand(3);
9239 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9240
9241 unsigned Opcode = 0;
9242 bool IsReversed;
Bob Wilsonba8ac742010-02-24 22:15:53 +00009243 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009244 IsReversed = false; // x CC y ? x : y
Bob Wilsonba8ac742010-02-24 22:15:53 +00009245 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009246 IsReversed = true ; // x CC y ? y : x
9247 } else {
9248 return SDValue();
9249 }
9250
Bob Wilsonba8ac742010-02-24 22:15:53 +00009251 bool IsUnordered;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009252 switch (CC) {
9253 default: break;
9254 case ISD::SETOLT:
9255 case ISD::SETOLE:
9256 case ISD::SETLT:
9257 case ISD::SETLE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009258 case ISD::SETULT:
9259 case ISD::SETULE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009260 // If LHS is NaN, an ordered comparison will be false and the result will
9261 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
9262 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9263 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
9264 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9265 break;
9266 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
9267 // will return -0, so vmin can only be used for unsafe math or if one of
9268 // the operands is known to be nonzero.
9269 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009270 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009271 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9272 break;
9273 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009274 break;
9275
9276 case ISD::SETOGT:
9277 case ISD::SETOGE:
9278 case ISD::SETGT:
9279 case ISD::SETGE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009280 case ISD::SETUGT:
9281 case ISD::SETUGE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009282 // If LHS is NaN, an ordered comparison will be false and the result will
9283 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
9284 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9285 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
9286 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9287 break;
9288 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
9289 // will return +0, so vmax can only be used for unsafe math or if one of
9290 // the operands is known to be nonzero.
9291 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009292 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009293 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9294 break;
9295 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009296 break;
9297 }
9298
9299 if (!Opcode)
9300 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009301 return DAG.getNode(Opcode, SDLoc(N), N->getValueType(0), LHS, RHS);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009302}
9303
Evan Chengf863e3f2011-07-13 00:42:17 +00009304/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
9305SDValue
9306ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
9307 SDValue Cmp = N->getOperand(4);
9308 if (Cmp.getOpcode() != ARMISD::CMPZ)
9309 // Only looking at EQ and NE cases.
9310 return SDValue();
9311
9312 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009313 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +00009314 SDValue LHS = Cmp.getOperand(0);
9315 SDValue RHS = Cmp.getOperand(1);
9316 SDValue FalseVal = N->getOperand(0);
9317 SDValue TrueVal = N->getOperand(1);
9318 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +00009319 ARMCC::CondCodes CC =
9320 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +00009321
9322 // Simplify
9323 // mov r1, r0
9324 // cmp r1, x
9325 // mov r0, y
9326 // moveq r0, x
9327 // to
9328 // cmp r0, x
9329 // movne r0, y
9330 //
9331 // mov r1, r0
9332 // cmp r1, x
9333 // mov r0, x
9334 // movne r0, y
9335 // to
9336 // cmp r0, x
9337 // movne r0, y
9338 /// FIXME: Turn this into a target neutral optimization?
9339 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +00009340 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +00009341 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
9342 N->getOperand(3), Cmp);
9343 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
9344 SDValue ARMcc;
9345 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
9346 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
9347 N->getOperand(3), NewCmp);
9348 }
9349
9350 if (Res.getNode()) {
9351 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00009352 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +00009353 // Capture demanded bits information that would be otherwise lost.
9354 if (KnownZero == 0xfffffffe)
9355 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9356 DAG.getValueType(MVT::i1));
9357 else if (KnownZero == 0xffffff00)
9358 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9359 DAG.getValueType(MVT::i8));
9360 else if (KnownZero == 0xffff0000)
9361 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9362 DAG.getValueType(MVT::i16));
9363 }
9364
9365 return Res;
9366}
9367
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009368SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +00009369 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009370 switch (N->getOpcode()) {
9371 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009372 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +00009373 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009374 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009375 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +00009376 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +00009377 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
9378 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +00009379 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00009380 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson22806742010-09-22 22:09:21 +00009381 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009382 case ISD::STORE: return PerformSTORECombine(N, DCI);
9383 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
9384 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +00009385 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +00009386 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +00009387 case ISD::FP_TO_SINT:
9388 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
9389 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009390 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00009391 case ISD::SHL:
9392 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009393 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00009394 case ISD::SIGN_EXTEND:
9395 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009396 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
9397 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +00009398 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson06fce872011-02-07 17:43:21 +00009399 case ARMISD::VLD2DUP:
9400 case ARMISD::VLD3DUP:
9401 case ARMISD::VLD4DUP:
9402 return CombineBaseUpdate(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009403 case ARMISD::BUILD_VECTOR:
9404 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +00009405 case ISD::INTRINSIC_VOID:
9406 case ISD::INTRINSIC_W_CHAIN:
9407 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9408 case Intrinsic::arm_neon_vld1:
9409 case Intrinsic::arm_neon_vld2:
9410 case Intrinsic::arm_neon_vld3:
9411 case Intrinsic::arm_neon_vld4:
9412 case Intrinsic::arm_neon_vld2lane:
9413 case Intrinsic::arm_neon_vld3lane:
9414 case Intrinsic::arm_neon_vld4lane:
9415 case Intrinsic::arm_neon_vst1:
9416 case Intrinsic::arm_neon_vst2:
9417 case Intrinsic::arm_neon_vst3:
9418 case Intrinsic::arm_neon_vst4:
9419 case Intrinsic::arm_neon_vst2lane:
9420 case Intrinsic::arm_neon_vst3lane:
9421 case Intrinsic::arm_neon_vst4lane:
9422 return CombineBaseUpdate(N, DCI);
9423 default: break;
9424 }
9425 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009426 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009427 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009428}
9429
Evan Chengd42641c2011-02-02 01:06:55 +00009430bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9431 EVT VT) const {
9432 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9433}
9434
Matt Arsenault25793a32014-02-05 23:15:53 +00009435bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT, unsigned,
9436 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009437 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +00009438 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009439
9440 switch (VT.getSimpleVT().SimpleTy) {
9441 default:
9442 return false;
9443 case MVT::i8:
9444 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009445 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009446 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +00009447 if (AllowsUnaligned) {
9448 if (Fast)
9449 *Fast = Subtarget->hasV7Ops();
9450 return true;
9451 }
9452 return false;
9453 }
Evan Chengeec6bc62012-08-15 17:44:53 +00009454 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009455 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009456 // For any little-endian targets with neon, we can support unaligned ld/st
9457 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +00009458 // A big-endian target may also explicitly support unaligned accesses
Evan Cheng79e2ca92012-12-10 23:21:26 +00009459 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) {
9460 if (Fast)
9461 *Fast = true;
9462 return true;
9463 }
9464 return false;
9465 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009466 }
9467}
9468
Lang Hames9929c422011-11-02 22:52:45 +00009469static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
9470 unsigned AlignCheck) {
9471 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
9472 (DstAlign == 0 || DstAlign % AlignCheck == 0));
9473}
9474
9475EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
9476 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00009477 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +00009478 bool MemcpyStrSrc,
9479 MachineFunction &MF) const {
9480 const Function *F = MF.getFunction();
9481
9482 // See if we can use NEON instructions for this...
Evan Cheng962711e2012-12-12 02:34:41 +00009483 if ((!IsMemset || ZeroMemset) &&
Evan Cheng79e2ca92012-12-10 23:21:26 +00009484 Subtarget->hasNEON() &&
Bill Wendling698e84f2012-12-30 10:32:01 +00009485 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
9486 Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009487 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +00009488 if (Size >= 16 &&
9489 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault25793a32014-02-05 23:15:53 +00009490 (allowsUnalignedMemoryAccesses(MVT::v2f64, 0, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009491 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +00009492 } else if (Size >= 8 &&
9493 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault25793a32014-02-05 23:15:53 +00009494 (allowsUnalignedMemoryAccesses(MVT::f64, 0, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009495 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +00009496 }
9497 }
9498
Lang Hamesb85fcd02011-11-08 18:56:23 +00009499 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +00009500 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +00009501 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +00009502 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +00009503 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +00009504
Lang Hames9929c422011-11-02 22:52:45 +00009505 // Let the target-independent logic figure it out.
9506 return MVT::Other;
9507}
9508
Evan Cheng9ec512d2012-12-06 19:13:27 +00009509bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
9510 if (Val.getOpcode() != ISD::LOAD)
9511 return false;
9512
9513 EVT VT1 = Val.getValueType();
9514 if (!VT1.isSimple() || !VT1.isInteger() ||
9515 !VT2.isSimple() || !VT2.isInteger())
9516 return false;
9517
9518 switch (VT1.getSimpleVT().SimpleTy) {
9519 default: break;
9520 case MVT::i1:
9521 case MVT::i8:
9522 case MVT::i16:
9523 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
9524 return true;
9525 }
9526
9527 return false;
9528}
9529
Tim Northovercc2e9032013-08-06 13:58:03 +00009530bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
9531 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
9532 return false;
9533
9534 if (!isTypeLegal(EVT::getEVT(Ty1)))
9535 return false;
9536
9537 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
9538
9539 // Assuming the caller doesn't have a zeroext or signext return parameter,
9540 // truncation all the way down to i1 is valid.
9541 return true;
9542}
9543
9544
Evan Chengdc49a8d2009-08-14 20:09:37 +00009545static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
9546 if (V < 0)
9547 return false;
9548
9549 unsigned Scale = 1;
9550 switch (VT.getSimpleVT().SimpleTy) {
9551 default: return false;
9552 case MVT::i1:
9553 case MVT::i8:
9554 // Scale == 1;
9555 break;
9556 case MVT::i16:
9557 // Scale == 2;
9558 Scale = 2;
9559 break;
9560 case MVT::i32:
9561 // Scale == 4;
9562 Scale = 4;
9563 break;
9564 }
9565
9566 if ((V & (Scale - 1)) != 0)
9567 return false;
9568 V /= Scale;
9569 return V == (V & ((1LL << 5) - 1));
9570}
9571
9572static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
9573 const ARMSubtarget *Subtarget) {
9574 bool isNeg = false;
9575 if (V < 0) {
9576 isNeg = true;
9577 V = - V;
9578 }
9579
9580 switch (VT.getSimpleVT().SimpleTy) {
9581 default: return false;
9582 case MVT::i1:
9583 case MVT::i8:
9584 case MVT::i16:
9585 case MVT::i32:
9586 // + imm12 or - imm8
9587 if (isNeg)
9588 return V == (V & ((1LL << 8) - 1));
9589 return V == (V & ((1LL << 12) - 1));
9590 case MVT::f32:
9591 case MVT::f64:
9592 // Same as ARM mode. FIXME: NEON?
9593 if (!Subtarget->hasVFP2())
9594 return false;
9595 if ((V & 3) != 0)
9596 return false;
9597 V >>= 2;
9598 return V == (V & ((1LL << 8) - 1));
9599 }
9600}
9601
Evan Cheng2150b922007-03-12 23:30:29 +00009602/// isLegalAddressImmediate - Return true if the integer value can be used
9603/// as the offset of the target addressing mode for load / store of the
9604/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009605static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009606 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +00009607 if (V == 0)
9608 return true;
9609
Evan Chengce5dfb62009-03-09 19:15:00 +00009610 if (!VT.isSimple())
9611 return false;
9612
Evan Chengdc49a8d2009-08-14 20:09:37 +00009613 if (Subtarget->isThumb1Only())
9614 return isLegalT1AddressImmediate(V, VT);
9615 else if (Subtarget->isThumb2())
9616 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +00009617
Evan Chengdc49a8d2009-08-14 20:09:37 +00009618 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +00009619 if (V < 0)
9620 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +00009621 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +00009622 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +00009623 case MVT::i1:
9624 case MVT::i8:
9625 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +00009626 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009627 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +00009628 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +00009629 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009630 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +00009631 case MVT::f32:
9632 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009633 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +00009634 return false;
Evan Chengbef131de2007-05-03 02:00:18 +00009635 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +00009636 return false;
9637 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009638 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +00009639 }
Evan Cheng10043e22007-01-19 07:51:42 +00009640}
9641
Evan Chengdc49a8d2009-08-14 20:09:37 +00009642bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
9643 EVT VT) const {
9644 int Scale = AM.Scale;
9645 if (Scale < 0)
9646 return false;
9647
9648 switch (VT.getSimpleVT().SimpleTy) {
9649 default: return false;
9650 case MVT::i1:
9651 case MVT::i8:
9652 case MVT::i16:
9653 case MVT::i32:
9654 if (Scale == 1)
9655 return true;
9656 // r + r << imm
9657 Scale = Scale & ~1;
9658 return Scale == 2 || Scale == 4 || Scale == 8;
9659 case MVT::i64:
9660 // r + r
9661 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
9662 return true;
9663 return false;
9664 case MVT::isVoid:
9665 // Note, we allow "void" uses (basically, uses that aren't loads or
9666 // stores), because arm allows folding a scale into many arithmetic
9667 // operations. This should be made more precise and revisited later.
9668
9669 // Allow r << imm, but the imm has to be a multiple of two.
9670 if (Scale & 1) return false;
9671 return isPowerOf2_32(Scale);
9672 }
9673}
9674
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009675/// isLegalAddressingMode - Return true if the addressing mode represented
9676/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson7117a912009-03-20 22:42:55 +00009677bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +00009678 Type *Ty) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009679 EVT VT = getValueType(Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +00009680 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +00009681 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009682
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009683 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +00009684 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009685 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009686
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009687 switch (AM.Scale) {
9688 case 0: // no scale reg, must be "r+i" or "r", or "i".
9689 break;
9690 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009691 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009692 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +00009693 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009694 default:
Chris Lattner502c3f42007-04-13 06:50:55 +00009695 // ARM doesn't support any R+R*scale+imm addr modes.
9696 if (AM.BaseOffs)
9697 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009698
Bob Wilson866c1742009-04-08 17:55:28 +00009699 if (!VT.isSimple())
9700 return false;
9701
Evan Chengdc49a8d2009-08-14 20:09:37 +00009702 if (Subtarget->isThumb2())
9703 return isLegalT2ScaledAddressingMode(AM, VT);
9704
Chris Lattner9b6d69e2007-04-10 03:48:29 +00009705 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +00009706 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009707 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +00009708 case MVT::i1:
9709 case MVT::i8:
9710 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +00009711 if (Scale < 0) Scale = -Scale;
9712 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009713 return true;
9714 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +00009715 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +00009716 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009717 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009718 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +00009719 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009720 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +00009721 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009722
Owen Anderson9f944592009-08-11 20:47:22 +00009723 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009724 // Note, we allow "void" uses (basically, uses that aren't loads or
9725 // stores), because arm allows folding a scale into many arithmetic
9726 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +00009727
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009728 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +00009729 if (Scale & 1) return false;
9730 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009731 }
Evan Cheng2150b922007-03-12 23:30:29 +00009732 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009733 return true;
Evan Cheng2150b922007-03-12 23:30:29 +00009734}
9735
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009736/// isLegalICmpImmediate - Return true if the specified immediate is legal
9737/// icmp immediate, that is the target has icmp instructions which can compare
9738/// a register against the immediate without having to materialize the
9739/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +00009740bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +00009741 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009742 if (!Subtarget->isThumb())
Chandler Carruth8a102c22012-04-06 20:10:52 +00009743 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009744 if (Subtarget->isThumb2())
Chandler Carruth8a102c22012-04-06 20:10:52 +00009745 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +00009746 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +00009747 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009748}
9749
Andrew Tricka22cdb72012-07-18 18:34:27 +00009750/// isLegalAddImmediate - Return true if the specified immediate is a legal add
9751/// *or sub* immediate, that is the target has add or sub instructions which can
9752/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +00009753/// immediate into a register.
9754bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +00009755 // Same encoding for add/sub, just flip the sign.
9756 int64_t AbsImm = llvm::abs64(Imm);
9757 if (!Subtarget->isThumb())
9758 return ARM_AM::getSOImmVal(AbsImm) != -1;
9759 if (Subtarget->isThumb2())
9760 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
9761 // Thumb1 only has 8-bit unsigned immediate.
9762 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +00009763}
9764
Owen Anderson53aa7a92009-08-10 22:56:29 +00009765static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +00009766 bool isSEXTLoad, SDValue &Base,
9767 SDValue &Offset, bool &isInc,
9768 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +00009769 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9770 return false;
9771
Owen Anderson9f944592009-08-11 20:47:22 +00009772 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +00009773 // AddressingMode 3
9774 Base = Ptr->getOperand(0);
9775 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00009776 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +00009777 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +00009778 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +00009779 isInc = false;
9780 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9781 return true;
9782 }
9783 }
9784 isInc = (Ptr->getOpcode() == ISD::ADD);
9785 Offset = Ptr->getOperand(1);
9786 return true;
Owen Anderson9f944592009-08-11 20:47:22 +00009787 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +00009788 // AddressingMode 2
9789 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00009790 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +00009791 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +00009792 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +00009793 isInc = false;
9794 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9795 Base = Ptr->getOperand(0);
9796 return true;
9797 }
9798 }
9799
9800 if (Ptr->getOpcode() == ISD::ADD) {
9801 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +00009802 ARM_AM::ShiftOpc ShOpcVal=
9803 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +00009804 if (ShOpcVal != ARM_AM::no_shift) {
9805 Base = Ptr->getOperand(1);
9806 Offset = Ptr->getOperand(0);
9807 } else {
9808 Base = Ptr->getOperand(0);
9809 Offset = Ptr->getOperand(1);
9810 }
9811 return true;
9812 }
9813
9814 isInc = (Ptr->getOpcode() == ISD::ADD);
9815 Base = Ptr->getOperand(0);
9816 Offset = Ptr->getOperand(1);
9817 return true;
9818 }
9819
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00009820 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +00009821 return false;
9822}
9823
Owen Anderson53aa7a92009-08-10 22:56:29 +00009824static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +00009825 bool isSEXTLoad, SDValue &Base,
9826 SDValue &Offset, bool &isInc,
9827 SelectionDAG &DAG) {
9828 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9829 return false;
9830
9831 Base = Ptr->getOperand(0);
9832 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
9833 int RHSC = (int)RHS->getZExtValue();
9834 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
9835 assert(Ptr->getOpcode() == ISD::ADD);
9836 isInc = false;
9837 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9838 return true;
9839 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
9840 isInc = Ptr->getOpcode() == ISD::ADD;
9841 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
9842 return true;
9843 }
9844 }
9845
9846 return false;
9847}
9848
Evan Cheng10043e22007-01-19 07:51:42 +00009849/// getPreIndexedAddressParts - returns true by value, base pointer and
9850/// offset pointer and addressing mode by reference if the node's address
9851/// can be legally represented as pre-indexed load / store address.
9852bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009853ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
9854 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +00009855 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +00009856 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +00009857 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +00009858 return false;
9859
Owen Anderson53aa7a92009-08-10 22:56:29 +00009860 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009861 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +00009862 bool isSEXTLoad = false;
9863 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
9864 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00009865 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +00009866 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9867 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
9868 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00009869 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +00009870 } else
9871 return false;
9872
9873 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +00009874 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +00009875 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +00009876 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
9877 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +00009878 else
Evan Cheng84c6cda2009-07-02 07:28:31 +00009879 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +00009880 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +00009881 if (!isLegal)
9882 return false;
9883
9884 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
9885 return true;
Evan Cheng10043e22007-01-19 07:51:42 +00009886}
9887
9888/// getPostIndexedAddressParts - returns true by value, base pointer and
9889/// offset pointer and addressing mode by reference if this node can be
9890/// combined with a load / store to form a post-indexed load / store.
9891bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009892 SDValue &Base,
9893 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +00009894 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +00009895 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +00009896 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +00009897 return false;
9898
Owen Anderson53aa7a92009-08-10 22:56:29 +00009899 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009900 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +00009901 bool isSEXTLoad = false;
9902 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +00009903 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +00009904 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +00009905 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9906 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +00009907 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +00009908 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +00009909 } else
9910 return false;
9911
9912 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +00009913 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +00009914 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +00009915 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +00009916 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +00009917 else
Evan Cheng84c6cda2009-07-02 07:28:31 +00009918 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
9919 isInc, DAG);
9920 if (!isLegal)
9921 return false;
9922
Evan Chengf19384d2010-05-18 21:31:17 +00009923 if (Ptr != Base) {
9924 // Swap base ptr and offset to catch more post-index load / store when
9925 // it's legal. In Thumb2 mode, offset must be an immediate.
9926 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
9927 !Subtarget->isThumb2())
9928 std::swap(Base, Offset);
9929
9930 // Post-indexed load / store update the base pointer.
9931 if (Ptr != Base)
9932 return false;
9933 }
9934
Evan Cheng84c6cda2009-07-02 07:28:31 +00009935 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
9936 return true;
Evan Cheng10043e22007-01-19 07:51:42 +00009937}
9938
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009939void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Bob Wilson7117a912009-03-20 22:42:55 +00009940 APInt &KnownZero,
Dan Gohmanf990faf2008-02-13 00:35:47 +00009941 APInt &KnownOne,
Dan Gohman309d3d52007-06-22 14:59:07 +00009942 const SelectionDAG &DAG,
Evan Cheng10043e22007-01-19 07:51:42 +00009943 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +00009944 unsigned BitWidth = KnownOne.getBitWidth();
9945 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00009946 switch (Op.getOpcode()) {
9947 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +00009948 case ARMISD::ADDC:
9949 case ARMISD::ADDE:
9950 case ARMISD::SUBC:
9951 case ARMISD::SUBE:
9952 // These nodes' second result is a boolean
9953 if (Op.getResNo() == 0)
9954 break;
9955 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
9956 break;
Evan Cheng10043e22007-01-19 07:51:42 +00009957 case ARMISD::CMOV: {
9958 // Bits are known zero/one if known on the LHS and RHS.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00009959 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +00009960 if (KnownZero == 0 && KnownOne == 0) return;
9961
Dan Gohmanf990faf2008-02-13 00:35:47 +00009962 APInt KnownZeroRHS, KnownOneRHS;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00009963 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +00009964 KnownZero &= KnownZeroRHS;
9965 KnownOne &= KnownOneRHS;
9966 return;
9967 }
Tim Northover01b4aa92014-04-03 15:10:35 +00009968 case ISD::INTRINSIC_W_CHAIN: {
9969 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
9970 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
9971 switch (IntID) {
9972 default: return;
9973 case Intrinsic::arm_ldaex:
9974 case Intrinsic::arm_ldrex: {
9975 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
9976 unsigned MemBits = VT.getScalarType().getSizeInBits();
9977 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
9978 return;
9979 }
9980 }
9981 }
Evan Cheng10043e22007-01-19 07:51:42 +00009982 }
9983}
9984
9985//===----------------------------------------------------------------------===//
9986// ARM Inline Assembly Support
9987//===----------------------------------------------------------------------===//
9988
Evan Cheng078b0b02011-01-08 01:24:27 +00009989bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
9990 // Looking for "rev" which is V6+.
9991 if (!Subtarget->hasV6Ops())
9992 return false;
9993
9994 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9995 std::string AsmStr = IA->getAsmString();
9996 SmallVector<StringRef, 4> AsmPieces;
9997 SplitString(AsmStr, AsmPieces, ";\n");
9998
9999 switch (AsmPieces.size()) {
10000 default: return false;
10001 case 1:
10002 AsmStr = AsmPieces[0];
10003 AsmPieces.clear();
10004 SplitString(AsmStr, AsmPieces, " \t,");
10005
10006 // rev $0, $1
10007 if (AsmPieces.size() == 3 &&
10008 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
10009 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000010010 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000010011 if (Ty && Ty->getBitWidth() == 32)
10012 return IntrinsicLowering::LowerToByteSwap(CI);
10013 }
10014 break;
10015 }
10016
10017 return false;
10018}
10019
Evan Cheng10043e22007-01-19 07:51:42 +000010020/// getConstraintType - Given a constraint letter, return the type of
10021/// constraint it is for this target.
10022ARMTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +000010023ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
10024 if (Constraint.size() == 1) {
10025 switch (Constraint[0]) {
10026 default: break;
10027 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000010028 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000010029 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000010030 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000010031 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000010032 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000010033 // An address with a single base register. Due to the way we
10034 // currently handle addresses it is the same as an 'r' memory constraint.
10035 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000010036 }
Eric Christophere256cd02011-06-21 22:10:57 +000010037 } else if (Constraint.size() == 2) {
10038 switch (Constraint[0]) {
10039 default: break;
10040 // All 'U+' constraints are addresses.
10041 case 'U': return C_Memory;
10042 }
Evan Cheng10043e22007-01-19 07:51:42 +000010043 }
Chris Lattnerd6855142007-03-25 02:14:49 +000010044 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000010045}
10046
John Thompsone8360b72010-10-29 17:29:13 +000010047/// Examine constraint type and operand type and determine a weight value.
10048/// This object must already have been set up with the operand type
10049/// and the current alternative constraint selected.
10050TargetLowering::ConstraintWeight
10051ARMTargetLowering::getSingleConstraintMatchWeight(
10052 AsmOperandInfo &info, const char *constraint) const {
10053 ConstraintWeight weight = CW_Invalid;
10054 Value *CallOperandVal = info.CallOperandVal;
10055 // If we don't have a value, we can't do a match,
10056 // but allow it at the lowest weight.
10057 if (CallOperandVal == NULL)
10058 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000010059 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000010060 // Look at the constraint type.
10061 switch (*constraint) {
10062 default:
10063 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10064 break;
10065 case 'l':
10066 if (type->isIntegerTy()) {
10067 if (Subtarget->isThumb())
10068 weight = CW_SpecificReg;
10069 else
10070 weight = CW_Register;
10071 }
10072 break;
10073 case 'w':
10074 if (type->isFloatingPointTy())
10075 weight = CW_Register;
10076 break;
10077 }
10078 return weight;
10079}
10080
Eric Christophercf2007c2011-06-30 23:50:52 +000010081typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
10082RCPair
Evan Cheng10043e22007-01-19 07:51:42 +000010083ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +000010084 MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000010085 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010086 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000010087 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000010088 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010089 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010090 return RCPair(0U, &ARM::tGPRRegClass);
10091 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000010092 case 'h': // High regs or no regs.
10093 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010094 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000010095 break;
Chris Lattner6223e832007-04-02 17:24:08 +000010096 case 'r':
Craig Topperc7242e02012-04-20 07:30:17 +000010097 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010098 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000010099 if (VT == MVT::Other)
10100 break;
Owen Anderson9f944592009-08-11 20:47:22 +000010101 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010102 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000010103 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010104 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000010105 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010106 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010107 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010108 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000010109 if (VT == MVT::Other)
10110 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010111 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010112 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010113 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010114 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010115 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010116 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010117 break;
Eric Christopherc011d312011-07-01 00:30:46 +000010118 case 't':
10119 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010120 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000010121 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010122 }
10123 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000010124 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000010125 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000010126
Evan Cheng10043e22007-01-19 07:51:42 +000010127 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
10128}
10129
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010130/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10131/// vector. If it is invalid, don't add anything to Ops.
10132void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000010133 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010134 std::vector<SDValue>&Ops,
10135 SelectionDAG &DAG) const {
10136 SDValue Result(0, 0);
10137
Eric Christopherde9399b2011-06-02 23:16:42 +000010138 // Currently only support length 1 constraints.
10139 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000010140
Eric Christopherde9399b2011-06-02 23:16:42 +000010141 char ConstraintLetter = Constraint[0];
10142 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010143 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000010144 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010145 case 'I': case 'J': case 'K': case 'L':
10146 case 'M': case 'N': case 'O':
10147 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10148 if (!C)
10149 return;
10150
10151 int64_t CVal64 = C->getSExtValue();
10152 int CVal = (int) CVal64;
10153 // None of these constraints allow values larger than 32 bits. Check
10154 // that the value fits in an int.
10155 if (CVal != CVal64)
10156 return;
10157
Eric Christopherde9399b2011-06-02 23:16:42 +000010158 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000010159 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000010160 // Constant suitable for movw, must be between 0 and
10161 // 65535.
10162 if (Subtarget->hasV6T2Ops())
10163 if (CVal >= 0 && CVal <= 65535)
10164 break;
10165 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010166 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000010167 if (Subtarget->isThumb1Only()) {
10168 // This must be a constant between 0 and 255, for ADD
10169 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010170 if (CVal >= 0 && CVal <= 255)
10171 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010172 } else if (Subtarget->isThumb2()) {
10173 // A constant that can be used as an immediate value in a
10174 // data-processing instruction.
10175 if (ARM_AM::getT2SOImmVal(CVal) != -1)
10176 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010177 } else {
10178 // A constant that can be used as an immediate value in a
10179 // data-processing instruction.
10180 if (ARM_AM::getSOImmVal(CVal) != -1)
10181 break;
10182 }
10183 return;
10184
10185 case 'J':
David Goodwin22c2fba2009-07-08 23:10:31 +000010186 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010187 // This must be a constant between -255 and -1, for negated ADD
10188 // immediates. This can be used in GCC with an "n" modifier that
10189 // prints the negated value, for use with SUB instructions. It is
10190 // not useful otherwise but is implemented for compatibility.
10191 if (CVal >= -255 && CVal <= -1)
10192 break;
10193 } else {
10194 // This must be a constant between -4095 and 4095. It is not clear
10195 // what this constraint is intended for. Implemented for
10196 // compatibility with GCC.
10197 if (CVal >= -4095 && CVal <= 4095)
10198 break;
10199 }
10200 return;
10201
10202 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000010203 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010204 // A 32-bit value where only one byte has a nonzero value. Exclude
10205 // zero to match GCC. This constraint is used by GCC internally for
10206 // constants that can be loaded with a move/shift combination.
10207 // It is not useful otherwise but is implemented for compatibility.
10208 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
10209 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010210 } else if (Subtarget->isThumb2()) {
10211 // A constant whose bitwise inverse can be used as an immediate
10212 // value in a data-processing instruction. This can be used in GCC
10213 // with a "B" modifier that prints the inverted value, for use with
10214 // BIC and MVN instructions. It is not useful otherwise but is
10215 // implemented for compatibility.
10216 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
10217 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010218 } else {
10219 // A constant whose bitwise inverse can be used as an immediate
10220 // value in a data-processing instruction. This can be used in GCC
10221 // with a "B" modifier that prints the inverted value, for use with
10222 // BIC and MVN instructions. It is not useful otherwise but is
10223 // implemented for compatibility.
10224 if (ARM_AM::getSOImmVal(~CVal) != -1)
10225 break;
10226 }
10227 return;
10228
10229 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000010230 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010231 // This must be a constant between -7 and 7,
10232 // for 3-operand ADD/SUB immediate instructions.
10233 if (CVal >= -7 && CVal < 7)
10234 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010235 } else if (Subtarget->isThumb2()) {
10236 // A constant whose negation can be used as an immediate value in a
10237 // data-processing instruction. This can be used in GCC with an "n"
10238 // modifier that prints the negated value, for use with SUB
10239 // instructions. It is not useful otherwise but is implemented for
10240 // compatibility.
10241 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
10242 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010243 } else {
10244 // A constant whose negation can be used as an immediate value in a
10245 // data-processing instruction. This can be used in GCC with an "n"
10246 // modifier that prints the negated value, for use with SUB
10247 // instructions. It is not useful otherwise but is implemented for
10248 // compatibility.
10249 if (ARM_AM::getSOImmVal(-CVal) != -1)
10250 break;
10251 }
10252 return;
10253
10254 case 'M':
David Goodwin22c2fba2009-07-08 23:10:31 +000010255 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010256 // This must be a multiple of 4 between 0 and 1020, for
10257 // ADD sp + immediate.
10258 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
10259 break;
10260 } else {
10261 // A power of two or a constant between 0 and 32. This is used in
10262 // GCC for the shift amount on shifted register operands, but it is
10263 // useful in general for any shift amounts.
10264 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
10265 break;
10266 }
10267 return;
10268
10269 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000010270 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010271 // This must be a constant between 0 and 31, for shift amounts.
10272 if (CVal >= 0 && CVal <= 31)
10273 break;
10274 }
10275 return;
10276
10277 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000010278 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010279 // This must be a multiple of 4 between -508 and 508, for
10280 // ADD/SUB sp = sp + immediate.
10281 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
10282 break;
10283 }
10284 return;
10285 }
10286 Result = DAG.getTargetConstant(CVal, Op.getValueType());
10287 break;
10288 }
10289
10290 if (Result.getNode()) {
10291 Ops.push_back(Result);
10292 return;
10293 }
Dale Johannesence97d552010-06-25 21:55:36 +000010294 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010295}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010296
Renato Golin87610692013-07-16 09:32:17 +000010297SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
10298 assert(Subtarget->isTargetAEABI() && "Register-based DivRem lowering only");
10299 unsigned Opcode = Op->getOpcode();
10300 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
10301 "Invalid opcode for Div/Rem lowering");
10302 bool isSigned = (Opcode == ISD::SDIVREM);
10303 EVT VT = Op->getValueType(0);
10304 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
10305
10306 RTLIB::Libcall LC;
10307 switch (VT.getSimpleVT().SimpleTy) {
10308 default: llvm_unreachable("Unexpected request for libcall!");
10309 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
10310 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
10311 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
10312 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
10313 }
10314
10315 SDValue InChain = DAG.getEntryNode();
10316
10317 TargetLowering::ArgListTy Args;
10318 TargetLowering::ArgListEntry Entry;
10319 for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) {
10320 EVT ArgVT = Op->getOperand(i).getValueType();
10321 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
10322 Entry.Node = Op->getOperand(i);
10323 Entry.Ty = ArgTy;
10324 Entry.isSExt = isSigned;
10325 Entry.isZExt = !isSigned;
10326 Args.push_back(Entry);
10327 }
10328
10329 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
10330 getPointerTy());
10331
10332 Type *RetTy = (Type*)StructType::get(Ty, Ty, NULL);
10333
10334 SDLoc dl(Op);
10335 TargetLowering::
10336 CallLoweringInfo CLI(InChain, RetTy, isSigned, !isSigned, false, true,
10337 0, getLibcallCallingConv(LC), /*isTailCall=*/false,
10338 /*doesNotReturn=*/false, /*isReturnValueUsed=*/true,
10339 Callee, Args, DAG, dl);
10340 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
10341
10342 return CallInfo.first;
10343}
10344
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010345bool
10346ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
10347 // The ARM target isn't yet aware of offsets.
10348 return false;
10349}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010350
Jim Grosbach11013ed2010-07-16 23:05:05 +000010351bool ARM::isBitFieldInvertedMask(unsigned v) {
10352 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010353 return false;
10354
Jim Grosbach11013ed2010-07-16 23:05:05 +000010355 // there can be 1's on either or both "outsides", all the "inside"
10356 // bits must be 0's
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010357 unsigned TO = CountTrailingOnes_32(v);
10358 unsigned LO = CountLeadingOnes_32(v);
10359 v = (v >> TO) << TO;
10360 v = (v << LO) >> LO;
10361 return v == 0;
Jim Grosbach11013ed2010-07-16 23:05:05 +000010362}
10363
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010364/// isFPImmLegal - Returns true if the target can instruction select the
10365/// specified FP immediate natively. If false, the legalizer will
10366/// materialize the FP immediate as a load from a constant pool.
10367bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
10368 if (!Subtarget->hasVFP3())
10369 return false;
10370 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000010371 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010372 if (VT == MVT::f64)
Jim Grosbachefc761a2011-09-30 00:50:06 +000010373 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010374 return false;
10375}
Bob Wilson5549d492010-09-21 17:56:22 +000010376
Wesley Peck527da1b2010-11-23 03:31:01 +000010377/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000010378/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
10379/// specified in the intrinsic calls.
10380bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
10381 const CallInst &I,
10382 unsigned Intrinsic) const {
10383 switch (Intrinsic) {
10384 case Intrinsic::arm_neon_vld1:
10385 case Intrinsic::arm_neon_vld2:
10386 case Intrinsic::arm_neon_vld3:
10387 case Intrinsic::arm_neon_vld4:
10388 case Intrinsic::arm_neon_vld2lane:
10389 case Intrinsic::arm_neon_vld3lane:
10390 case Intrinsic::arm_neon_vld4lane: {
10391 Info.opc = ISD::INTRINSIC_W_CHAIN;
10392 // Conservatively set memVT to the entire set of vectors loaded.
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010393 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010394 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10395 Info.ptrVal = I.getArgOperand(0);
10396 Info.offset = 0;
10397 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10398 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10399 Info.vol = false; // volatile loads with NEON intrinsics not supported
10400 Info.readMem = true;
10401 Info.writeMem = false;
10402 return true;
10403 }
10404 case Intrinsic::arm_neon_vst1:
10405 case Intrinsic::arm_neon_vst2:
10406 case Intrinsic::arm_neon_vst3:
10407 case Intrinsic::arm_neon_vst4:
10408 case Intrinsic::arm_neon_vst2lane:
10409 case Intrinsic::arm_neon_vst3lane:
10410 case Intrinsic::arm_neon_vst4lane: {
10411 Info.opc = ISD::INTRINSIC_VOID;
10412 // Conservatively set memVT to the entire set of vectors stored.
10413 unsigned NumElts = 0;
10414 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000010415 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000010416 if (!ArgTy->isVectorTy())
10417 break;
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010418 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010419 }
10420 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10421 Info.ptrVal = I.getArgOperand(0);
10422 Info.offset = 0;
10423 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10424 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10425 Info.vol = false; // volatile stores with NEON intrinsics not supported
10426 Info.readMem = false;
10427 Info.writeMem = true;
10428 return true;
10429 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010430 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000010431 case Intrinsic::arm_ldrex: {
10432 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
10433 Info.opc = ISD::INTRINSIC_W_CHAIN;
10434 Info.memVT = MVT::getVT(PtrTy->getElementType());
10435 Info.ptrVal = I.getArgOperand(0);
10436 Info.offset = 0;
10437 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10438 Info.vol = true;
10439 Info.readMem = true;
10440 Info.writeMem = false;
10441 return true;
10442 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010443 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000010444 case Intrinsic::arm_strex: {
10445 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
10446 Info.opc = ISD::INTRINSIC_W_CHAIN;
10447 Info.memVT = MVT::getVT(PtrTy->getElementType());
10448 Info.ptrVal = I.getArgOperand(1);
10449 Info.offset = 0;
10450 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10451 Info.vol = true;
10452 Info.readMem = false;
10453 Info.writeMem = true;
10454 return true;
10455 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010456 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010457 case Intrinsic::arm_strexd: {
10458 Info.opc = ISD::INTRINSIC_W_CHAIN;
10459 Info.memVT = MVT::i64;
10460 Info.ptrVal = I.getArgOperand(2);
10461 Info.offset = 0;
10462 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000010463 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010464 Info.readMem = false;
10465 Info.writeMem = true;
10466 return true;
10467 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010468 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010469 case Intrinsic::arm_ldrexd: {
10470 Info.opc = ISD::INTRINSIC_W_CHAIN;
10471 Info.memVT = MVT::i64;
10472 Info.ptrVal = I.getArgOperand(0);
10473 Info.offset = 0;
10474 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000010475 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010476 Info.readMem = true;
10477 Info.writeMem = false;
10478 return true;
10479 }
Bob Wilson5549d492010-09-21 17:56:22 +000010480 default:
10481 break;
10482 }
10483
10484 return false;
10485}
Juergen Ributzka659ce002014-01-28 01:20:14 +000010486
10487/// \brief Returns true if it is beneficial to convert a load of a constant
10488/// to just the constant itself.
10489bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
10490 Type *Ty) const {
10491 assert(Ty->isIntegerTy());
10492
10493 unsigned Bits = Ty->getPrimitiveSizeInBits();
10494 if (Bits == 0 || Bits > 32)
10495 return false;
10496 return true;
10497}
Tim Northover037f26f22014-04-17 18:22:47 +000010498
10499bool ARMTargetLowering::shouldExpandAtomicInIR(Instruction *Inst) const {
10500 // Loads and stores less than 64-bits are already atomic; ones above that
10501 // are doomed anyway, so defer to the default libcall and blame the OS when
10502 // things go wrong:
10503 if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
10504 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() == 64;
10505 else if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
10506 return LI->getType()->getPrimitiveSizeInBits() == 64;
10507
10508 // For the real atomic operations, we have ldrex/strex up to 64 bits.
10509 return Inst->getType()->getPrimitiveSizeInBits() <= 64;
10510}
10511
10512Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
10513 AtomicOrdering Ord) const {
10514 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
10515 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
10516 bool IsAcquire =
10517 Ord == Acquire || Ord == AcquireRelease || Ord == SequentiallyConsistent;
10518
10519 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
10520 // intrinsic must return {i32, i32} and we have to recombine them into a
10521 // single i64 here.
10522 if (ValTy->getPrimitiveSizeInBits() == 64) {
10523 Intrinsic::ID Int =
10524 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
10525 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
10526
10527 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
10528 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
10529
10530 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
10531 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
10532 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
10533 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
10534 return Builder.CreateOr(
10535 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
10536 }
10537
10538 Type *Tys[] = { Addr->getType() };
10539 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
10540 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
10541
10542 return Builder.CreateTruncOrBitCast(
10543 Builder.CreateCall(Ldrex, Addr),
10544 cast<PointerType>(Addr->getType())->getElementType());
10545}
10546
10547Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
10548 Value *Addr,
10549 AtomicOrdering Ord) const {
10550 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
10551 bool IsRelease =
10552 Ord == Release || Ord == AcquireRelease || Ord == SequentiallyConsistent;
10553
10554 // Since the intrinsics must have legal type, the i64 intrinsics take two
10555 // parameters: "i32, i32". We must marshal Val into the appropriate form
10556 // before the call.
10557 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
10558 Intrinsic::ID Int =
10559 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
10560 Function *Strex = Intrinsic::getDeclaration(M, Int);
10561 Type *Int32Ty = Type::getInt32Ty(M->getContext());
10562
10563 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
10564 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
10565 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
10566 return Builder.CreateCall3(Strex, Lo, Hi, Addr);
10567 }
10568
10569 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
10570 Type *Tys[] = { Addr->getType() };
10571 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
10572
10573 return Builder.CreateCall2(
10574 Strex, Builder.CreateZExtOrBitCast(
10575 Val, Strex->getFunctionType()->getParamType(0)),
10576 Addr);
10577}