blob: 2a68a671fb811bf3c94118b49f0265af7bfd23a0 [file] [log] [blame]
Evan Cheng10043e22007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Cheng10043e22007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Craig Topper188ed9d2012-03-17 07:33:42 +000015#include "ARMISelLowering.h"
Eric Christopher1c069172010-09-10 22:42:06 +000016#include "ARMCallingConv.h"
Evan Cheng10043e22007-01-19 07:51:42 +000017#include "ARMConstantPoolValue.h"
Evan Cheng10043e22007-01-19 07:51:42 +000018#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov9a232f42009-08-21 12:41:24 +000019#include "ARMPerfectShuffle.h"
Evan Cheng10043e22007-01-19 07:51:42 +000020#include "ARMSubtarget.h"
21#include "ARMTargetMachine.h"
Chris Lattner4e7dfaf2009-08-02 00:34:36 +000022#include "ARMTargetObjectFile.h"
Evan Chenga20cde32011-07-20 23:34:39 +000023#include "MCTargetDesc/ARMAddressingModes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/ADT/Statistic.h"
25#include "llvm/ADT/StringExtras.h"
Bob Wilsona4c22902009-04-17 19:07:39 +000026#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng078b0b02011-01-08 01:24:27 +000027#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Cheng10043e22007-01-19 07:51:42 +000028#include "llvm/CodeGen/MachineBasicBlock.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Bill Wendling202803e2011-10-05 00:02:33 +000032#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng10043e22007-01-19 07:51:42 +000034#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/CallingConv.h"
36#include "llvm/IR/Constants.h"
37#include "llvm/IR/Function.h"
38#include "llvm/IR/GlobalValue.h"
Tim Northover037f26f22014-04-17 18:22:47 +000039#include "llvm/IR/IRBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000040#include "llvm/IR/Instruction.h"
41#include "llvm/IR/Instructions.h"
42#include "llvm/IR/Intrinsics.h"
43#include "llvm/IR/Type.h"
Bill Wendling46ffefc2010-03-09 02:46:12 +000044#include "llvm/MC/MCSectionMachO.h"
Jim Grosbach32bb3622010-04-14 22:28:31 +000045#include "llvm/Support/CommandLine.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000046#include "llvm/Support/ErrorHandling.h"
Evan Cheng2150b922007-03-12 23:30:29 +000047#include "llvm/Support/MathExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000048#include "llvm/Target/TargetOptions.h"
David Peixottoc32e24a2013-10-17 19:49:22 +000049#include <utility>
Evan Cheng10043e22007-01-19 07:51:42 +000050using namespace llvm;
51
Chandler Carruth84e68b22014-04-22 02:41:26 +000052#define DEBUG_TYPE "arm-isel"
53
Dale Johannesend679ff72010-06-03 21:09:53 +000054STATISTIC(NumTailCalls, "Number of tail calls");
Evan Cheng68aec142011-01-19 02:16:49 +000055STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Manman Ren9f911162012-06-01 02:44:42 +000056STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
Dale Johannesend679ff72010-06-03 21:09:53 +000057
Eric Christopher347f4c32010-12-15 23:47:29 +000058cl::opt<bool>
Jim Grosbach32bb3622010-04-14 22:28:31 +000059EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng25f93642010-07-08 02:08:50 +000060 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbach32bb3622010-04-14 22:28:31 +000061 cl::init(false));
62
Evan Chengf128bdc2010-06-16 07:35:02 +000063static cl::opt<bool>
64ARMInterworking("arm-interworking", cl::Hidden,
65 cl::desc("Enable / disable ARM interworking (for debugging only)"),
66 cl::init(true));
67
Benjamin Kramer7ba71be2011-11-26 23:01:57 +000068namespace {
Cameron Zwarich89019782011-06-10 20:59:24 +000069 class ARMCCState : public CCState {
70 public:
71 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
Craig Topperb94011f2013-07-14 04:42:23 +000072 const TargetMachine &TM, SmallVectorImpl<CCValAssign> &locs,
Cameron Zwarich89019782011-06-10 20:59:24 +000073 LLVMContext &C, ParmContext PC)
74 : CCState(CC, isVarArg, MF, TM, locs, C) {
75 assert(((PC == Call) || (PC == Prologue)) &&
76 "ARMCCState users must specify whether their context is call"
77 "or prologue generation.");
78 CallOrPrologue = PC;
79 }
80 };
81}
82
Stuart Hastings45fe3c32011-04-20 16:47:52 +000083// The APCS parameter registers.
Craig Topper840beec2014-04-04 05:16:06 +000084static const MCPhysReg GPRArgRegs[] = {
Stuart Hastings45fe3c32011-04-20 16:47:52 +000085 ARM::R0, ARM::R1, ARM::R2, ARM::R3
86};
87
Craig Topper4fa625f2012-08-12 03:16:37 +000088void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT,
89 MVT PromotedBitwiseVT) {
Bob Wilson2e076c42009-06-22 23:27:02 +000090 if (VT != PromotedLdStVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +000091 setOperationAction(ISD::LOAD, VT, Promote);
92 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000093
Craig Topper4fa625f2012-08-12 03:16:37 +000094 setOperationAction(ISD::STORE, VT, Promote);
95 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000096 }
97
Craig Topper4fa625f2012-08-12 03:16:37 +000098 MVT ElemTy = VT.getVectorElementType();
Owen Anderson9f944592009-08-11 20:47:22 +000099 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Craig Topper4fa625f2012-08-12 03:16:37 +0000100 setOperationAction(ISD::SETCC, VT, Custom);
101 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
102 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000103 if (ElemTy == MVT::i32) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000104 setOperationAction(ISD::SINT_TO_FP, VT, Custom);
105 setOperationAction(ISD::UINT_TO_FP, VT, Custom);
106 setOperationAction(ISD::FP_TO_SINT, VT, Custom);
107 setOperationAction(ISD::FP_TO_UINT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000108 } else {
Craig Topper4fa625f2012-08-12 03:16:37 +0000109 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
110 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
111 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
112 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Bob Wilson5d8cfb22009-09-16 20:20:44 +0000113 }
Craig Topper4fa625f2012-08-12 03:16:37 +0000114 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
115 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
116 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal);
117 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
118 setOperationAction(ISD::SELECT, VT, Expand);
119 setOperationAction(ISD::SELECT_CC, VT, Expand);
Jim Grosbach30af4422012-10-12 22:59:21 +0000120 setOperationAction(ISD::VSELECT, VT, Expand);
Craig Topper4fa625f2012-08-12 03:16:37 +0000121 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000122 if (VT.isInteger()) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000123 setOperationAction(ISD::SHL, VT, Custom);
124 setOperationAction(ISD::SRA, VT, Custom);
125 setOperationAction(ISD::SRL, VT, Custom);
Bob Wilson2e076c42009-06-22 23:27:02 +0000126 }
127
128 // Promote all bit-wise operations.
129 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000130 setOperationAction(ISD::AND, VT, Promote);
131 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
132 setOperationAction(ISD::OR, VT, Promote);
133 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT);
134 setOperationAction(ISD::XOR, VT, Promote);
135 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
Bob Wilson2e076c42009-06-22 23:27:02 +0000136 }
Bob Wilson4ed397c2009-09-16 00:17:28 +0000137
138 // Neon does not support vector divide/remainder operations.
Craig Topper4fa625f2012-08-12 03:16:37 +0000139 setOperationAction(ISD::SDIV, VT, Expand);
140 setOperationAction(ISD::UDIV, VT, Expand);
141 setOperationAction(ISD::FDIV, VT, Expand);
142 setOperationAction(ISD::SREM, VT, Expand);
143 setOperationAction(ISD::UREM, VT, Expand);
144 setOperationAction(ISD::FREM, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000145}
146
Craig Topper4fa625f2012-08-12 03:16:37 +0000147void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topperc7242e02012-04-20 07:30:17 +0000148 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000149 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000150}
151
Craig Topper4fa625f2012-08-12 03:16:37 +0000152void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Jakob Stoklund Olesen20912062014-01-14 06:18:34 +0000153 addRegisterClass(VT, &ARM::DPairRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000154 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000155}
156
Chris Lattner5e693ed2009-07-28 03:13:23 +0000157static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
Tim Northoverd6a729b2014-01-06 14:28:05 +0000158 if (TM.getSubtarget<ARMSubtarget>().isTargetMachO())
Bill Wendlingbbcaa402010-03-15 21:09:38 +0000159 return new TargetLoweringObjectFileMachO();
Bill Wendling46ffefc2010-03-09 02:46:12 +0000160
Chris Lattner4e7dfaf2009-08-02 00:34:36 +0000161 return new ARMElfTargetObjectFile();
Chris Lattner5e693ed2009-07-28 03:13:23 +0000162}
163
Evan Cheng10043e22007-01-19 07:51:42 +0000164ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Cheng408aa562009-11-06 22:24:13 +0000165 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng10043e22007-01-19 07:51:42 +0000166 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Chengdf907f42010-07-23 22:39:59 +0000167 RegInfo = TM.getRegisterInfo();
Evan Chengbf407072010-09-10 01:29:16 +0000168 Itins = TM.getInstrItineraryData();
Evan Cheng10043e22007-01-19 07:51:42 +0000169
Duncan Sandsf2641e12011-09-06 19:07:46 +0000170 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
171
Tim Northoverd6a729b2014-01-06 14:28:05 +0000172 if (Subtarget->isTargetMachO()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000173 // Uses VFP for Thumb libfuncs if available.
Jim Grosbach1d1d6d42013-10-24 23:07:11 +0000174 if (Subtarget->isThumb() && Subtarget->hasVFP2() &&
175 Subtarget->hasARMOps()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000176 // Single-precision floating-point arithmetic.
177 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
178 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
179 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
180 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000181
Evan Chengc9f22fd12007-04-27 08:15:43 +0000182 // Double-precision floating-point arithmetic.
183 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
184 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
185 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
186 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng143576d2007-01-31 09:30:58 +0000187
Evan Chengc9f22fd12007-04-27 08:15:43 +0000188 // Single-precision comparisons.
189 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
190 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
191 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
192 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
193 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
194 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
195 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
196 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000197
Evan Chengc9f22fd12007-04-27 08:15:43 +0000198 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
199 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
200 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
201 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
202 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
203 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
204 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
205 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng143576d2007-01-31 09:30:58 +0000206
Evan Chengc9f22fd12007-04-27 08:15:43 +0000207 // Double-precision comparisons.
208 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
209 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
210 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
211 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
212 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
213 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
214 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
215 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000216
Evan Chengc9f22fd12007-04-27 08:15:43 +0000217 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
218 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
219 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
220 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
221 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
222 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
223 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
224 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Cheng10043e22007-01-19 07:51:42 +0000225
Evan Chengc9f22fd12007-04-27 08:15:43 +0000226 // Floating-point to integer conversions.
227 // i64 conversions are done via library routines even when generating VFP
228 // instructions, so use the same ones.
229 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
230 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
231 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
232 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000233
Evan Chengc9f22fd12007-04-27 08:15:43 +0000234 // Conversions between floating types.
235 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
236 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
237
238 // Integer to floating-point conversions.
239 // i64 conversions are done via library routines even when generating VFP
240 // instructions, so use the same ones.
Bob Wilsondc40d5a2009-03-20 23:16:43 +0000241 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
242 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengc9f22fd12007-04-27 08:15:43 +0000243 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
244 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
245 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
246 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
247 }
Evan Cheng10043e22007-01-19 07:51:42 +0000248 }
249
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000250 // These libcalls are not available in 32-bit.
251 setLibcallName(RTLIB::SHL_I128, 0);
252 setLibcallName(RTLIB::SRL_I128, 0);
253 setLibcallName(RTLIB::SRA_I128, 0);
254
Saleem Abdulrasoolcd130822014-04-02 20:32:05 +0000255 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetMachO() &&
256 !Subtarget->isTargetWindows()) {
Wesley Peck527da1b2010-11-23 03:31:01 +0000257 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000258 // RTABI chapter 4.1.2, Table 2
259 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
260 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
261 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
262 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
263 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
264 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
265 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
266 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
267
268 // Double-precision floating-point comparison helper functions
269 // RTABI chapter 4.1.2, Table 3
270 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
271 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
272 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
273 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
274 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
275 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
276 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
277 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
278 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
279 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
280 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
281 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
282 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
283 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
284 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
285 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
286 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
287 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
288 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
289 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
290 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
291 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
292 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
293 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
294
295 // Single-precision floating-point arithmetic helper functions
296 // RTABI chapter 4.1.2, Table 4
297 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
298 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
299 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
300 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
301 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
302 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
303 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
304 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
305
306 // Single-precision floating-point comparison helper functions
307 // RTABI chapter 4.1.2, Table 5
308 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
309 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
310 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
311 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
312 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
313 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
314 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
315 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
316 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
317 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
318 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
319 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
320 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
321 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
322 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
323 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
324 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
325 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
326 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
327 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
328 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
329 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
330 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
331 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
332
333 // Floating-point to integer conversions.
334 // RTABI chapter 4.1.2, Table 6
335 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
336 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
337 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
338 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
339 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
340 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
341 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
342 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
343 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
344 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
345 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
346 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
347 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
348 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
349 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
350 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
351
352 // Conversions between floating types.
353 // RTABI chapter 4.1.2, Table 7
354 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
355 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
356 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peck527da1b2010-11-23 03:31:01 +0000357 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000358
359 // Integer to floating-point conversions.
360 // RTABI chapter 4.1.2, Table 8
361 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
362 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
363 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
364 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
365 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
366 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
367 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
368 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
369 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
370 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
371 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
372 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
373 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
374 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
375 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
376 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
377
378 // Long long helper functions
379 // RTABI chapter 4.2, Table 9
380 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000381 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
382 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
383 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
384 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
385 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
386 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
387 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
388 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
389 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
390
391 // Integer division functions
392 // RTABI chapter 4.3.1
393 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
394 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
395 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
Anton Korobeynikovd0c46552012-01-29 09:11:50 +0000396 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000397 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
398 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
399 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
Anton Korobeynikovd0c46552012-01-29 09:11:50 +0000400 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000401 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
402 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
403 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikovd0c46552012-01-29 09:11:50 +0000404 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000405 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
406 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peck527da1b2010-11-23 03:31:01 +0000407 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikovd0c46552012-01-29 09:11:50 +0000408 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
Renato Golin4cd51872011-05-22 21:41:23 +0000409
410 // Memory operations
411 // RTABI chapter 4.3.4
412 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
413 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
414 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikovd0c46552012-01-29 09:11:50 +0000415 setLibcallCallingConv(RTLIB::MEMCPY, CallingConv::ARM_AAPCS);
416 setLibcallCallingConv(RTLIB::MEMMOVE, CallingConv::ARM_AAPCS);
417 setLibcallCallingConv(RTLIB::MEMSET, CallingConv::ARM_AAPCS);
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +0000418 }
419
Bob Wilsonbc158992011-10-07 16:59:21 +0000420 // Use divmod compiler-rt calls for iOS 5.0 and later.
Cameron Esfahani943908b2013-08-29 20:23:14 +0000421 if (Subtarget->getTargetTriple().isiOS() &&
Bob Wilsonbc158992011-10-07 16:59:21 +0000422 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
423 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
424 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
425 }
426
David Goodwin22c2fba2009-07-08 23:10:31 +0000427 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000428 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000429 else
Craig Topperc7242e02012-04-20 07:30:17 +0000430 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000431 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
432 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000433 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Jim Grosbach4d5dc3e2010-08-11 15:44:15 +0000434 if (!Subtarget->isFPOnlySP())
Craig Topperc7242e02012-04-20 07:30:17 +0000435 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Bob Wilson7117a912009-03-20 22:42:55 +0000436
Owen Anderson9f944592009-08-11 20:47:22 +0000437 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000438 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000439
Eli Friedman6f84fed2011-11-08 01:43:53 +0000440 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
441 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
442 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
443 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
444 setTruncStoreAction((MVT::SimpleValueType)VT,
445 (MVT::SimpleValueType)InnerVT, Expand);
446 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
447 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
448 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
449 }
450
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000451 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000452 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000453
Bob Wilson2e076c42009-06-22 23:27:02 +0000454 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000455 addDRTypeForNEON(MVT::v2f32);
456 addDRTypeForNEON(MVT::v8i8);
457 addDRTypeForNEON(MVT::v4i16);
458 addDRTypeForNEON(MVT::v2i32);
459 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000460
Owen Anderson9f944592009-08-11 20:47:22 +0000461 addQRTypeForNEON(MVT::v4f32);
462 addQRTypeForNEON(MVT::v2f64);
463 addQRTypeForNEON(MVT::v16i8);
464 addQRTypeForNEON(MVT::v8i16);
465 addQRTypeForNEON(MVT::v4i32);
466 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000467
Bob Wilson194a2512009-09-15 23:55:57 +0000468 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
469 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000470 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
471 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000472 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
473 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
474 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000475 // FIXME: Code duplication: FDIV and FREM are expanded always, see
476 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000477 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
478 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000479 // FIXME: Create unittest.
480 // In another words, find a way when "copysign" appears in DAG with vector
481 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000482 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000483 // FIXME: Code duplication: SETCC has custom operation action, see
484 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000485 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000486 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000487 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
488 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
489 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
490 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
491 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
492 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
493 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
494 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
495 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
496 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
497 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
498 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000499 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000500 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
501 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
502 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
503 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
504 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000505 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000506
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000507 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
508 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
509 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
510 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
511 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
512 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
513 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
514 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
515 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
516 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000517 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
518 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
519 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
520 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000521 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000522
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000523 // Mark v2f32 intrinsics.
524 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
525 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
526 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
527 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
528 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
529 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
530 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
531 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
532 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
533 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
534 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
535 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
536 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
537 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
538 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
539
Bob Wilson6cc46572009-09-16 00:32:15 +0000540 // Neon does not support some operations on v1i64 and v2i64 types.
541 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000542 // Custom handling for some quad-vector types to detect VMULL.
543 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
544 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
545 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000546 // Custom handling for some vector types to avoid expensive expansions
547 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
548 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
549 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
550 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000551 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
552 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000553 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000554 // a destination type that is wider than the source, and nor does
555 // it have a FP_TO_[SU]INT instruction with a narrower destination than
556 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000557 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
558 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000559 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
560 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000561
Eli Friedmane6385e62012-11-15 22:44:27 +0000562 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000563 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000564
Evan Chengb4eae132012-12-04 22:41:50 +0000565 // NEON does not have single instruction CTPOP for vectors with element
566 // types wider than 8-bits. However, custom lowering can leverage the
567 // v8i8/v16i8 vcnt instruction.
568 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
569 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
570 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
571 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
572
Jim Grosbach5f215872013-02-27 21:31:12 +0000573 // NEON only has FMA instructions as of VFP4.
574 if (!Subtarget->hasVFP4()) {
575 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
576 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
577 }
578
Bob Wilson06fce872011-02-07 17:43:21 +0000579 setTargetDAGCombine(ISD::INTRINSIC_VOID);
580 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000581 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
582 setTargetDAGCombine(ISD::SHL);
583 setTargetDAGCombine(ISD::SRL);
584 setTargetDAGCombine(ISD::SRA);
585 setTargetDAGCombine(ISD::SIGN_EXTEND);
586 setTargetDAGCombine(ISD::ZERO_EXTEND);
587 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsonc6c13a32010-02-18 06:05:53 +0000588 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000589 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000590 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000591 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
592 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000593 setTargetDAGCombine(ISD::FP_TO_SINT);
594 setTargetDAGCombine(ISD::FP_TO_UINT);
595 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem097106b2011-10-15 20:03:12 +0000596
James Molloy547d4c02012-02-20 09:24:05 +0000597 // It is legal to extload from v4i8 to v4i16 or v4i32.
598 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
599 MVT::v4i16, MVT::v2i16,
600 MVT::v2i32};
601 for (unsigned i = 0; i < 6; ++i) {
602 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
603 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
604 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
605 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000606 }
607
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000608 // ARM and Thumb2 support UMLAL/SMLAL.
609 if (!Subtarget->isThumb1Only())
610 setTargetDAGCombine(ISD::ADDC);
611
612
Evan Cheng6addd652007-05-18 00:19:34 +0000613 computeRegisterProperties();
Evan Cheng10043e22007-01-19 07:51:42 +0000614
615 // ARM does not have f32 extending load.
Owen Anderson9f944592009-08-11 20:47:22 +0000616 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000617
Duncan Sands95d46ef2008-01-23 20:39:46 +0000618 // ARM does not have i1 sign extending load.
Owen Anderson9f944592009-08-11 20:47:22 +0000619 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000620
Evan Cheng10043e22007-01-19 07:51:42 +0000621 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000622 if (!Subtarget->isThumb1Only()) {
623 for (unsigned im = (unsigned)ISD::PRE_INC;
624 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000625 setIndexedLoadAction(im, MVT::i1, Legal);
626 setIndexedLoadAction(im, MVT::i8, Legal);
627 setIndexedLoadAction(im, MVT::i16, Legal);
628 setIndexedLoadAction(im, MVT::i32, Legal);
629 setIndexedStoreAction(im, MVT::i1, Legal);
630 setIndexedStoreAction(im, MVT::i8, Legal);
631 setIndexedStoreAction(im, MVT::i16, Legal);
632 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000633 }
Evan Cheng10043e22007-01-19 07:51:42 +0000634 }
635
636 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000637 setOperationAction(ISD::MUL, MVT::i64, Expand);
638 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000639 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000640 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
641 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000642 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000643 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
644 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000645 setOperationAction(ISD::MULHS, MVT::i32, Expand);
646
Jim Grosbach5d994042009-10-31 19:38:01 +0000647 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000648 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000649 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000650 setOperationAction(ISD::SRL, MVT::i64, Custom);
651 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000652
Evan Chenge8916542011-08-30 01:34:54 +0000653 if (!Subtarget->isThumb1Only()) {
654 // FIXME: We should do this for Thumb1 as well.
655 setOperationAction(ISD::ADDC, MVT::i32, Custom);
656 setOperationAction(ISD::ADDE, MVT::i32, Custom);
657 setOperationAction(ISD::SUBC, MVT::i32, Custom);
658 setOperationAction(ISD::SUBE, MVT::i32, Custom);
659 }
660
Evan Cheng10043e22007-01-19 07:51:42 +0000661 // ARM does not have ROTL.
Owen Anderson9f944592009-08-11 20:47:22 +0000662 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach8546ec92010-01-18 19:58:49 +0000663 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000664 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000665 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000666 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000667
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000668 // These just redirect to CTTZ and CTLZ on ARM.
669 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
670 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
671
Tim Northoverbc933082013-05-23 19:11:20 +0000672 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
673
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000674 // Only ARMv6 has BSWAP.
675 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000676 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000677
Bob Wilsone8a549c2012-09-29 21:43:49 +0000678 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
679 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
680 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbach92d999002010-05-05 20:44:35 +0000681 setOperationAction(ISD::SDIV, MVT::i32, Expand);
682 setOperationAction(ISD::UDIV, MVT::i32, Expand);
683 }
Renato Golin87610692013-07-16 09:32:17 +0000684
685 // FIXME: Also set divmod for SREM on EABI
Owen Anderson9f944592009-08-11 20:47:22 +0000686 setOperationAction(ISD::SREM, MVT::i32, Expand);
687 setOperationAction(ISD::UREM, MVT::i32, Expand);
Renato Golin87610692013-07-16 09:32:17 +0000688 // Register based DivRem for AEABI (RTABI 4.2)
689 if (Subtarget->isTargetAEABI()) {
690 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
691 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
692 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
693 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
694 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
695 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
696 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
697 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
698
699 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
700 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
701 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
702 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
703 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
704 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
705 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
706 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
707
708 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
709 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
710 } else {
711 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
712 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
713 }
Bob Wilson7117a912009-03-20 22:42:55 +0000714
Owen Anderson9f944592009-08-11 20:47:22 +0000715 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
716 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
717 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
718 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000719 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000720
Evan Cheng74d92c12011-04-08 21:37:21 +0000721 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000722
Evan Cheng10043e22007-01-19 07:51:42 +0000723 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000724 setOperationAction(ISD::VASTART, MVT::Other, Custom);
725 setOperationAction(ISD::VAARG, MVT::Other, Expand);
726 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
727 setOperationAction(ISD::VAEND, MVT::Other, Expand);
728 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
729 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000730
Tim Northoverd6a729b2014-01-06 14:28:05 +0000731 if (!Subtarget->isTargetMachO()) {
732 // Non-MachO platforms may return values in these registers via the
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000733 // personality function.
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000734 setExceptionPointerRegister(ARM::R0);
735 setExceptionSelectorRegister(ARM::R1);
736 }
Anton Korobeynikovf3a62312011-01-24 22:38:45 +0000737
Evan Chengf7f97b42010-04-15 22:20:34 +0000738 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng6e809de2010-08-11 06:22:01 +0000739 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
740 // the default expansion.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000741 if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000742 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
743 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000744 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000745
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000746 // On v8, we have particularly efficient implementations of atomic fences
747 // if they can be combined with nearby atomic loads and stores.
748 if (!Subtarget->hasV8Ops()) {
749 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
750 setInsertFencesForAtomic(true);
751 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000752 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000753 // If there's anything we can use as a barrier, go through custom lowering
754 // for ATOMIC_FENCE.
755 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
756 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
757
Jim Grosbach6860bb72010-06-18 22:35:32 +0000758 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000759 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000760 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000761 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000762 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000763 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000764 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000765 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000766 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000767 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000768 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000769 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000770 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000771 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
772 // Unordered/Monotonic case.
773 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
774 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000775 }
Evan Cheng10043e22007-01-19 07:51:42 +0000776
Evan Cheng21acf9f2010-11-04 05:19:35 +0000777 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000778
Eli Friedman8cfa7712010-06-26 04:36:50 +0000779 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
780 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000781 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
782 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000783 }
Owen Anderson9f944592009-08-11 20:47:22 +0000784 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000785
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000786 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
787 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000788 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000789 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000790 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000791 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
792 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000793
794 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000795 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbach31984832010-07-07 00:07:57 +0000796 if (Subtarget->isTargetDarwin()) {
797 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
798 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall7d84ece2011-05-29 19:50:32 +0000799 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbach31984832010-07-07 00:07:57 +0000800 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000801
Owen Anderson9f944592009-08-11 20:47:22 +0000802 setOperationAction(ISD::SETCC, MVT::i32, Expand);
803 setOperationAction(ISD::SETCC, MVT::f32, Expand);
804 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000805 setOperationAction(ISD::SELECT, MVT::i32, Custom);
806 setOperationAction(ISD::SELECT, MVT::f32, Custom);
807 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000808 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
809 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
810 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000811
Owen Anderson9f944592009-08-11 20:47:22 +0000812 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
813 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
814 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
815 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
816 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000817
Dan Gohman482732a2007-10-11 23:21:31 +0000818 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000819 setOperationAction(ISD::FSIN, MVT::f64, Expand);
820 setOperationAction(ISD::FSIN, MVT::f32, Expand);
821 setOperationAction(ISD::FCOS, MVT::f32, Expand);
822 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000823 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
824 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000825 setOperationAction(ISD::FREM, MVT::f64, Expand);
826 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000827 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
828 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000829 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
830 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000831 }
Owen Anderson9f944592009-08-11 20:47:22 +0000832 setOperationAction(ISD::FPOW, MVT::f64, Expand);
833 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000834
Evan Chengd0007f32012-04-10 21:40:28 +0000835 if (!Subtarget->hasVFP4()) {
836 setOperationAction(ISD::FMA, MVT::f64, Expand);
837 setOperationAction(ISD::FMA, MVT::f32, Expand);
838 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000839
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000840 // Various VFP goodness
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000841 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilsone4191e72010-03-19 22:51:32 +0000842 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
843 if (Subtarget->hasVFP2()) {
844 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
845 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
846 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
847 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
848 }
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000849 // Special handling for half-precision FP.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000850 if (!Subtarget->hasFP16()) {
851 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
852 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000853 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000854 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000855
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000856 // Combine sin / cos into one node or libcall if possible.
857 if (Subtarget->hasSinCos()) {
858 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
859 setLibcallName(RTLIB::SINCOS_F64, "sincos");
860 if (Subtarget->getTargetTriple().getOS() == Triple::IOS) {
861 // For iOS, we don't want to the normal expansion of a libcall to
862 // sincos. We want to issue a libcall to __sincos_stret.
863 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
864 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
865 }
866 }
Evan Cheng10043e22007-01-19 07:51:42 +0000867
Chris Lattnerf3f4ad92007-11-27 22:36:16 +0000868 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000869 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +0000870 setTargetDAGCombine(ISD::ADD);
871 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +0000872 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +0000873 setTargetDAGCombine(ISD::AND);
874 setTargetDAGCombine(ISD::OR);
875 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +0000876
Evan Chengf258a152012-02-23 02:58:19 +0000877 if (Subtarget->hasV6Ops())
878 setTargetDAGCombine(ISD::SRL);
879
Evan Cheng10043e22007-01-19 07:51:42 +0000880 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +0000881
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000882 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
883 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +0000884 setSchedulingPreference(Sched::RegPressure);
885 else
886 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +0000887
Evan Cheng3ae2b792011-01-06 06:52:41 +0000888 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000889 MaxStoresPerMemset = 8;
890 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
891 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
892 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
893 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
894 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
Evan Chengb71233f2010-06-26 01:52:05 +0000895
Rafael Espindolaa76eccf2010-07-11 04:01:49 +0000896 // On ARM arguments smaller than 4 bytes are extended, so all arguments
897 // are at least 4 bytes aligned.
898 setMinStackArgumentAlignment(4);
899
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000900 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000901 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000902
Eli Friedman2518f832011-05-06 20:34:06 +0000903 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +0000904}
905
Andrew Trick43f25632011-01-19 02:35:27 +0000906// FIXME: It might make sense to define the representative register class as the
907// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
908// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
909// SPR's representative would be DPR_VFP2. This should work well if register
910// pressure tracking were modified such that a register use would increment the
911// pressure of the register class's representative and all of it's super
912// classes' representatives transitively. We have not implemented this because
913// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000914// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +0000915// and extractions.
Evan Chenga77f3d32010-07-21 06:09:07 +0000916std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000917ARMTargetLowering::findRepresentativeClass(MVT VT) const{
Evan Chenga77f3d32010-07-21 06:09:07 +0000918 const TargetRegisterClass *RRC = 0;
919 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000920 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +0000921 default:
Evan Chenga77f3d32010-07-21 06:09:07 +0000922 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng28590382010-07-21 23:53:58 +0000923 // Use DPR as representative register class for all floating point
924 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
925 // the cost is 1 for both f32 and f64.
926 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +0000927 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +0000928 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +0000929 // When NEON is used for SP, only half of the register file is available
930 // because operations that define both SP and DP results will be constrained
931 // to the VFP2 class (D0-D15). We currently model this constraint prior to
932 // coalescing by double-counting the SP regs. See the FIXME above.
933 if (Subtarget->useNEONForSinglePrecisionFP())
934 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000935 break;
936 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
937 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +0000938 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000939 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000940 break;
941 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +0000942 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000943 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +0000944 break;
945 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +0000946 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000947 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +0000948 break;
Evan Cheng10f99a32010-07-19 22:15:08 +0000949 }
Evan Chenga77f3d32010-07-21 06:09:07 +0000950 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +0000951}
952
Evan Cheng10043e22007-01-19 07:51:42 +0000953const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
954 switch (Opcode) {
955 default: return 0;
956 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +0000957 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +0000958 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
959 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +0000960 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +0000961 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
962 case ARMISD::tCALL: return "ARMISD::tCALL";
963 case ARMISD::BRCOND: return "ARMISD::BRCOND";
964 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +0000965 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +0000966 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +0000967 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +0000968 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
969 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +0000970 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +0000971 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +0000972 case ARMISD::CMPFP: return "ARMISD::CMPFP";
973 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +0000974 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +0000975 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +0000976
Evan Cheng10043e22007-01-19 07:51:42 +0000977 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +0000978
Jim Grosbach8546ec92010-01-18 19:58:49 +0000979 case ARMISD::RBIT: return "ARMISD::RBIT";
980
Bob Wilsone4191e72010-03-19 22:51:32 +0000981 case ARMISD::FTOSI: return "ARMISD::FTOSI";
982 case ARMISD::FTOUI: return "ARMISD::FTOUI";
983 case ARMISD::SITOF: return "ARMISD::SITOF";
984 case ARMISD::UITOF: return "ARMISD::UITOF";
985
Evan Cheng10043e22007-01-19 07:51:42 +0000986 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
987 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
988 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +0000989
Evan Chenge8916542011-08-30 01:34:54 +0000990 case ARMISD::ADDC: return "ARMISD::ADDC";
991 case ARMISD::ADDE: return "ARMISD::ADDE";
992 case ARMISD::SUBC: return "ARMISD::SUBC";
993 case ARMISD::SUBE: return "ARMISD::SUBE";
994
Bob Wilson22806742010-09-22 22:09:21 +0000995 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
996 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +0000997
Evan Chengec6d7c92009-10-28 06:55:03 +0000998 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
999 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
1000
Dale Johannesend679ff72010-06-03 21:09:53 +00001001 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001002
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001003 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001004
Evan Chengb972e562009-08-07 00:34:42 +00001005 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1006
Bob Wilson7ed59712010-10-30 00:54:37 +00001007 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001008
Evan Cheng8740ee32010-11-03 06:34:55 +00001009 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1010
Bob Wilson2e076c42009-06-22 23:27:02 +00001011 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001012 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001013 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001014 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1015 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001016 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1017 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001018 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1019 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001020 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1021 case ARMISD::VTST: return "ARMISD::VTST";
1022
1023 case ARMISD::VSHL: return "ARMISD::VSHL";
1024 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1025 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001026 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1027 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1028 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1029 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1030 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1031 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1032 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1033 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1034 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1035 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1036 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1037 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
1038 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1039 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001040 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001041 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001042 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001043 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001044 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001045 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001046 case ARMISD::VREV64: return "ARMISD::VREV64";
1047 case ARMISD::VREV32: return "ARMISD::VREV32";
1048 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001049 case ARMISD::VZIP: return "ARMISD::VZIP";
1050 case ARMISD::VUZP: return "ARMISD::VUZP";
1051 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001052 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1053 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001054 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1055 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001056 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1057 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001058 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilsonc6c13a32010-02-18 06:05:53 +00001059 case ARMISD::FMAX: return "ARMISD::FMAX";
1060 case ARMISD::FMIN: return "ARMISD::FMIN";
Joey Goulye3dd6842013-08-23 12:01:13 +00001061 case ARMISD::VMAXNM: return "ARMISD::VMAX";
1062 case ARMISD::VMINNM: return "ARMISD::VMIN";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001063 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001064 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1065 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001066 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilson2d790df2010-11-28 06:51:26 +00001067 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1068 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1069 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001070 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1071 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1072 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1073 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1074 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1075 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1076 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1077 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1078 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1079 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1080 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1081 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1082 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1083 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1084 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1085 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1086 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001087 }
1088}
1089
Matt Arsenault758659232013-05-18 00:21:46 +00001090EVT ARMTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Duncan Sandsf2641e12011-09-06 19:07:46 +00001091 if (!VT.isVector()) return getPointerTy();
1092 return VT.changeVectorElementTypeToInteger();
1093}
1094
Evan Cheng4cad68e2010-05-15 02:18:07 +00001095/// getRegClassFor - Return the register class that should be used for the
1096/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001097const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001098 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1099 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1100 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001101 if (Subtarget->hasNEON()) {
1102 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001103 return &ARM::QQPRRegClass;
1104 if (VT == MVT::v8i64)
1105 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001106 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001107 return TargetLowering::getRegClassFor(VT);
1108}
1109
Eric Christopher84bdfd82010-07-21 22:26:11 +00001110// Create a fast isel object.
1111FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001112ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1113 const TargetLibraryInfo *libInfo) const {
1114 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001115}
1116
Anton Korobeynikov19edda02010-07-24 21:52:08 +00001117/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1118/// be used for loads / stores from the global.
1119unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1120 return (Subtarget->isThumb1Only() ? 127 : 4095);
1121}
1122
Evan Cheng4401f882010-05-20 23:26:43 +00001123Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001124 unsigned NumVals = N->getNumValues();
1125 if (!NumVals)
1126 return Sched::RegPressure;
1127
1128 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001129 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001130 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001131 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001132 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001133 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001134 }
Evan Chengbf914992010-05-28 23:25:23 +00001135
1136 if (!N->isMachineOpcode())
1137 return Sched::RegPressure;
1138
1139 // Load are scheduled for latency even if there instruction itinerary
1140 // is not available.
1141 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001142 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001143
Evan Cheng6cc775f2011-06-28 19:10:37 +00001144 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001145 return Sched::RegPressure;
1146 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001147 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001148 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001149
Evan Cheng4401f882010-05-20 23:26:43 +00001150 return Sched::RegPressure;
1151}
1152
Evan Cheng10043e22007-01-19 07:51:42 +00001153//===----------------------------------------------------------------------===//
1154// Lowering Code
1155//===----------------------------------------------------------------------===//
1156
Evan Cheng10043e22007-01-19 07:51:42 +00001157/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1158static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1159 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001160 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001161 case ISD::SETNE: return ARMCC::NE;
1162 case ISD::SETEQ: return ARMCC::EQ;
1163 case ISD::SETGT: return ARMCC::GT;
1164 case ISD::SETGE: return ARMCC::GE;
1165 case ISD::SETLT: return ARMCC::LT;
1166 case ISD::SETLE: return ARMCC::LE;
1167 case ISD::SETUGT: return ARMCC::HI;
1168 case ISD::SETUGE: return ARMCC::HS;
1169 case ISD::SETULT: return ARMCC::LO;
1170 case ISD::SETULE: return ARMCC::LS;
1171 }
1172}
1173
Bob Wilsona2e83332009-09-09 23:14:54 +00001174/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1175static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001176 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001177 CondCode2 = ARMCC::AL;
1178 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001179 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001180 case ISD::SETEQ:
1181 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1182 case ISD::SETGT:
1183 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1184 case ISD::SETGE:
1185 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1186 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001187 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001188 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1189 case ISD::SETO: CondCode = ARMCC::VC; break;
1190 case ISD::SETUO: CondCode = ARMCC::VS; break;
1191 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1192 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1193 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1194 case ISD::SETLT:
1195 case ISD::SETULT: CondCode = ARMCC::LT; break;
1196 case ISD::SETLE:
1197 case ISD::SETULE: CondCode = ARMCC::LE; break;
1198 case ISD::SETNE:
1199 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1200 }
Evan Cheng10043e22007-01-19 07:51:42 +00001201}
1202
Bob Wilsona4c22902009-04-17 19:07:39 +00001203//===----------------------------------------------------------------------===//
1204// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001205//===----------------------------------------------------------------------===//
1206
1207#include "ARMGenCallingConv.inc"
1208
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001209/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1210/// given CallingConvention value.
Sandeep Patel68c5f472009-09-02 08:44:58 +00001211CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001212 bool Return,
1213 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001214 switch (CC) {
1215 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001216 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001217 case CallingConv::Fast:
Evan Cheng817bbac2010-10-23 02:19:37 +00001218 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng08dd8c82010-10-22 18:23:05 +00001219 if (!Subtarget->isAAPCS_ABI())
1220 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1221 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1222 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1223 }
1224 // Fallthrough
1225 case CallingConv::C: {
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001226 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng08dd8c82010-10-22 18:23:05 +00001227 if (!Subtarget->isAAPCS_ABI())
1228 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1229 else if (Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001230 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1231 !isVarArg)
Evan Cheng08dd8c82010-10-22 18:23:05 +00001232 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1233 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1234 }
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001235 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikov1b42e642012-01-29 09:06:09 +00001236 if (!isVarArg)
1237 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1238 // Fallthrough
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001239 case CallingConv::ARM_AAPCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001240 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001241 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001242 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001243 case CallingConv::GHC:
1244 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001245 }
1246}
1247
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001248/// LowerCallResult - Lower the result values of a call into the
1249/// appropriate copies out of appropriate physical registers.
1250SDValue
1251ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001252 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001253 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001254 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001255 SmallVectorImpl<SDValue> &InVals,
1256 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001257
Bob Wilsona4c22902009-04-17 19:07:39 +00001258 // Assign locations to each value returned by this call.
1259 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00001260 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1261 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001262 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001263 CCAssignFnForNode(CallConv, /* Return*/ true,
1264 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001265
1266 // Copy all of the result registers out of their specified physreg.
1267 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1268 CCValAssign VA = RVLocs[i];
1269
Stephen Linb8bd2322013-04-20 05:14:40 +00001270 // Pass 'this' value directly from the argument to return value, to avoid
1271 // reg unit interference
1272 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001273 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1274 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001275 InVals.push_back(ThisVal);
1276 continue;
1277 }
1278
Bob Wilson0041bd32009-04-25 00:33:20 +00001279 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001280 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001281 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001282 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001283 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001284 Chain = Lo.getValue(1);
1285 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001286 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001287 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001288 InFlag);
1289 Chain = Hi.getValue(1);
1290 InFlag = Hi.getValue(2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001291 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001292
Owen Anderson9f944592009-08-11 20:47:22 +00001293 if (VA.getLocVT() == MVT::v2f64) {
1294 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1295 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1296 DAG.getConstant(0, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001297
1298 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001299 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001300 Chain = Lo.getValue(1);
1301 InFlag = Lo.getValue(2);
1302 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001303 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001304 Chain = Hi.getValue(1);
1305 InFlag = Hi.getValue(2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001306 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001307 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1308 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001309 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001310 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001311 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1312 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001313 Chain = Val.getValue(1);
1314 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001315 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001316
1317 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001318 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001319 case CCValAssign::Full: break;
1320 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001321 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001322 break;
1323 }
1324
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001325 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001326 }
1327
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001328 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001329}
1330
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001331/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001332SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001333ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1334 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001335 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001336 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001337 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001338 unsigned LocMemOffset = VA.getLocMemOffset();
1339 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1340 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilsona4c22902009-04-17 19:07:39 +00001341 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner886250c2010-09-21 18:51:21 +00001342 MachinePointerInfo::getStack(LocMemOffset),
David Greene0d0149f2010-02-15 16:55:24 +00001343 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001344}
1345
Andrew Trickef9de2a2013-05-25 02:42:55 +00001346void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001347 SDValue Chain, SDValue &Arg,
1348 RegsToPassVector &RegsToPass,
1349 CCValAssign &VA, CCValAssign &NextVA,
1350 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001351 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001352 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001353
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001354 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001355 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson2e076c42009-06-22 23:27:02 +00001356 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1357
1358 if (NextVA.isRegLoc())
1359 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1360 else {
1361 assert(NextVA.isMemLoc());
1362 if (StackPtr.getNode() == 0)
1363 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1364
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001365 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1366 dl, DAG, NextVA,
1367 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001368 }
1369}
1370
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001371/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001372/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1373/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001374SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001375ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001376 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001377 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001378 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001379 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1380 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1381 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001382 SDValue Chain = CLI.Chain;
1383 SDValue Callee = CLI.Callee;
1384 bool &isTailCall = CLI.IsTailCall;
1385 CallingConv::ID CallConv = CLI.CallConv;
1386 bool doesNotRet = CLI.DoesNotReturn;
1387 bool isVarArg = CLI.IsVarArg;
1388
Dale Johannesend679ff72010-06-03 21:09:53 +00001389 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001390 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1391 bool isThisReturn = false;
1392 bool isSibCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001393
Bob Wilson8decdc42011-10-07 17:17:49 +00001394 // Disable tail calls if they're not supported.
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001395 if (!Subtarget->supportsTailCall() || MF.getTarget().Options.DisableTailCalls)
Bob Wilson3c9ed762010-08-13 22:43:33 +00001396 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001397
Dale Johannesend679ff72010-06-03 21:09:53 +00001398 if (isTailCall) {
1399 // Check if it's really possible to do a tail call.
1400 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001401 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001402 Outs, OutVals, Ins, DAG);
Dale Johannesend679ff72010-06-03 21:09:53 +00001403 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1404 // detected sibcalls.
1405 if (isTailCall) {
1406 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001407 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001408 }
1409 }
Evan Cheng10043e22007-01-19 07:51:42 +00001410
Bob Wilsona4c22902009-04-17 19:07:39 +00001411 // Analyze operands of the call, assigning locations to each operand.
1412 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00001413 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1414 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001415 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001416 CCAssignFnForNode(CallConv, /* Return*/ false,
1417 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001418
Bob Wilsona4c22902009-04-17 19:07:39 +00001419 // Get a count of how many bytes are to be pushed on the stack.
1420 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001421
Dale Johannesend679ff72010-06-03 21:09:53 +00001422 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001423 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001424 NumBytes = 0;
1425
Evan Cheng10043e22007-01-19 07:51:42 +00001426 // Adjust the stack pointer for the new arguments...
1427 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001428 if (!isSibCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00001429 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
1430 dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001431
Jim Grosbach6ad4bcb2010-02-24 01:43:03 +00001432 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Cheng10043e22007-01-19 07:51:42 +00001433
Bob Wilson2e076c42009-06-22 23:27:02 +00001434 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001435 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001436
Bob Wilsona4c22902009-04-17 19:07:39 +00001437 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001438 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001439 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1440 i != e;
1441 ++i, ++realArgIdx) {
1442 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001443 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001444 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001445 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001446
Bob Wilsona4c22902009-04-17 19:07:39 +00001447 // Promote the value if needed.
1448 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001449 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001450 case CCValAssign::Full: break;
1451 case CCValAssign::SExt:
1452 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1453 break;
1454 case CCValAssign::ZExt:
1455 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1456 break;
1457 case CCValAssign::AExt:
1458 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1459 break;
1460 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001461 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001462 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001463 }
1464
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001465 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001466 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001467 if (VA.getLocVT() == MVT::v2f64) {
1468 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1469 DAG.getConstant(0, MVT::i32));
1470 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1471 DAG.getConstant(1, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001472
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001473 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001474 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1475
1476 VA = ArgLocs[++i]; // skip ahead to next loc
1477 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001478 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001479 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1480 } else {
1481 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001482
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001483 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1484 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001485 }
1486 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001487 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001488 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001489 }
1490 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001491 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1492 assert(VA.getLocVT() == MVT::i32 &&
1493 "unexpected calling convention register assignment");
1494 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001495 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001496 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001497 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001498 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001499 } else if (isByVal) {
1500 assert(VA.isMemLoc());
1501 unsigned offset = 0;
1502
1503 // True if this byval aggregate will be split between registers
1504 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001505 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
1506 unsigned CurByValIdx = CCInfo.getInRegsParamsProceed();
1507
1508 if (CurByValIdx < ByValArgsCount) {
1509
1510 unsigned RegBegin, RegEnd;
1511 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1512
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001513 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1514 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001515 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001516 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1517 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1518 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1519 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001520 false, false, false,
1521 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001522 MemOpChains.push_back(Load.getValue(1));
1523 RegsToPass.push_back(std::make_pair(j, Load));
1524 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001525
1526 // If parameter size outsides register area, "offset" value
1527 // helps us to calculate stack slot for remained part properly.
1528 offset = RegEnd - RegBegin;
1529
1530 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001531 }
1532
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001533 if (Flags.getByValSize() > 4*offset) {
Manman Ren9f911162012-06-01 02:44:42 +00001534 unsigned LocMemOffset = VA.getLocMemOffset();
1535 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1536 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1537 StkPtrOff);
1538 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1539 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1540 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1541 MVT::i32);
Manman Rene8735522012-06-01 19:33:18 +00001542 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001543
Manman Ren9f911162012-06-01 02:44:42 +00001544 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001545 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001546 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
1547 Ops, array_lengthof(Ops)));
1548 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001549 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001550 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001551
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001552 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1553 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001554 }
Evan Cheng10043e22007-01-19 07:51:42 +00001555 }
1556
1557 if (!MemOpChains.empty())
Owen Anderson9f944592009-08-11 20:47:22 +00001558 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Cheng10043e22007-01-19 07:51:42 +00001559 &MemOpChains[0], MemOpChains.size());
1560
1561 // Build a sequence of copy-to-reg nodes chained together with token chain
1562 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001563 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001564 // Tail call byval lowering might overwrite argument registers so in case of
1565 // tail call optimization the copies to registers are lowered later.
1566 if (!isTailCall)
1567 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1568 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1569 RegsToPass[i].second, InFlag);
1570 InFlag = Chain.getValue(1);
1571 }
Evan Cheng10043e22007-01-19 07:51:42 +00001572
Dale Johannesend679ff72010-06-03 21:09:53 +00001573 // For tail calls lower the arguments to the 'real' stack slot.
1574 if (isTailCall) {
1575 // Force all the incoming stack arguments to be loaded from the stack
1576 // before any new outgoing arguments are stored to the stack, because the
1577 // outgoing stack slots may alias the incoming argument stack slots, and
1578 // the alias isn't otherwise explicit. This is slightly more conservative
1579 // than necessary, because it means that each store effectively depends
1580 // on every argument instead of just those arguments it would clobber.
1581
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001582 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001583 InFlag = SDValue();
1584 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1585 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1586 RegsToPass[i].second, InFlag);
1587 InFlag = Chain.getValue(1);
1588 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001589 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001590 }
1591
Bill Wendling24c79f22008-09-16 21:48:12 +00001592 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1593 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1594 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001595 bool isDirect = false;
1596 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001597 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001598 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001599
1600 if (EnableARMLongCalls) {
1601 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1602 && "long-calls with non-static relocation model!");
1603 // Handle a global address or an external symbol. If it's not one of
1604 // those, the target's already in a register, so we don't need to do
1605 // anything extra.
1606 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001607 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001608 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001609 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001610 ARMConstantPoolValue *CPV =
1611 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1612
Jim Grosbach32bb3622010-04-14 22:28:31 +00001613 // Get the address of the callee into a register
1614 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1615 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1616 Callee = DAG.getLoad(getPointerTy(), dl,
1617 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001618 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001619 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001620 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1621 const char *Sym = S->getSymbol();
1622
1623 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001624 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001625 ARMConstantPoolValue *CPV =
1626 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1627 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001628 // Get the address of the callee into a register
1629 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1630 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1631 Callee = DAG.getLoad(getPointerTy(), dl,
1632 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001633 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001634 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001635 }
1636 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001637 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001638 isDirect = true;
Chris Lattner55452c22009-07-15 04:12:33 +00001639 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Tim Northoverd6a729b2014-01-06 14:28:05 +00001640 bool isStub = (isExt && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001641 getTargetMachine().getRelocationModel() != Reloc::Static;
1642 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc3c949b42007-06-19 21:05:09 +00001643 // ARM call to a local ARM function is predicable.
Evan Chengf128bdc2010-06-16 07:35:02 +00001644 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001645 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001646 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001647 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Tim Northover72360d22013-12-02 10:35:41 +00001648 Callee = DAG.getNode(ARMISD::WrapperPIC, dl, getPointerTy(),
1649 DAG.getTargetGlobalAddress(GV, dl, getPointerTy()));
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001650 } else {
1651 // On ELF targets for PIC code, direct calls should go through the PLT
1652 unsigned OpFlags = 0;
1653 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001654 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001655 OpFlags = ARMII::MO_PLT;
1656 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1657 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001658 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001659 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001660 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001661 getTargetMachine().getRelocationModel() != Reloc::Static;
1662 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng83f35172007-01-30 20:37:08 +00001663 // tBX takes a register source operand.
1664 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001665 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001666 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001667 ARMConstantPoolValue *CPV =
1668 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1669 ARMPCLabelIndex, 4);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001670 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001671 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen021052a2009-02-04 20:06:27 +00001672 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Chengcdbb70c2009-10-31 03:39:36 +00001673 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001674 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001675 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00001676 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson7117a912009-03-20 22:42:55 +00001677 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen021052a2009-02-04 20:06:27 +00001678 getPointerTy(), Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001679 } else {
1680 unsigned OpFlags = 0;
1681 // On ELF targets for PIC code, direct calls should go through the PLT
1682 if (Subtarget->isTargetELF() &&
1683 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1684 OpFlags = ARMII::MO_PLT;
1685 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1686 }
Evan Cheng10043e22007-01-19 07:51:42 +00001687 }
1688
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001689 // FIXME: handle tail calls differently.
1690 unsigned CallOpc;
Tim Northoverdee86042013-12-02 14:46:26 +00001691 bool HasMinSizeAttr = Subtarget->isMinSize();
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001692 if (Subtarget->isThumb()) {
1693 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001694 CallOpc = ARMISD::CALL_NOLINK;
1695 else
1696 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1697 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001698 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001699 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001700 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet8e1fe842012-11-02 21:32:17 +00001701 // Emit regular call when code size is the priority
1702 !HasMinSizeAttr)
Evan Cheng65f9d192012-02-28 18:51:51 +00001703 // "mov lr, pc; b _foo" to avoid confusing the RSP
1704 CallOpc = ARMISD::CALL_NOLINK;
1705 else
1706 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001707 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001708
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001709 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001710 Ops.push_back(Chain);
1711 Ops.push_back(Callee);
1712
1713 // Add argument registers to the end of the list so that they are known live
1714 // into the call.
1715 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1716 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1717 RegsToPass[i].second.getValueType()));
1718
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001719 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001720 if (!isTailCall) {
1721 const uint32_t *Mask;
1722 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1723 const ARMBaseRegisterInfo *ARI = static_cast<const ARMBaseRegisterInfo*>(TRI);
1724 if (isThisReturn) {
1725 // For 'this' returns, use the R0-preserving mask if applicable
1726 Mask = ARI->getThisReturnPreservedMask(CallConv);
1727 if (!Mask) {
1728 // Set isThisReturn to false if the calling convention is not one that
1729 // allows 'returned' to be modeled in this way, so LowerCallResult does
1730 // not try to pass 'this' straight through
1731 isThisReturn = false;
1732 Mask = ARI->getCallPreservedMask(CallConv);
1733 }
1734 } else
Stephen Linff7fcee2013-06-26 21:42:14 +00001735 Mask = ARI->getCallPreservedMask(CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001736
Matthias Braunc22630e2013-10-04 16:52:54 +00001737 assert(Mask && "Missing call preserved mask for calling convention");
1738 Ops.push_back(DAG.getRegisterMask(Mask));
1739 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001740
Gabor Greiff304a7a2008-08-28 21:40:38 +00001741 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001742 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001743
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001744 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001745 if (isTailCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001746 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesend679ff72010-06-03 21:09:53 +00001747
Duncan Sands739a0542008-07-02 17:40:58 +00001748 // Returns a chain and a flag for retval copy to use.
Dale Johannesend679ff72010-06-03 21:09:53 +00001749 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng10043e22007-01-19 07:51:42 +00001750 InFlag = Chain.getValue(1);
1751
Chris Lattner27539552008-10-11 22:08:30 +00001752 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001753 DAG.getIntPtrConstant(0, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001754 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001755 InFlag = Chain.getValue(1);
1756
Bob Wilsona4c22902009-04-17 19:07:39 +00001757 // Handle result values, copying them out of physregs into vregs that we
1758 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001759 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001760 InVals, isThisReturn,
1761 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001762}
1763
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001764/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001765/// on the stack. Remember the next parameter register to allocate,
1766/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001767/// this.
1768void
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001769ARMTargetLowering::HandleByVal(
1770 CCState *State, unsigned &size, unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001771 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1772 assert((State->getCallOrPrologue() == Prologue ||
1773 State->getCallOrPrologue() == Call) &&
1774 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001775
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001776 if ((ARM::R0 <= reg) && (reg <= ARM::R3)) {
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001777 if (Subtarget->isAAPCS_ABI() && Align > 4) {
1778 unsigned AlignInRegs = Align / 4;
1779 unsigned Waste = (ARM::R4 - reg) % AlignInRegs;
1780 for (unsigned i = 0; i < Waste; ++i)
1781 reg = State->AllocateReg(GPRArgRegs, 4);
1782 }
1783 if (reg != 0) {
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001784 unsigned excess = 4 * (ARM::R4 - reg);
1785
1786 // Special case when NSAA != SP and parameter size greater than size of
1787 // all remained GPR regs. In that case we can't split parameter, we must
1788 // send it to stack. We also must set NCRN to R4, so waste all
1789 // remained registers.
Oliver Stannardd55e1152014-03-05 15:25:27 +00001790 const unsigned NSAAOffset = State->getNextStackOffset();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001791 if (Subtarget->isAAPCS_ABI() && NSAAOffset != 0 && size > excess) {
1792 while (State->AllocateReg(GPRArgRegs, 4))
1793 ;
1794 return;
1795 }
1796
1797 // First register for byval parameter is the first register that wasn't
1798 // allocated before this method call, so it would be "reg".
1799 // If parameter is small enough to be saved in range [reg, r4), then
1800 // the end (first after last) register would be reg + param-size-in-regs,
1801 // else parameter would be splitted between registers and stack,
1802 // end register would be r4 in this case.
1803 unsigned ByValRegBegin = reg;
Stepan Dyatkovskiy2703bca2013-05-08 14:51:27 +00001804 unsigned ByValRegEnd = (size < excess) ? reg + size/4 : (unsigned)ARM::R4;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001805 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
1806 // Note, first register is allocated in the beginning of function already,
1807 // allocate remained amount of registers we need.
1808 for (unsigned i = reg+1; i != ByValRegEnd; ++i)
1809 State->AllocateReg(GPRArgRegs, 4);
Oliver Stannardd55e1152014-03-05 15:25:27 +00001810 // A byval parameter that is split between registers and memory needs its
1811 // size truncated here.
1812 // In the case where the entire structure fits in registers, we set the
1813 // size in memory to zero.
1814 if (size < excess)
1815 size = 0;
1816 else
1817 size -= excess;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001818 }
1819 }
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001820}
1821
Dale Johannesend679ff72010-06-03 21:09:53 +00001822/// MatchingStackOffset - Return true if the given stack call argument is
1823/// already available in the same position (relatively) of the caller's
1824/// incoming argument stack.
1825static
1826bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1827 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00001828 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00001829 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1830 int FI = INT_MAX;
1831 if (Arg.getOpcode() == ISD::CopyFromReg) {
1832 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001833 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00001834 return false;
1835 MachineInstr *Def = MRI->getVRegDef(VR);
1836 if (!Def)
1837 return false;
1838 if (!Flags.isByVal()) {
1839 if (!TII->isLoadFromStackSlot(Def, FI))
1840 return false;
1841 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00001842 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00001843 }
1844 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1845 if (Flags.isByVal())
1846 // ByVal argument is passed in as a pointer but it's now being
1847 // dereferenced. e.g.
1848 // define @foo(%struct.X* %A) {
1849 // tail call @bar(%struct.X* byval %A)
1850 // }
1851 return false;
1852 SDValue Ptr = Ld->getBasePtr();
1853 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1854 if (!FINode)
1855 return false;
1856 FI = FINode->getIndex();
1857 } else
1858 return false;
1859
1860 assert(FI != INT_MAX);
1861 if (!MFI->isFixedObjectIndex(FI))
1862 return false;
1863 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1864}
1865
1866/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1867/// for tail call optimization. Targets which want to do tail call
1868/// optimization should implement this function.
1869bool
1870ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1871 CallingConv::ID CalleeCC,
1872 bool isVarArg,
1873 bool isCalleeStructRet,
1874 bool isCallerStructRet,
1875 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001876 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00001877 const SmallVectorImpl<ISD::InputArg> &Ins,
1878 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00001879 const Function *CallerF = DAG.getMachineFunction().getFunction();
1880 CallingConv::ID CallerCC = CallerF->getCallingConv();
1881 bool CCMatch = CallerCC == CalleeCC;
1882
1883 // Look for obvious safe cases to perform tail call optimization that do not
1884 // require ABI changes. This is what gcc calls sibcall.
1885
Jim Grosbache3864cc2010-06-16 23:45:49 +00001886 // Do not sibcall optimize vararg calls unless the call site is not passing
1887 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00001888 if (isVarArg && !Outs.empty())
1889 return false;
1890
Tim Northoverd8407452013-10-01 14:33:28 +00001891 // Exception-handling functions need a special set of instructions to indicate
1892 // a return to the hardware. Tail-calling another function would probably
1893 // break this.
1894 if (CallerF->hasFnAttribute("interrupt"))
1895 return false;
1896
Dale Johannesend679ff72010-06-03 21:09:53 +00001897 // Also avoid sibcall optimization if either caller or callee uses struct
1898 // return semantics.
1899 if (isCalleeStructRet || isCallerStructRet)
1900 return false;
1901
Dale Johannesend24c66b2010-06-23 18:52:34 +00001902 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach3840c902011-07-08 20:18:11 +00001903 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1904 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1905 // support in the assembler and linker to be used. This would need to be
1906 // fixed to fully support tail calls in Thumb1.
1907 //
Dale Johannesene2289282010-07-08 01:18:23 +00001908 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1909 // LR. This means if we need to reload LR, it takes an extra instructions,
1910 // which outweighs the value of the tail call; but here we don't know yet
1911 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach535d3b42010-09-08 03:54:02 +00001912 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesene2289282010-07-08 01:18:23 +00001913 // emitEpilogue if LR is used.
Dale Johannesene2289282010-07-08 01:18:23 +00001914
1915 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1916 // but we need to make sure there are enough registers; the only valid
1917 // registers are the 4 used for parameters. We don't currently do this
1918 // case.
Evan Chengd4b08732010-11-30 23:55:39 +00001919 if (Subtarget->isThumb1Only())
1920 return false;
Dale Johannesen3ac52b32010-06-18 18:13:11 +00001921
Dale Johannesend679ff72010-06-03 21:09:53 +00001922 // If the calling conventions do not match, then we'd better make sure the
1923 // results are returned in the same way as what the caller expects.
1924 if (!CCMatch) {
1925 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwarich89019782011-06-10 20:59:24 +00001926 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1927 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00001928 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1929
1930 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwarich89019782011-06-10 20:59:24 +00001931 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1932 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00001933 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1934
1935 if (RVLocs1.size() != RVLocs2.size())
1936 return false;
1937 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1938 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1939 return false;
1940 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1941 return false;
1942 if (RVLocs1[i].isRegLoc()) {
1943 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1944 return false;
1945 } else {
1946 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1947 return false;
1948 }
1949 }
1950 }
1951
Manman Ren7e48b252012-10-12 23:39:43 +00001952 // If Caller's vararg or byval argument has been split between registers and
1953 // stack, do not perform tail call, since part of the argument is in caller's
1954 // local frame.
1955 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
1956 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00001957 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00001958 return false;
1959
Dale Johannesend679ff72010-06-03 21:09:53 +00001960 // If the callee takes no arguments then go on to check the results of the
1961 // call.
1962 if (!Outs.empty()) {
1963 // Check if stack adjustment is needed. For now, do not do this if any
1964 // argument is passed on the stack.
1965 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00001966 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1967 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00001968 CCInfo.AnalyzeCallOperands(Outs,
1969 CCAssignFnForNode(CalleeCC, false, isVarArg));
1970 if (CCInfo.getNextStackOffset()) {
1971 MachineFunction &MF = DAG.getMachineFunction();
1972
1973 // Check if the arguments are already laid out in the right way as
1974 // the caller's fixed stack objects.
1975 MachineFrameInfo *MFI = MF.getFrameInfo();
1976 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topper07720d82012-03-25 23:49:58 +00001977 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001978 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1979 i != e;
1980 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00001981 CCValAssign &VA = ArgLocs[i];
1982 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001983 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001984 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00001985 if (VA.getLocInfo() == CCValAssign::Indirect)
1986 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001987 if (VA.needsCustom()) {
1988 // f64 and vector types are split into multiple registers or
1989 // register/stack-slot combinations. The types will not match
1990 // the registers; give up on memory f64 refs until we figure
1991 // out what to do about this.
1992 if (!VA.isRegLoc())
1993 return false;
1994 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00001995 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001996 if (RegVT == MVT::v2f64) {
1997 if (!ArgLocs[++i].isRegLoc())
1998 return false;
1999 if (!ArgLocs[++i].isRegLoc())
2000 return false;
2001 }
2002 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002003 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2004 MFI, MRI, TII))
2005 return false;
2006 }
2007 }
2008 }
2009 }
2010
2011 return true;
2012}
2013
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002014bool
2015ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2016 MachineFunction &MF, bool isVarArg,
2017 const SmallVectorImpl<ISD::OutputArg> &Outs,
2018 LLVMContext &Context) const {
2019 SmallVector<CCValAssign, 16> RVLocs;
2020 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), RVLocs, Context);
2021 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2022 isVarArg));
2023}
2024
Tim Northoverd8407452013-10-01 14:33:28 +00002025static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2026 SDLoc DL, SelectionDAG &DAG) {
2027 const MachineFunction &MF = DAG.getMachineFunction();
2028 const Function *F = MF.getFunction();
2029
2030 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2031
2032 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2033 // version of the "preferred return address". These offsets affect the return
2034 // instruction if this is a return from PL1 without hypervisor extensions.
2035 // IRQ/FIQ: +4 "subs pc, lr, #4"
2036 // SWI: 0 "subs pc, lr, #0"
2037 // ABORT: +4 "subs pc, lr, #4"
2038 // UNDEF: +4/+2 "subs pc, lr, #0"
2039 // UNDEF varies depending on where the exception came from ARM or Thumb
2040 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2041
2042 int64_t LROffset;
2043 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2044 IntKind == "ABORT")
2045 LROffset = 4;
2046 else if (IntKind == "SWI" || IntKind == "UNDEF")
2047 LROffset = 0;
2048 else
2049 report_fatal_error("Unsupported interrupt attribute. If present, value "
2050 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2051
2052 RetOps.insert(RetOps.begin() + 1, DAG.getConstant(LROffset, MVT::i32, false));
2053
2054 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other,
2055 RetOps.data(), RetOps.size());
2056}
2057
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002058SDValue
2059ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002060 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002061 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002062 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002063 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002064
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002065 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002066 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002067
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002068 // CCState - Info about the registers and stack slots.
Cameron Zwarich89019782011-06-10 20:59:24 +00002069 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2070 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002071
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002072 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002073 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2074 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002075
Bob Wilsona4c22902009-04-17 19:07:39 +00002076 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002077 SmallVector<SDValue, 4> RetOps;
2078 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Bob Wilsona4c22902009-04-17 19:07:39 +00002079
2080 // Copy the result values into the output registers.
2081 for (unsigned i = 0, realRVLocIdx = 0;
2082 i != RVLocs.size();
2083 ++i, ++realRVLocIdx) {
2084 CCValAssign &VA = RVLocs[i];
2085 assert(VA.isRegLoc() && "Can only return in registers!");
2086
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002087 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002088
2089 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002090 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002091 case CCValAssign::Full: break;
2092 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002093 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002094 break;
2095 }
2096
Bob Wilsona4c22902009-04-17 19:07:39 +00002097 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002098 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002099 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002100 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2101 DAG.getConstant(0, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002102 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002103 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002104
2105 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
2106 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002107 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002108 VA = RVLocs[++i]; // skip ahead to next loc
2109 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2110 HalfGPRs.getValue(1), Flag);
2111 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002112 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002113 VA = RVLocs[++i]; // skip ahead to next loc
2114
2115 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002116 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2117 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002118 }
2119 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2120 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002121 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002122 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilsona4c22902009-04-17 19:07:39 +00002123 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002124 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002125 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002126 VA = RVLocs[++i]; // skip ahead to next loc
2127 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
2128 Flag);
2129 } else
2130 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2131
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002132 // Guarantee that all emitted copies are
2133 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002134 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002135 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002136 }
2137
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002138 // Update chain and glue.
2139 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002140 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002141 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002142
Tim Northoverd8407452013-10-01 14:33:28 +00002143 // CPUs which aren't M-class use a special sequence to return from
2144 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2145 // though we use "subs pc, lr, #N").
2146 //
2147 // M-class CPUs actually use a normal return sequence with a special
2148 // (hardware-provided) value in LR, so the normal code path works.
2149 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2150 !Subtarget->isMClass()) {
2151 if (Subtarget->isThumb1Only())
2152 report_fatal_error("interrupt attribute is not supported in Thumb1");
2153 return LowerInterruptReturn(RetOps, dl, DAG);
2154 }
2155
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002156 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other,
2157 RetOps.data(), RetOps.size());
Evan Cheng10043e22007-01-19 07:51:42 +00002158}
2159
Evan Chengf8bad082012-04-10 01:51:00 +00002160bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002161 if (N->getNumValues() != 1)
2162 return false;
2163 if (!N->hasNUsesOfValue(1, 0))
2164 return false;
2165
Evan Chengf8bad082012-04-10 01:51:00 +00002166 SDValue TCChain = Chain;
2167 SDNode *Copy = *N->use_begin();
2168 if (Copy->getOpcode() == ISD::CopyToReg) {
2169 // If the copy has a glue operand, we conservatively assume it isn't safe to
2170 // perform a tail call.
2171 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2172 return false;
2173 TCChain = Copy->getOperand(0);
2174 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2175 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002176 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002177 SmallPtrSet<SDNode*, 2> Copies;
2178 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002179 UI != UE; ++UI) {
2180 if (UI->getOpcode() != ISD::CopyToReg)
2181 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002182 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002183 }
Evan Chengf8bad082012-04-10 01:51:00 +00002184 if (Copies.size() > 2)
2185 return false;
2186
2187 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2188 UI != UE; ++UI) {
2189 SDValue UseChain = UI->getOperand(0);
2190 if (Copies.count(UseChain.getNode()))
2191 // Second CopyToReg
2192 Copy = *UI;
2193 else
2194 // First CopyToReg
2195 TCChain = UseChain;
2196 }
2197 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002198 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002199 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002200 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002201 Copy = *Copy->use_begin();
2202 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002203 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002204 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002205 } else {
2206 return false;
2207 }
2208
Evan Cheng419ea282010-12-01 22:59:46 +00002209 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002210 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2211 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002212 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2213 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002214 return false;
2215 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002216 }
2217
Evan Chengf8bad082012-04-10 01:51:00 +00002218 if (!HasRet)
2219 return false;
2220
2221 Chain = TCChain;
2222 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002223}
2224
Evan Cheng0663f232011-03-21 01:19:09 +00002225bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002226 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002227 return false;
2228
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00002229 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng0663f232011-03-21 01:19:09 +00002230 return false;
2231
2232 return !Subtarget->isThumb1Only();
2233}
2234
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002235// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2236// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2237// one of the above mentioned nodes. It has to be wrapped because otherwise
2238// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2239// be used to form addressing mode. These wrapped nodes will be selected
2240// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002241static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002242 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002243 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002244 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002245 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002246 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002247 if (CP->isMachineConstantPoolEntry())
2248 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2249 CP->getAlignment());
2250 else
2251 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2252 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002253 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002254}
2255
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002256unsigned ARMTargetLowering::getJumpTableEncoding() const {
2257 return MachineJumpTableInfo::EK_Inline;
2258}
2259
Dan Gohman21cea8a2010-04-17 15:26:15 +00002260SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2261 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002262 MachineFunction &MF = DAG.getMachineFunction();
2263 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2264 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002265 SDLoc DL(Op);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002266 EVT PtrVT = getPointerTy();
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002267 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002268 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2269 SDValue CPAddr;
2270 if (RelocM == Reloc::Static) {
2271 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2272 } else {
2273 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002274 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002275 ARMConstantPoolValue *CPV =
2276 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2277 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002278 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2279 }
2280 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2281 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002282 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002283 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002284 if (RelocM == Reloc::Static)
2285 return Result;
Evan Cheng408aa562009-11-06 22:24:13 +00002286 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002287 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002288}
2289
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002290// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002291SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002292ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002293 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002294 SDLoc dl(GA);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002295 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002296 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002297 MachineFunction &MF = DAG.getMachineFunction();
2298 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002299 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002300 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002301 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2302 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002303 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002304 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002305 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattner7727d052010-09-21 06:44:06 +00002306 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002307 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002308 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002309
Evan Cheng408aa562009-11-06 22:24:13 +00002310 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002311 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002312
2313 // call __tls_get_addr.
2314 ArgListTy Args;
2315 ArgListEntry Entry;
2316 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002317 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002318 Args.push_back(Entry);
Dale Johannesen555a3752009-01-30 23:10:59 +00002319 // FIXME: is there useful debug info available here?
Justin Holewinskiaa583972012-05-25 16:35:28 +00002320 TargetLowering::CallLoweringInfo CLI(Chain,
2321 (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng09c070f2009-08-14 19:11:20 +00002322 false, false, false, false,
Evan Cheng65f9d192012-02-28 18:51:51 +00002323 0, CallingConv::C, /*isTailCall=*/false,
2324 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling78c5b7a2010-03-02 01:55:18 +00002325 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Justin Holewinskiaa583972012-05-25 16:35:28 +00002326 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002327 return CallResult.first;
2328}
2329
2330// Lower ISD::GlobalTLSAddress using the "initial exec" or
2331// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002332SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002333ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002334 SelectionDAG &DAG,
2335 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002336 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002337 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002338 SDValue Offset;
2339 SDValue Chain = DAG.getEntryNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002340 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002341 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002342 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002343
Hans Wennborgaea41202012-05-04 09:40:39 +00002344 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002345 MachineFunction &MF = DAG.getMachineFunction();
2346 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002347 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002348 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002349 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2350 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002351 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2352 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2353 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002354 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002355 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002356 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002357 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002358 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002359 Chain = Offset.getValue(1);
2360
Evan Cheng408aa562009-11-06 22:24:13 +00002361 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002362 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002363
Evan Chengcdbb70c2009-10-31 03:39:36 +00002364 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002365 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002366 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002367 } else {
2368 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002369 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002370 ARMConstantPoolValue *CPV =
2371 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002372 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002373 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002374 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002375 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002376 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002377 }
2378
2379 // The address of the thread local variable is the add of the thread
2380 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002381 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002382}
2383
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002384SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002385ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002386 // TODO: implement the "local dynamic" model
2387 assert(Subtarget->isTargetELF() &&
2388 "TLS not implemented for non-ELF targets");
2389 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgaea41202012-05-04 09:40:39 +00002390
2391 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2392
2393 switch (model) {
2394 case TLSModel::GeneralDynamic:
2395 case TLSModel::LocalDynamic:
2396 return LowerToTLSGeneralDynamicModel(GA, DAG);
2397 case TLSModel::InitialExec:
2398 case TLSModel::LocalExec:
2399 return LowerToTLSExecModels(GA, DAG, model);
2400 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002401 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002402}
2403
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002404SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002405 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002406 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002407 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002408 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002409 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Rafael Espindola6de96a12009-01-15 20:18:42 +00002410 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002411 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002412 ARMConstantPoolConstant::Create(GV,
2413 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002414 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002415 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson7117a912009-03-20 22:42:55 +00002416 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002417 CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002418 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002419 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002420 SDValue Chain = Result.getValue(1);
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002421 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00002422 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002423 if (!UseGOTOFF)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002424 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002425 MachinePointerInfo::getGOT(),
2426 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002427 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002428 }
2429
2430 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002431 // pair. This is always cheaper.
2432 if (Subtarget->useMovt()) {
Evan Cheng68aec142011-01-19 02:16:49 +00002433 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002434 // FIXME: Once remat is capable of dealing with instructions with register
2435 // operands, expand this into two nodes.
2436 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2437 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002438 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002439 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2440 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2441 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2442 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002443 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002444 }
2445}
2446
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002447SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002448 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002449 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002450 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002451 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002452 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002453
Tim Northover72360d22013-12-02 10:35:41 +00002454 if (Subtarget->useMovt())
Evan Cheng68aec142011-01-19 02:16:49 +00002455 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002456
Tim Northover72360d22013-12-02 10:35:41 +00002457 // FIXME: Once remat is capable of dealing with instructions with register
2458 // operands, expand this into multiple nodes
2459 unsigned Wrapper =
2460 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002461
Tim Northover72360d22013-12-02 10:35:41 +00002462 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2463 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002464
Evan Cheng1b389522009-09-03 07:04:02 +00002465 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002466 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2467 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002468 return Result;
2469}
2470
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002471SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002472 SelectionDAG &DAG) const {
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002473 assert(Subtarget->isTargetELF() &&
2474 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Cheng408aa562009-11-06 22:24:13 +00002475 MachineFunction &MF = DAG.getMachineFunction();
2476 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002477 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002478 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002479 SDLoc dl(Op);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002480 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingc214cb02011-10-01 08:58:29 +00002481 ARMConstantPoolValue *CPV =
2482 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2483 ARMPCLabelIndex, PCAdj);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002484 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002485 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002486 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002487 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002488 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00002489 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002490 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002491}
2492
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002493SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002494ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002495 SDLoc dl(Op);
Jim Grosbachfaa3abb2010-05-27 23:49:24 +00002496 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002497 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2498 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002499 Op.getOperand(1), Val);
2500}
2501
2502SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002503ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002504 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002505 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2506 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2507}
2508
2509SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002510ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002511 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002512 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002513 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002514 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002515 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson17f88782009-08-04 00:25:01 +00002516 case Intrinsic::arm_thread_pointer: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002517 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson17f88782009-08-04 00:25:01 +00002518 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2519 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002520 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002521 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002522 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002523 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002524 EVT PtrVT = getPointerTy();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002525 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2526 SDValue CPAddr;
2527 unsigned PCAdj = (RelocM != Reloc::PIC_)
2528 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002529 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002530 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2531 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002532 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002533 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002534 SDValue Result =
Evan Chengcdbb70c2009-10-31 03:39:36 +00002535 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002536 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002537 false, false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002538
2539 if (RelocM == Reloc::PIC_) {
Evan Cheng408aa562009-11-06 22:24:13 +00002540 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002541 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2542 }
2543 return Result;
2544 }
Evan Cheng18381b42011-03-29 23:06:19 +00002545 case Intrinsic::arm_neon_vmulls:
2546 case Intrinsic::arm_neon_vmullu: {
2547 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2548 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002549 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002550 Op.getOperand(1), Op.getOperand(2));
2551 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002552 }
2553}
2554
Eli Friedman30a49e92011-08-03 21:06:02 +00002555static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2556 const ARMSubtarget *Subtarget) {
2557 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002558 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00002559 if (!Subtarget->hasDataBarrier()) {
2560 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2561 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2562 // here.
2563 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00002564 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00002565 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00002566 DAG.getConstant(0, MVT::i32));
2567 }
2568
Tim Northover36b24172013-07-03 09:20:36 +00002569 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
2570 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
2571 unsigned Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00002572 if (Subtarget->isMClass()) {
2573 // Only a full system barrier exists in the M-class architectures.
2574 Domain = ARM_MB::SY;
2575 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00002576 // Swift happens to implement ISHST barriers in a way that's compatible with
2577 // Release semantics but weaker than ISH so we'd be fools not to use
2578 // it. Beware: other processors probably don't!
2579 Domain = ARM_MB::ISHST;
2580 }
2581
Joey Gouly926d3f52013-09-05 15:35:24 +00002582 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
2583 DAG.getConstant(Intrinsic::arm_dmb, MVT::i32),
Tim Northover36b24172013-07-03 09:20:36 +00002584 DAG.getConstant(Domain, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002585}
2586
Evan Cheng8740ee32010-11-03 06:34:55 +00002587static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2588 const ARMSubtarget *Subtarget) {
2589 // ARM pre v5TE and Thumb1 does not have preload instructions.
2590 if (!(Subtarget->isThumb2() ||
2591 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2592 // Just preserve the chain.
2593 return Op.getOperand(0);
2594
Andrew Trickef9de2a2013-05-25 02:42:55 +00002595 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00002596 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2597 if (!isRead &&
2598 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2599 // ARMv7 with MP extension has PLDW.
2600 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00002601
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002602 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2603 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00002604 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00002605 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002606 isData = ~isData & 1;
2607 }
Evan Cheng8740ee32010-11-03 06:34:55 +00002608
2609 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng21acf9f2010-11-04 05:19:35 +00002610 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2611 DAG.getConstant(isData, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00002612}
2613
Dan Gohman31ae5862010-04-17 14:41:14 +00002614static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2615 MachineFunction &MF = DAG.getMachineFunction();
2616 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2617
Evan Cheng10043e22007-01-19 07:51:42 +00002618 // vastart just stores the address of the VarArgsFrameIndex slot into the
2619 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002620 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002621 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00002622 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002623 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00002624 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2625 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002626}
2627
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002628SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00002629ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2630 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002631 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00002632 MachineFunction &MF = DAG.getMachineFunction();
2633 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2634
Craig Topper760b1342012-02-22 05:59:10 +00002635 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00002636 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002637 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002638 else
Craig Topperc7242e02012-04-20 07:30:17 +00002639 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002640
2641 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002642 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002643 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002644
2645 SDValue ArgValue2;
2646 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002647 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00002648 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00002649
2650 // Create load node to retrieve arguments from the stack.
2651 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Chengcdbb70c2009-10-31 03:39:36 +00002652 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002653 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002654 false, false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00002655 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00002656 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002657 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002658 }
2659
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002660 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00002661}
2662
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002663void
2664ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002665 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002666 unsigned ArgSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002667 unsigned &ArgRegsSize,
2668 unsigned &ArgRegsSaveSize)
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002669 const {
2670 unsigned NumGPRs;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002671 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2672 unsigned RBegin, REnd;
2673 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2674 NumGPRs = REnd - RBegin;
2675 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002676 unsigned int firstUnalloced;
2677 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2678 sizeof(GPRArgRegs) /
2679 sizeof(GPRArgRegs[0]));
2680 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2681 }
2682
2683 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002684 ArgRegsSize = NumGPRs * 4;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002685
2686 // If parameter is split between stack and GPRs...
Mark Seabornbe266aa2014-02-16 18:59:48 +00002687 if (NumGPRs && Align > 4 &&
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002688 (ArgRegsSize < ArgSize ||
2689 InRegsParamRecordIdx >= CCInfo.getInRegsParamsCount())) {
Mark Seabornbe266aa2014-02-16 18:59:48 +00002690 // Add padding for part of param recovered from GPRs. For example,
2691 // if Align == 8, its last byte must be at address K*8 - 1.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002692 // We need to do it, since remained (stack) part of parameter has
2693 // stack alignment, and we need to "attach" "GPRs head" without gaps
2694 // to it:
2695 // Stack:
2696 // |---- 8 bytes block ----| |---- 8 bytes block ----| |---- 8 bytes...
2697 // [ [padding] [GPRs head] ] [ Tail passed via stack ....
2698 //
2699 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2700 unsigned Padding =
Mark Seabornbe266aa2014-02-16 18:59:48 +00002701 OffsetToAlignment(ArgRegsSize + AFI->getArgRegsSaveSize(), Align);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002702 ArgRegsSaveSize = ArgRegsSize + Padding;
2703 } else
2704 // We don't need to extend regs save size for byval parameters if they
2705 // are passed via GPRs only.
2706 ArgRegsSaveSize = ArgRegsSize;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002707}
2708
2709// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00002710// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002711// byval). Either way, we allocate stack slots adjacent to the data
2712// provided by our caller, and store the unallocated registers there.
2713// If this is a variadic function, the va_list pointer will begin with
2714// these values; otherwise, this reassembles a (byval) structure that
2715// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002716// Return: The frame index registers were stored into.
2717int
2718ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002719 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002720 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002721 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002722 unsigned OffsetFromOrigArg,
2723 unsigned ArgOffset,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002724 unsigned ArgSize,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002725 bool ForceMutable,
2726 unsigned ByValStoreOffset,
2727 unsigned TotalArgRegsSaveSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002728
2729 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00002730 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002731 // Setup first unallocated register as first byval register;
2732 // eat all remained registers
2733 // (these two actions are performed by HandleByVal method).
2734 // Then, here, we initialize stack frame with
2735 // "store-reg" instructions.
2736 // Case #2. Var-args function, that doesn't contain byval parameters.
2737 // The same: eat all remained unallocated registers,
2738 // initialize stack frame.
2739
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002740 MachineFunction &MF = DAG.getMachineFunction();
2741 MachineFrameInfo *MFI = MF.getFrameInfo();
2742 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002743 unsigned firstRegToSaveIndex, lastRegToSaveIndex;
2744 unsigned RBegin, REnd;
2745 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2746 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2747 firstRegToSaveIndex = RBegin - ARM::R0;
2748 lastRegToSaveIndex = REnd - ARM::R0;
2749 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002750 firstRegToSaveIndex = CCInfo.getFirstUnallocated
Craig Topper58713212013-07-15 04:27:47 +00002751 (GPRArgRegs, array_lengthof(GPRArgRegs));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002752 lastRegToSaveIndex = 4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002753 }
2754
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002755 unsigned ArgRegsSize, ArgRegsSaveSize;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002756 computeRegArea(CCInfo, MF, InRegsParamRecordIdx, ArgSize,
2757 ArgRegsSize, ArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002758
2759 // Store any by-val regs to their spots on the stack so that they may be
2760 // loaded by deferencing the result of formal parameter pointer or va_next.
2761 // Note: once stack area for byval/varargs registers
2762 // was initialized, it can't be initialized again.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002763 if (ArgRegsSaveSize) {
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002764 unsigned Padding = ArgRegsSaveSize - ArgRegsSize;
2765
2766 if (Padding) {
2767 assert(AFI->getStoredByValParamsPadding() == 0 &&
2768 "The only parameter may be padded.");
2769 AFI->setStoredByValParamsPadding(Padding);
2770 }
2771
Oliver Stannardd55e1152014-03-05 15:25:27 +00002772 int FrameIndex = MFI->CreateFixedObject(ArgRegsSaveSize,
2773 Padding +
2774 ByValStoreOffset -
2775 (int64_t)TotalArgRegsSaveSize,
2776 false);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002777 SDValue FIN = DAG.getFrameIndex(FrameIndex, getPointerTy());
Oliver Stannardd55e1152014-03-05 15:25:27 +00002778 if (Padding) {
2779 MFI->CreateFixedObject(Padding,
2780 ArgOffset + ByValStoreOffset -
2781 (int64_t)ArgRegsSaveSize,
2782 false);
2783 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002784
2785 SmallVector<SDValue, 4> MemOps;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002786 for (unsigned i = 0; firstRegToSaveIndex < lastRegToSaveIndex;
2787 ++firstRegToSaveIndex, ++i) {
Craig Topper760b1342012-02-22 05:59:10 +00002788 const TargetRegisterClass *RC;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002789 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002790 RC = &ARM::tGPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002791 else
Craig Topperc7242e02012-04-20 07:30:17 +00002792 RC = &ARM::GPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002793
2794 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2795 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2796 SDValue Store =
2797 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002798 MachinePointerInfo(OrigArg, OffsetFromOrigArg + 4*i),
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002799 false, false, 0);
2800 MemOps.push_back(Store);
2801 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2802 DAG.getConstant(4, getPointerTy()));
2803 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002804
2805 AFI->setArgRegsSaveSize(ArgRegsSaveSize + AFI->getArgRegsSaveSize());
2806
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002807 if (!MemOps.empty())
2808 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2809 &MemOps[0], MemOps.size());
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002810 return FrameIndex;
Oliver Stannardd55e1152014-03-05 15:25:27 +00002811 } else {
2812 if (ArgSize == 0) {
2813 // We cannot allocate a zero-byte object for the first variadic argument,
2814 // so just make up a size.
2815 ArgSize = 4;
2816 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002817 // This will point to the next argument passed via stack.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002818 return MFI->CreateFixedObject(
Oliver Stannardd55e1152014-03-05 15:25:27 +00002819 ArgSize, ArgOffset, !ForceMutable);
2820 }
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002821}
2822
2823// Setup stack frame, the va_list pointer will start from.
2824void
2825ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002826 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002827 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002828 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002829 bool ForceMutable) const {
2830 MachineFunction &MF = DAG.getMachineFunction();
2831 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2832
2833 // Try to store any remaining integer argument regs
2834 // to their spots on the stack so that they may be loaded by deferencing
2835 // the result of va_next.
2836 // If there is no regs to be stored, just point address after last
2837 // argument passed via stack.
2838 int FrameIndex =
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002839 StoreByValRegs(CCInfo, DAG, dl, Chain, 0, CCInfo.getInRegsParamsCount(),
Oliver Stannardd55e1152014-03-05 15:25:27 +00002840 0, ArgOffset, 0, ForceMutable, 0, TotalArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002841
2842 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002843}
2844
Bob Wilson2e076c42009-06-22 23:27:02 +00002845SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002846ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002847 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002848 const SmallVectorImpl<ISD::InputArg>
2849 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002850 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002851 SmallVectorImpl<SDValue> &InVals)
2852 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00002853 MachineFunction &MF = DAG.getMachineFunction();
2854 MachineFrameInfo *MFI = MF.getFrameInfo();
2855
Bob Wilsona4c22902009-04-17 19:07:39 +00002856 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2857
2858 // Assign locations to all of the incoming arguments.
2859 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00002860 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2861 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002862 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002863 CCAssignFnForNode(CallConv, /* Return*/ false,
2864 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00002865
Bob Wilsona4c22902009-04-17 19:07:39 +00002866 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002867 int lastInsIndex = -1;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002868 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002869 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
2870 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002871
2872 // Initially ArgRegsSaveSize is zero.
2873 // Then we increase this value each time we meet byval parameter.
2874 // We also increase this value in case of varargs function.
2875 AFI->setArgRegsSaveSize(0);
2876
Oliver Stannardd55e1152014-03-05 15:25:27 +00002877 unsigned ByValStoreOffset = 0;
2878 unsigned TotalArgRegsSaveSize = 0;
2879 unsigned ArgRegsSaveSizeMaxAlign = 4;
2880
2881 // Calculate the amount of stack space that we need to allocate to store
2882 // byval and variadic arguments that are passed in registers.
2883 // We need to know this before we allocate the first byval or variadic
2884 // argument, as they will be allocated a stack slot below the CFA (Canonical
2885 // Frame Address, the stack pointer at entry to the function).
2886 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2887 CCValAssign &VA = ArgLocs[i];
2888 if (VA.isMemLoc()) {
2889 int index = VA.getValNo();
2890 if (index != lastInsIndex) {
2891 ISD::ArgFlagsTy Flags = Ins[index].Flags;
2892 if (Flags.isByVal()) {
2893 unsigned ExtraArgRegsSize;
2894 unsigned ExtraArgRegsSaveSize;
2895 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsProceed(),
2896 Flags.getByValSize(),
2897 ExtraArgRegsSize, ExtraArgRegsSaveSize);
2898
2899 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
2900 if (Flags.getByValAlign() > ArgRegsSaveSizeMaxAlign)
2901 ArgRegsSaveSizeMaxAlign = Flags.getByValAlign();
2902 CCInfo.nextInRegsParam();
2903 }
2904 lastInsIndex = index;
2905 }
2906 }
2907 }
2908 CCInfo.rewindByValRegsInfo();
2909 lastInsIndex = -1;
2910 if (isVarArg) {
2911 unsigned ExtraArgRegsSize;
2912 unsigned ExtraArgRegsSaveSize;
2913 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsCount(), 0,
2914 ExtraArgRegsSize, ExtraArgRegsSaveSize);
2915 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
2916 }
2917 // If the arg regs save area contains N-byte aligned values, the
2918 // bottom of it must be at least N-byte aligned.
2919 TotalArgRegsSaveSize = RoundUpToAlignment(TotalArgRegsSaveSize, ArgRegsSaveSizeMaxAlign);
2920 TotalArgRegsSaveSize = std::min(TotalArgRegsSaveSize, 16U);
2921
Bob Wilsona4c22902009-04-17 19:07:39 +00002922 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2923 CCValAssign &VA = ArgLocs[i];
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002924 std::advance(CurOrigArg, Ins[VA.getValNo()].OrigArgIndex - CurArgIdx);
2925 CurArgIdx = Ins[VA.getValNo()].OrigArgIndex;
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002926 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00002927 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002928 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00002929
Bob Wilsona4c22902009-04-17 19:07:39 +00002930 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002931 // f64 and vector types are split up into multiple registers or
2932 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00002933 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002934 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002935 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00002936 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00002937 SDValue ArgValue2;
2938 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00002939 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson699bdf72010-04-13 22:03:22 +00002940 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2941 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002942 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002943 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00002944 } else {
2945 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2946 Chain, DAG, dl);
2947 }
Owen Anderson9f944592009-08-11 20:47:22 +00002948 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2949 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00002950 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson9f944592009-08-11 20:47:22 +00002951 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00002952 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2953 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002954 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00002955
Bob Wilson2e076c42009-06-22 23:27:02 +00002956 } else {
Craig Topper760b1342012-02-22 05:59:10 +00002957 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002958
Owen Anderson9f944592009-08-11 20:47:22 +00002959 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00002960 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00002961 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00002962 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00002963 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00002964 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00002965 else if (RegVT == MVT::i32)
Craig Topperc7242e02012-04-20 07:30:17 +00002966 RC = AFI->isThumb1OnlyFunction() ?
2967 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2968 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002969 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00002970 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00002971
2972 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002973 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002974 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00002975 }
2976
2977 // If this is an 8 or 16-bit value, it is really passed promoted
2978 // to 32 bits. Insert an assert[sz]ext to capture this, then
2979 // truncate to the right size.
2980 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002981 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002982 case CCValAssign::Full: break;
2983 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002984 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00002985 break;
2986 case CCValAssign::SExt:
2987 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2988 DAG.getValueType(VA.getValVT()));
2989 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2990 break;
2991 case CCValAssign::ZExt:
2992 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2993 DAG.getValueType(VA.getValVT()));
2994 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2995 break;
2996 }
2997
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002998 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00002999
3000 } else { // VA.isRegLoc()
3001
3002 // sanity check
3003 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003004 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003005
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003006 int index = ArgLocs[i].getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003007
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003008 // Some Ins[] entries become multiple ArgLoc[] entries.
3009 // Process them only once.
3010 if (index != lastInsIndex)
3011 {
3012 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003013 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003014 // This can be changed with more analysis.
3015 // In case of tail call optimization mark all arguments mutable.
3016 // Since they could be overwritten by lowering of arguments in case of
3017 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003018 if (Flags.isByVal()) {
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003019 unsigned CurByValIndex = CCInfo.getInRegsParamsProceed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003020
3021 ByValStoreOffset = RoundUpToAlignment(ByValStoreOffset, Flags.getByValAlign());
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003022 int FrameIndex = StoreByValRegs(
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003023 CCInfo, DAG, dl, Chain, CurOrigArg,
3024 CurByValIndex,
3025 Ins[VA.getValNo()].PartOffset,
3026 VA.getLocMemOffset(),
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003027 Flags.getByValSize(),
Oliver Stannardd55e1152014-03-05 15:25:27 +00003028 true /*force mutable frames*/,
3029 ByValStoreOffset,
3030 TotalArgRegsSaveSize);
3031 ByValStoreOffset += Flags.getByValSize();
3032 ByValStoreOffset = std::min(ByValStoreOffset, 16U);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003033 InVals.push_back(DAG.getFrameIndex(FrameIndex, getPointerTy()));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003034 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003035 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003036 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003037 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003038 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003039
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003040 // Create load nodes to retrieve arguments from the stack.
3041 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3042 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
3043 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003044 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003045 }
3046 lastInsIndex = index;
3047 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003048 }
3049 }
3050
3051 // varargs
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003052 if (isVarArg)
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003053 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003054 CCInfo.getNextStackOffset(),
3055 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003056
Oliver Stannardb14c6252014-04-02 16:10:33 +00003057 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3058
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003059 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003060}
3061
3062/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003063static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003064 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003065 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003066 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003067 // Maybe this has already been legalized into the constant pool?
3068 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003069 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003070 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003071 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003072 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003073 }
3074 }
3075 return false;
3076}
3077
Evan Cheng10043e22007-01-19 07:51:42 +00003078/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3079/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003080SDValue
3081ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003082 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003083 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003084 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003085 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003086 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003087 // Constant does not fit, try adjusting it by one?
3088 switch (CC) {
3089 default: break;
3090 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003091 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003092 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003093 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003094 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003095 }
3096 break;
3097 case ISD::SETULT:
3098 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003099 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003100 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003101 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003102 }
3103 break;
3104 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003105 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003106 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003107 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003108 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003109 }
3110 break;
3111 case ISD::SETULE:
3112 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003113 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003114 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003115 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003116 }
3117 break;
3118 }
3119 }
3120 }
3121
3122 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003123 ARMISD::NodeType CompareType;
3124 switch (CondCode) {
3125 default:
3126 CompareType = ARMISD::CMP;
3127 break;
3128 case ARMCC::EQ:
3129 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003130 // Uses only Z Flag
3131 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003132 break;
3133 }
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003134 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003135 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003136}
3137
3138/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003139SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003140ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003141 SDLoc dl) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003142 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003143 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003144 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003145 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003146 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3147 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003148}
3149
Bob Wilson45acbd02011-03-08 01:17:20 +00003150/// duplicateCmp - Glue values can have only one use, so this function
3151/// duplicates a comparison node.
3152SDValue
3153ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3154 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003155 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003156 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3157 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3158
3159 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3160 Cmp = Cmp.getOperand(0);
3161 Opc = Cmp.getOpcode();
3162 if (Opc == ARMISD::CMPFP)
3163 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3164 else {
3165 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3166 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3167 }
3168 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3169}
3170
Bill Wendling6a981312010-08-11 08:43:16 +00003171SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3172 SDValue Cond = Op.getOperand(0);
3173 SDValue SelectTrue = Op.getOperand(1);
3174 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003175 SDLoc dl(Op);
Bill Wendling6a981312010-08-11 08:43:16 +00003176
3177 // Convert:
3178 //
3179 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3180 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3181 //
3182 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3183 const ConstantSDNode *CMOVTrue =
3184 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3185 const ConstantSDNode *CMOVFalse =
3186 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3187
3188 if (CMOVTrue && CMOVFalse) {
3189 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3190 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3191
3192 SDValue True;
3193 SDValue False;
3194 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3195 True = SelectTrue;
3196 False = SelectFalse;
3197 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3198 True = SelectFalse;
3199 False = SelectTrue;
3200 }
3201
3202 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003203 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003204 SDValue ARMcc = Cond.getOperand(2);
3205 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003206 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003207 assert(True.getValueType() == VT);
3208 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendling6a981312010-08-11 08:43:16 +00003209 }
3210 }
3211 }
3212
Dan Gohmand4a77c42012-02-24 00:09:36 +00003213 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3214 // undefined bits before doing a full-word comparison with zero.
3215 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
3216 DAG.getConstant(1, Cond.getValueType()));
3217
Bill Wendling6a981312010-08-11 08:43:16 +00003218 return DAG.getSelectCC(dl, Cond,
3219 DAG.getConstant(0, Cond.getValueType()),
3220 SelectTrue, SelectFalse, ISD::SETNE);
3221}
3222
Joey Gouly881eab52013-08-22 15:29:11 +00003223static ISD::CondCode getInverseCCForVSEL(ISD::CondCode CC) {
3224 if (CC == ISD::SETNE)
3225 return ISD::SETEQ;
Weiming Zhao63871d22013-12-18 22:25:17 +00003226 return ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003227}
3228
3229static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3230 bool &swpCmpOps, bool &swpVselOps) {
3231 // Start by selecting the GE condition code for opcodes that return true for
3232 // 'equality'
3233 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3234 CC == ISD::SETULE)
3235 CondCode = ARMCC::GE;
3236
3237 // and GT for opcodes that return false for 'equality'.
3238 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3239 CC == ISD::SETULT)
3240 CondCode = ARMCC::GT;
3241
3242 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3243 // to swap the compare operands.
3244 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3245 CC == ISD::SETULT)
3246 swpCmpOps = true;
3247
3248 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3249 // If we have an unordered opcode, we need to swap the operands to the VSEL
3250 // instruction (effectively negating the condition).
3251 //
3252 // This also has the effect of swapping which one of 'less' or 'greater'
3253 // returns true, so we also swap the compare operands. It also switches
3254 // whether we return true for 'equality', so we compensate by picking the
3255 // opposite condition code to our original choice.
3256 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3257 CC == ISD::SETUGT) {
3258 swpCmpOps = !swpCmpOps;
3259 swpVselOps = !swpVselOps;
3260 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3261 }
3262
3263 // 'ordered' is 'anything but unordered', so use the VS condition code and
3264 // swap the VSEL operands.
3265 if (CC == ISD::SETO) {
3266 CondCode = ARMCC::VS;
3267 swpVselOps = true;
3268 }
3269
3270 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3271 // code and swap the VSEL operands.
3272 if (CC == ISD::SETUNE) {
3273 CondCode = ARMCC::EQ;
3274 swpVselOps = true;
3275 }
3276}
3277
Dan Gohman21cea8a2010-04-17 15:26:15 +00003278SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003279 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003280 SDValue LHS = Op.getOperand(0);
3281 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003282 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003283 SDValue TrueVal = Op.getOperand(2);
3284 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003285 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003286
Owen Anderson9f944592009-08-11 20:47:22 +00003287 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003288 // Try to generate VSEL on ARMv8.
3289 // The VSEL instruction can't use all the usual ARM condition
3290 // codes: it only has two bits to select the condition code, so it's
3291 // constrained to use only GE, GT, VS and EQ.
3292 //
3293 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3294 // swap the operands of the previous compare instruction (effectively
3295 // inverting the compare condition, swapping 'less' and 'greater') and
3296 // sometimes need to swap the operands to the VSEL (which inverts the
3297 // condition in the sense of firing whenever the previous condition didn't)
Joey Goulyccd04892013-09-13 13:46:57 +00003298 if (getSubtarget()->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
Joey Gouly881eab52013-08-22 15:29:11 +00003299 TrueVal.getValueType() == MVT::f64)) {
3300 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3301 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3302 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
3303 CC = getInverseCCForVSEL(CC);
3304 std::swap(TrueVal, FalseVal);
3305 }
3306 }
3307
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003308 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003309 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003310 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Joey Gouly881eab52013-08-22 15:29:11 +00003311 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3312 Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003313 }
3314
3315 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003316 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003317
Joey Gouly881eab52013-08-22 15:29:11 +00003318 // Try to generate VSEL on ARMv8.
Joey Goulyccd04892013-09-13 13:46:57 +00003319 if (getSubtarget()->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
Joey Gouly881eab52013-08-22 15:29:11 +00003320 TrueVal.getValueType() == MVT::f64)) {
Joey Goulye3dd6842013-08-23 12:01:13 +00003321 // We can select VMAXNM/VMINNM from a compare followed by a select with the
3322 // same operands, as follows:
3323 // c = fcmp [ogt, olt, ugt, ult] a, b
3324 // select c, a, b
3325 // We only do this in unsafe-fp-math, because signed zeros and NaNs are
3326 // handled differently than the original code sequence.
3327 if (getTargetMachine().Options.UnsafeFPMath && LHS == TrueVal &&
3328 RHS == FalseVal) {
3329 if (CC == ISD::SETOGT || CC == ISD::SETUGT)
3330 return DAG.getNode(ARMISD::VMAXNM, dl, VT, TrueVal, FalseVal);
3331 if (CC == ISD::SETOLT || CC == ISD::SETULT)
3332 return DAG.getNode(ARMISD::VMINNM, dl, VT, TrueVal, FalseVal);
3333 }
3334
Joey Gouly881eab52013-08-22 15:29:11 +00003335 bool swpCmpOps = false;
3336 bool swpVselOps = false;
3337 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3338
3339 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3340 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3341 if (swpCmpOps)
3342 std::swap(LHS, RHS);
3343 if (swpVselOps)
3344 std::swap(TrueVal, FalseVal);
3345 }
3346 }
3347
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003348 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3349 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003350 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00003351 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003352 ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003353 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003354 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003355 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003356 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson7117a912009-03-20 22:42:55 +00003357 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003358 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Cheng10043e22007-01-19 07:51:42 +00003359 }
3360 return Result;
3361}
3362
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003363/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3364/// to morph to an integer compare sequence.
3365static bool canChangeToInt(SDValue Op, bool &SeenZero,
3366 const ARMSubtarget *Subtarget) {
3367 SDNode *N = Op.getNode();
3368 if (!N->hasOneUse())
3369 // Otherwise it requires moving the value from fp to integer registers.
3370 return false;
3371 if (!N->getNumValues())
3372 return false;
3373 EVT VT = Op.getValueType();
3374 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3375 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3376 // vmrs are very slow, e.g. cortex-a8.
3377 return false;
3378
3379 if (isFloatingPointZero(Op)) {
3380 SeenZero = true;
3381 return true;
3382 }
3383 return ISD::isNormalLoad(N);
3384}
3385
3386static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3387 if (isFloatingPointZero(Op))
3388 return DAG.getConstant(0, MVT::i32);
3389
3390 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003391 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003392 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003393 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003394 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003395
3396 llvm_unreachable("Unknown VFP cmp argument!");
3397}
3398
3399static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3400 SDValue &RetVal1, SDValue &RetVal2) {
3401 if (isFloatingPointZero(Op)) {
3402 RetVal1 = DAG.getConstant(0, MVT::i32);
3403 RetVal2 = DAG.getConstant(0, MVT::i32);
3404 return;
3405 }
3406
3407 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3408 SDValue Ptr = Ld->getBasePtr();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003409 RetVal1 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003410 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003411 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003412 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003413 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003414
3415 EVT PtrType = Ptr.getValueType();
3416 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003417 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003418 PtrType, Ptr, DAG.getConstant(4, PtrType));
Andrew Trickef9de2a2013-05-25 02:42:55 +00003419 RetVal2 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003420 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003421 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003422 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003423 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003424 return;
3425 }
3426
3427 llvm_unreachable("Unknown VFP cmp argument!");
3428}
3429
3430/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3431/// f32 and even f64 comparisons to integer ones.
3432SDValue
3433ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3434 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003435 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003436 SDValue LHS = Op.getOperand(2);
3437 SDValue RHS = Op.getOperand(3);
3438 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003439 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003440
Evan Chengd12af5d2012-03-01 23:27:13 +00003441 bool LHSSeenZero = false;
3442 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3443 bool RHSSeenZero = false;
3444 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3445 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003446 // If unsafe fp math optimization is enabled and there are no other uses of
3447 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003448 // to an integer comparison.
3449 if (CC == ISD::SETOEQ)
3450 CC = ISD::SETEQ;
3451 else if (CC == ISD::SETUNE)
3452 CC = ISD::SETNE;
3453
Evan Chengd12af5d2012-03-01 23:27:13 +00003454 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003455 SDValue ARMcc;
3456 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003457 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3458 bitcastf32Toi32(LHS, DAG), Mask);
3459 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3460 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003461 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3462 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3463 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3464 Chain, Dest, ARMcc, CCR, Cmp);
3465 }
3466
3467 SDValue LHS1, LHS2;
3468 SDValue RHS1, RHS2;
3469 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3470 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003471 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3472 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003473 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3474 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003475 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003476 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3477 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3478 }
3479
3480 return SDValue();
3481}
3482
3483SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3484 SDValue Chain = Op.getOperand(0);
3485 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3486 SDValue LHS = Op.getOperand(2);
3487 SDValue RHS = Op.getOperand(3);
3488 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003489 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003490
Owen Anderson9f944592009-08-11 20:47:22 +00003491 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003492 SDValue ARMcc;
3493 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003494 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003495 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003496 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003497 }
3498
Owen Anderson9f944592009-08-11 20:47:22 +00003499 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003500
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003501 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003502 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3503 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3504 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3505 if (Result.getNode())
3506 return Result;
3507 }
3508
Evan Cheng10043e22007-01-19 07:51:42 +00003509 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003510 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003511
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003512 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3513 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003514 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003515 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003516 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesen400dc2e2009-02-06 21:50:26 +00003517 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Cheng10043e22007-01-19 07:51:42 +00003518 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003519 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3520 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesen400dc2e2009-02-06 21:50:26 +00003521 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Cheng10043e22007-01-19 07:51:42 +00003522 }
3523 return Res;
3524}
3525
Dan Gohman21cea8a2010-04-17 15:26:15 +00003526SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003527 SDValue Chain = Op.getOperand(0);
3528 SDValue Table = Op.getOperand(1);
3529 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003530 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003531
Owen Anderson53aa7a92009-08-10 22:56:29 +00003532 EVT PTy = getPointerTy();
Evan Cheng10043e22007-01-19 07:51:42 +00003533 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3534 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3f17aee2009-07-14 18:44:34 +00003535 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003536 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson9f944592009-08-11 20:47:22 +00003537 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chengc8bed032009-07-28 20:53:24 +00003538 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3539 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003540 if (Subtarget->isThumb2()) {
3541 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3542 // which does another jump to the destination. This also makes it easier
3543 // to translate it to TBB / TBH later.
3544 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00003545 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Chengc6d70ae2009-07-29 02:18:14 +00003546 Addr, Op.getOperand(2), JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003547 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00003548 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003549 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattner7727d052010-09-21 06:44:06 +00003550 MachinePointerInfo::getJumpTable(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003551 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003552 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00003553 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson9f944592009-08-11 20:47:22 +00003554 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003555 } else {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003556 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003557 MachinePointerInfo::getJumpTable(),
3558 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003559 Chain = Addr.getValue(1);
Owen Anderson9f944592009-08-11 20:47:22 +00003560 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003561 }
Evan Cheng10043e22007-01-19 07:51:42 +00003562}
3563
Eli Friedman2d4055b2011-11-09 23:36:02 +00003564static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00003565 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003566 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003567
James Molloy547d4c02012-02-20 09:24:05 +00003568 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3569 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3570 return Op;
3571 return DAG.UnrollVectorOp(Op.getNode());
3572 }
3573
3574 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3575 "Invalid type for custom lowering!");
3576 if (VT != MVT::v4i16)
3577 return DAG.UnrollVectorOp(Op.getNode());
3578
3579 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3580 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003581}
3582
Bob Wilsone4191e72010-03-19 22:51:32 +00003583static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman2d4055b2011-11-09 23:36:02 +00003584 EVT VT = Op.getValueType();
3585 if (VT.isVector())
3586 return LowerVectorFP_TO_INT(Op, DAG);
3587
Andrew Trickef9de2a2013-05-25 02:42:55 +00003588 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003589 unsigned Opc;
3590
3591 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003592 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003593 case ISD::FP_TO_SINT:
3594 Opc = ARMISD::FTOSI;
3595 break;
3596 case ISD::FP_TO_UINT:
3597 Opc = ARMISD::FTOUI;
3598 break;
3599 }
3600 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peck527da1b2010-11-23 03:31:01 +00003601 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003602}
3603
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003604static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3605 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003606 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003607
Eli Friedman2d4055b2011-11-09 23:36:02 +00003608 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3609 if (VT.getVectorElementType() == MVT::f32)
3610 return Op;
3611 return DAG.UnrollVectorOp(Op.getNode());
3612 }
3613
Duncan Sandsa41634e2011-08-12 14:54:45 +00003614 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3615 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003616 if (VT != MVT::v4f32)
3617 return DAG.UnrollVectorOp(Op.getNode());
3618
3619 unsigned CastOpc;
3620 unsigned Opc;
3621 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003622 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003623 case ISD::SINT_TO_FP:
3624 CastOpc = ISD::SIGN_EXTEND;
3625 Opc = ISD::SINT_TO_FP;
3626 break;
3627 case ISD::UINT_TO_FP:
3628 CastOpc = ISD::ZERO_EXTEND;
3629 Opc = ISD::UINT_TO_FP;
3630 break;
3631 }
3632
3633 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3634 return DAG.getNode(Opc, dl, VT, Op);
3635}
3636
Bob Wilsone4191e72010-03-19 22:51:32 +00003637static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3638 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003639 if (VT.isVector())
3640 return LowerVectorINT_TO_FP(Op, DAG);
3641
Andrew Trickef9de2a2013-05-25 02:42:55 +00003642 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003643 unsigned Opc;
3644
3645 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003646 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003647 case ISD::SINT_TO_FP:
3648 Opc = ARMISD::SITOF;
3649 break;
3650 case ISD::UINT_TO_FP:
3651 Opc = ARMISD::UITOF;
3652 break;
3653 }
3654
Wesley Peck527da1b2010-11-23 03:31:01 +00003655 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilsone4191e72010-03-19 22:51:32 +00003656 return DAG.getNode(Opc, dl, VT, Op);
3657}
3658
Evan Cheng25f93642010-07-08 02:08:50 +00003659SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00003660 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003661 SDValue Tmp0 = Op.getOperand(0);
3662 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003663 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003664 EVT VT = Op.getValueType();
3665 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00003666 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3667 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3668 bool UseNEON = !InGPR && Subtarget->hasNEON();
3669
3670 if (UseNEON) {
3671 // Use VBSL to copy the sign bit.
3672 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3673 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3674 DAG.getTargetConstant(EncodedVal, MVT::i32));
3675 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3676 if (VT == MVT::f64)
3677 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3678 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3679 DAG.getConstant(32, MVT::i32));
3680 else /*if (VT == MVT::f32)*/
3681 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3682 if (SrcVT == MVT::f32) {
3683 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3684 if (VT == MVT::f64)
3685 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3686 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3687 DAG.getConstant(32, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00003688 } else if (VT == MVT::f32)
3689 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3690 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3691 DAG.getConstant(32, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00003692 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3693 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3694
3695 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3696 MVT::i32);
3697 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3698 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3699 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00003700
Evan Chengd6b641e2011-02-23 02:24:55 +00003701 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3702 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3703 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00003704 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00003705 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3706 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3707 DAG.getConstant(0, MVT::i32));
3708 } else {
3709 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3710 }
3711
3712 return Res;
3713 }
Evan Cheng2da1c952011-02-11 02:28:55 +00003714
3715 // Bitcast operand 1 to i32.
3716 if (SrcVT == MVT::f64)
3717 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3718 &Tmp1, 1).getValue(1);
3719 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3720
Evan Chengd6b641e2011-02-23 02:24:55 +00003721 // Or in the signbit with integer operations.
3722 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3723 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3724 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3725 if (VT == MVT::f32) {
3726 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3727 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3728 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3729 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00003730 }
3731
Evan Chengd6b641e2011-02-23 02:24:55 +00003732 // f64: Or the high part with signbit and then combine two parts.
3733 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3734 &Tmp0, 1);
3735 SDValue Lo = Tmp0.getValue(0);
3736 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3737 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3738 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00003739}
3740
Evan Cheng168ced92010-05-22 01:47:14 +00003741SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3742 MachineFunction &MF = DAG.getMachineFunction();
3743 MachineFrameInfo *MFI = MF.getFrameInfo();
3744 MFI->setReturnAddressIsTaken(true);
3745
Bill Wendling908bf812014-01-06 00:43:20 +00003746 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00003747 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00003748
Evan Cheng168ced92010-05-22 01:47:14 +00003749 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003750 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00003751 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3752 if (Depth) {
3753 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3754 SDValue Offset = DAG.getConstant(4, MVT::i32);
3755 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3756 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003757 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00003758 }
3759
3760 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00003761 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00003762 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3763}
3764
Dan Gohman21cea8a2010-04-17 15:26:15 +00003765SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003766 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3767 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00003768
Owen Anderson53aa7a92009-08-10 22:56:29 +00003769 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003770 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003771 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Tim Northoverd6a729b2014-01-06 14:28:05 +00003772 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetMachO())
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003773 ? ARM::R7 : ARM::R11;
3774 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3775 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00003776 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3777 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003778 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003779 return FrameAddr;
3780}
3781
Wesley Peck527da1b2010-11-23 03:31:01 +00003782/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00003783/// expand a bit convert where either the source or destination type is i64 to
3784/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3785/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3786/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00003787static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00003788 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003789 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003790 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00003791
Bob Wilson59b70ea2010-04-17 05:30:19 +00003792 // This function is only supposed to be called for i64 types, either as the
3793 // source or destination of the bit convert.
3794 EVT SrcVT = Op.getValueType();
3795 EVT DstVT = N->getValueType(0);
3796 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00003797 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00003798
Bob Wilson59b70ea2010-04-17 05:30:19 +00003799 // Turn i64->f64 into VMOVDRR.
3800 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson9f944592009-08-11 20:47:22 +00003801 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3802 DAG.getConstant(0, MVT::i32));
3803 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3804 DAG.getConstant(1, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00003805 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00003806 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00003807 }
Bob Wilson7117a912009-03-20 22:42:55 +00003808
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00003809 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00003810 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3811 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3812 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3813 // Merge the pieces into a single i64 value.
3814 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3815 }
Bob Wilson7117a912009-03-20 22:42:55 +00003816
Bob Wilson59b70ea2010-04-17 05:30:19 +00003817 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00003818}
3819
Bob Wilson2e076c42009-06-22 23:27:02 +00003820/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00003821/// Zero vectors are used to represent vector negation and in those cases
3822/// will be implemented with the NEON VNEG instruction. However, VNEG does
3823/// not support i64 elements, so sometimes the zero vectors will need to be
3824/// explicitly constructed. Regardless, use a canonical VMOV to create the
3825/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003826static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003827 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00003828 // The canonical modified immediate encoding of a zero vector is....0!
3829 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3830 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3831 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00003832 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00003833}
3834
Jim Grosbach624fcb22009-10-31 21:00:56 +00003835/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3836/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00003837SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3838 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00003839 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3840 EVT VT = Op.getValueType();
3841 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003842 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00003843 SDValue ShOpLo = Op.getOperand(0);
3844 SDValue ShOpHi = Op.getOperand(1);
3845 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003846 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00003847 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00003848
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00003849 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3850
Jim Grosbach624fcb22009-10-31 21:00:56 +00003851 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3852 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3853 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3854 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3855 DAG.getConstant(VTBits, MVT::i32));
3856 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3857 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00003858 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00003859
3860 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3861 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003862 ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00003863 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003864 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00003865 CCR, Cmp);
3866
3867 SDValue Ops[2] = { Lo, Hi };
3868 return DAG.getMergeValues(Ops, 2, dl);
3869}
3870
Jim Grosbach5d994042009-10-31 19:38:01 +00003871/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3872/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00003873SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3874 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00003875 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3876 EVT VT = Op.getValueType();
3877 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003878 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00003879 SDValue ShOpLo = Op.getOperand(0);
3880 SDValue ShOpHi = Op.getOperand(1);
3881 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003882 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00003883
3884 assert(Op.getOpcode() == ISD::SHL_PARTS);
3885 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3886 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3887 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3888 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3889 DAG.getConstant(VTBits, MVT::i32));
3890 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3891 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3892
3893 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3894 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3895 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003896 ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00003897 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003898 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00003899 CCR, Cmp);
3900
3901 SDValue Ops[2] = { Lo, Hi };
3902 return DAG.getMergeValues(Ops, 2, dl);
3903}
3904
Jim Grosbach535d3b42010-09-08 03:54:02 +00003905SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00003906 SelectionDAG &DAG) const {
3907 // The rounding mode is in bits 23:22 of the FPSCR.
3908 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3909 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3910 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003911 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00003912 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3913 DAG.getConstant(Intrinsic::arm_get_fpscr,
3914 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00003915 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemanb69b1822010-08-03 21:31:55 +00003916 DAG.getConstant(1U << 22, MVT::i32));
3917 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3918 DAG.getConstant(22, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00003919 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemanb69b1822010-08-03 21:31:55 +00003920 DAG.getConstant(3, MVT::i32));
3921}
3922
Jim Grosbach8546ec92010-01-18 19:58:49 +00003923static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3924 const ARMSubtarget *ST) {
3925 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003926 SDLoc dl(N);
Jim Grosbach8546ec92010-01-18 19:58:49 +00003927
3928 if (!ST->hasV6T2Ops())
3929 return SDValue();
3930
3931 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3932 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3933}
3934
Evan Chengb4eae132012-12-04 22:41:50 +00003935/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
3936/// for each 16-bit element from operand, repeated. The basic idea is to
3937/// leverage vcnt to get the 8-bit counts, gather and add the results.
3938///
3939/// Trace for v4i16:
3940/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
3941/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
3942/// 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 +00003943/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00003944/// [b0 b1 b2 b3 b4 b5 b6 b7]
3945/// +[b1 b0 b3 b2 b5 b4 b7 b6]
3946/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
3947/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
3948static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
3949 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003950 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00003951
3952 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
3953 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
3954 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
3955 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
3956 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
3957 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
3958}
3959
3960/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
3961/// bit-count for each 16-bit element from the operand. We need slightly
3962/// different sequencing for v4i16 and v8i16 to stay within NEON's available
3963/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00003964///
Evan Chengb4eae132012-12-04 22:41:50 +00003965/// Trace for v4i16:
3966/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
3967/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
3968/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
3969/// v4i16:Extracted = [k0 k1 k2 k3 ]
3970static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
3971 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003972 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00003973
3974 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
3975 if (VT.is64BitVector()) {
3976 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
3977 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
3978 DAG.getIntPtrConstant(0));
3979 } else {
3980 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
3981 BitCounts, DAG.getIntPtrConstant(0));
3982 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
3983 }
3984}
3985
3986/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
3987/// bit-count for each 32-bit element from the operand. The idea here is
3988/// to split the vector into 16-bit elements, leverage the 16-bit count
3989/// routine, and then combine the results.
3990///
3991/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
3992/// input = [v0 v1 ] (vi: 32-bit elements)
3993/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
3994/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00003995/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00003996/// [k0 k1 k2 k3 ]
3997/// N1 =+[k1 k0 k3 k2 ]
3998/// [k0 k2 k1 k3 ]
3999/// N2 =+[k1 k3 k0 k2 ]
4000/// [k0 k2 k1 k3 ]
4001/// Extended =+[k1 k3 k0 k2 ]
4002/// [k0 k2 ]
4003/// Extracted=+[k1 k3 ]
4004///
4005static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4006 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004007 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004008
4009 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4010
4011 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4012 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4013 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4014 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4015 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4016
4017 if (VT.is64BitVector()) {
4018 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4019 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
4020 DAG.getIntPtrConstant(0));
4021 } else {
4022 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
4023 DAG.getIntPtrConstant(0));
4024 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4025 }
4026}
4027
4028static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4029 const ARMSubtarget *ST) {
4030 EVT VT = N->getValueType(0);
4031
4032 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004033 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4034 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004035 "Unexpected type for custom ctpop lowering");
4036
4037 if (VT.getVectorElementType() == MVT::i32)
4038 return lowerCTPOP32BitElements(N, DAG);
4039 else
4040 return lowerCTPOP16BitElements(N, DAG);
4041}
4042
Bob Wilson2e076c42009-06-22 23:27:02 +00004043static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4044 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004045 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004046 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004047
Bob Wilson7d471332010-11-18 21:16:28 +00004048 if (!VT.isVector())
4049 return SDValue();
4050
Bob Wilson2e076c42009-06-22 23:27:02 +00004051 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004052 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004053
Bob Wilson7d471332010-11-18 21:16:28 +00004054 // Left shifts translate directly to the vshiftu intrinsic.
4055 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004056 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilson7d471332010-11-18 21:16:28 +00004057 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
4058 N->getOperand(0), N->getOperand(1));
4059
4060 assert((N->getOpcode() == ISD::SRA ||
4061 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4062
4063 // NEON uses the same intrinsics for both left and right shifts. For
4064 // right shifts, the shift amounts are negative, so negate the vector of
4065 // shift amounts.
4066 EVT ShiftVT = N->getOperand(1).getValueType();
4067 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4068 getZeroVector(ShiftVT, DAG, dl),
4069 N->getOperand(1));
4070 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4071 Intrinsic::arm_neon_vshifts :
4072 Intrinsic::arm_neon_vshiftu);
4073 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4074 DAG.getConstant(vshiftInt, MVT::i32),
4075 N->getOperand(0), NegatedCount);
4076}
4077
4078static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4079 const ARMSubtarget *ST) {
4080 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004081 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004082
Eli Friedman682d8c12009-08-22 03:13:10 +00004083 // We can get here for a node like i32 = ISD::SHL i32, i64
4084 if (VT != MVT::i64)
4085 return SDValue();
4086
4087 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004088 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004089
Chris Lattnerf81d5882007-11-24 07:07:01 +00004090 // We only lower SRA, SRL of 1 here, all others use generic lowering.
4091 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmaneffb8942008-09-12 16:56:44 +00004092 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands6ed40142008-12-01 11:39:25 +00004093 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004094
Chris Lattnerf81d5882007-11-24 07:07:01 +00004095 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004096 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004097
Chris Lattnerf81d5882007-11-24 07:07:01 +00004098 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004099 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004100 DAG.getConstant(0, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004101 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004102 DAG.getConstant(1, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004103
Chris Lattnerf81d5882007-11-24 07:07:01 +00004104 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4105 // captures the result into a carry flag.
4106 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004107 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson7117a912009-03-20 22:42:55 +00004108
Chris Lattnerf81d5882007-11-24 07:07:01 +00004109 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004110 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004111
Chris Lattnerf81d5882007-11-24 07:07:01 +00004112 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004113 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004114}
4115
Bob Wilson2e076c42009-06-22 23:27:02 +00004116static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4117 SDValue TmpOp0, TmpOp1;
4118 bool Invert = false;
4119 bool Swap = false;
4120 unsigned Opc = 0;
4121
4122 SDValue Op0 = Op.getOperand(0);
4123 SDValue Op1 = Op.getOperand(1);
4124 SDValue CC = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004125 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004126 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004127 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004128
4129 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
4130 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004131 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004132 case ISD::SETUNE:
4133 case ISD::SETNE: Invert = true; // Fallthrough
4134 case ISD::SETOEQ:
4135 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4136 case ISD::SETOLT:
4137 case ISD::SETLT: Swap = true; // Fallthrough
4138 case ISD::SETOGT:
4139 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4140 case ISD::SETOLE:
4141 case ISD::SETLE: Swap = true; // Fallthrough
4142 case ISD::SETOGE:
4143 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4144 case ISD::SETUGE: Swap = true; // Fallthrough
4145 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4146 case ISD::SETUGT: Swap = true; // Fallthrough
4147 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4148 case ISD::SETUEQ: Invert = true; // Fallthrough
4149 case ISD::SETONE:
4150 // Expand this to (OLT | OGT).
4151 TmpOp0 = Op0;
4152 TmpOp1 = Op1;
4153 Opc = ISD::OR;
4154 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
4155 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
4156 break;
4157 case ISD::SETUO: Invert = true; // Fallthrough
4158 case ISD::SETO:
4159 // Expand this to (OLT | OGE).
4160 TmpOp0 = Op0;
4161 TmpOp1 = Op1;
4162 Opc = ISD::OR;
4163 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
4164 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
4165 break;
4166 }
4167 } else {
4168 // Integer comparisons.
4169 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004170 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004171 case ISD::SETNE: Invert = true;
4172 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4173 case ISD::SETLT: Swap = true;
4174 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4175 case ISD::SETLE: Swap = true;
4176 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4177 case ISD::SETULT: Swap = true;
4178 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4179 case ISD::SETULE: Swap = true;
4180 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4181 }
4182
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004183 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004184 if (Opc == ARMISD::VCEQ) {
4185
4186 SDValue AndOp;
4187 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4188 AndOp = Op0;
4189 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4190 AndOp = Op1;
4191
4192 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004193 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004194 AndOp = AndOp.getOperand(0);
4195
4196 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4197 Opc = ARMISD::VTST;
Wesley Peck527da1b2010-11-23 03:31:01 +00004198 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
4199 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004200 Invert = !Invert;
4201 }
4202 }
4203 }
4204
4205 if (Swap)
4206 std::swap(Op0, Op1);
4207
Owen Andersonc7baee32010-11-08 23:21:22 +00004208 // If one of the operands is a constant vector zero, attempt to fold the
4209 // comparison to a specialized compare-against-zero form.
4210 SDValue SingleOp;
4211 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4212 SingleOp = Op0;
4213 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4214 if (Opc == ARMISD::VCGE)
4215 Opc = ARMISD::VCLEZ;
4216 else if (Opc == ARMISD::VCGT)
4217 Opc = ARMISD::VCLTZ;
4218 SingleOp = Op1;
4219 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004220
Owen Andersonc7baee32010-11-08 23:21:22 +00004221 SDValue Result;
4222 if (SingleOp.getNode()) {
4223 switch (Opc) {
4224 case ARMISD::VCEQ:
4225 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
4226 case ARMISD::VCGE:
4227 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
4228 case ARMISD::VCLEZ:
4229 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
4230 case ARMISD::VCGT:
4231 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
4232 case ARMISD::VCLTZ:
4233 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
4234 default:
4235 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
4236 }
4237 } else {
4238 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
4239 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004240
4241 if (Invert)
4242 Result = DAG.getNOT(dl, Result, VT);
4243
4244 return Result;
4245}
4246
Bob Wilson5b2b5042010-06-14 22:19:57 +00004247/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4248/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004249/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004250static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4251 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Andersona4076922010-11-05 21:57:54 +00004252 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004253 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004254
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004255 // SplatBitSize is set to the smallest size that splats the vector, so a
4256 // zero vector will always have SplatBitSize == 8. However, NEON modified
4257 // immediate instructions others than VMOV do not support the 8-bit encoding
4258 // of a zero vector, and the default encoding of zero is supposed to be the
4259 // 32-bit version.
4260 if (SplatBits == 0)
4261 SplatBitSize = 32;
4262
Bob Wilson2e076c42009-06-22 23:27:02 +00004263 switch (SplatBitSize) {
4264 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004265 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004266 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004267 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004268 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004269 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004270 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004271 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004272 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004273
4274 case 16:
4275 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004276 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004277 if ((SplatBits & ~0xff) == 0) {
4278 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004279 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004280 Imm = SplatBits;
4281 break;
4282 }
4283 if ((SplatBits & ~0xff00) == 0) {
4284 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004285 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004286 Imm = SplatBits >> 8;
4287 break;
4288 }
4289 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004290
4291 case 32:
4292 // NEON's 32-bit VMOV supports splat values where:
4293 // * only one byte is nonzero, or
4294 // * the least significant byte is 0xff and the second byte is nonzero, or
4295 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004296 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004297 if ((SplatBits & ~0xff) == 0) {
4298 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004299 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004300 Imm = SplatBits;
4301 break;
4302 }
4303 if ((SplatBits & ~0xff00) == 0) {
4304 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004305 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004306 Imm = SplatBits >> 8;
4307 break;
4308 }
4309 if ((SplatBits & ~0xff0000) == 0) {
4310 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004311 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004312 Imm = SplatBits >> 16;
4313 break;
4314 }
4315 if ((SplatBits & ~0xff000000) == 0) {
4316 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004317 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004318 Imm = SplatBits >> 24;
4319 break;
4320 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004321
Owen Andersona4076922010-11-05 21:57:54 +00004322 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4323 if (type == OtherModImm) return SDValue();
4324
Bob Wilson2e076c42009-06-22 23:27:02 +00004325 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004326 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4327 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004328 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004329 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004330 break;
4331 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004332
4333 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004334 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4335 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004336 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004337 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004338 break;
4339 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004340
4341 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4342 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4343 // VMOV.I32. A (very) minor optimization would be to replicate the value
4344 // and fall through here to test for a valid 64-bit splat. But, then the
4345 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004346 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004347
4348 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004349 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004350 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004351 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00004352 uint64_t BitMask = 0xff;
4353 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004354 unsigned ImmMask = 1;
4355 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00004356 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00004357 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004358 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00004359 Imm |= ImmMask;
4360 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004361 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004362 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004363 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004364 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00004365 }
Bob Wilson6eae5202010-06-11 21:34:50 +00004366 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004367 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00004368 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00004369 break;
4370 }
4371
Bob Wilson6eae5202010-06-11 21:34:50 +00004372 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00004373 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00004374 }
4375
Bob Wilsona3f19012010-07-13 21:16:48 +00004376 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
4377 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00004378}
4379
Lang Hames591cdaf2012-03-29 21:56:11 +00004380SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4381 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004382 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00004383 return SDValue();
4384
Tim Northoverf79c3a52013-08-20 08:57:11 +00004385 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00004386 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004387
4388 // Try splatting with a VMOV.f32...
4389 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00004390 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
4391
Lang Hames591cdaf2012-03-29 21:56:11 +00004392 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004393 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
4394 // We have code in place to select a valid ConstantFP already, no need to
4395 // do any mangling.
4396 return Op;
4397 }
4398
4399 // It's a float and we are trying to use NEON operations where
4400 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004401 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004402 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
4403 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4404 NewVal);
4405 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
4406 DAG.getConstant(0, MVT::i32));
4407 }
4408
Tim Northoverf79c3a52013-08-20 08:57:11 +00004409 // The rest of our options are NEON only, make sure that's allowed before
4410 // proceeding..
4411 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
4412 return SDValue();
4413
Lang Hames591cdaf2012-03-29 21:56:11 +00004414 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00004415 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
4416
4417 // It wouldn't really be worth bothering for doubles except for one very
4418 // important value, which does happen to match: 0.0. So make sure we don't do
4419 // anything stupid.
4420 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
4421 return SDValue();
4422
4423 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
4424 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4425 false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004426 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004427 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004428 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4429 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004430 if (IsDouble)
4431 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4432
4433 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004434 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4435 VecConstant);
4436 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4437 DAG.getConstant(0, MVT::i32));
4438 }
4439
4440 // Finally, try a VMVN.i32
Tim Northoverf79c3a52013-08-20 08:57:11 +00004441 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4442 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004443 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004444 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004445 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004446
4447 if (IsDouble)
4448 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4449
4450 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004451 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4452 VecConstant);
4453 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4454 DAG.getConstant(0, MVT::i32));
4455 }
4456
4457 return SDValue();
4458}
4459
Quentin Colombet8e1fe842012-11-02 21:32:17 +00004460// check if an VEXT instruction can handle the shuffle mask when the
4461// vector sources of the shuffle are the same.
4462static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4463 unsigned NumElts = VT.getVectorNumElements();
4464
4465 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4466 if (M[0] < 0)
4467 return false;
4468
4469 Imm = M[0];
4470
4471 // If this is a VEXT shuffle, the immediate value is the index of the first
4472 // element. The other shuffle indices must be the successive elements after
4473 // the first one.
4474 unsigned ExpectedElt = Imm;
4475 for (unsigned i = 1; i < NumElts; ++i) {
4476 // Increment the expected index. If it wraps around, just follow it
4477 // back to index zero and keep going.
4478 ++ExpectedElt;
4479 if (ExpectedElt == NumElts)
4480 ExpectedElt = 0;
4481
4482 if (M[i] < 0) continue; // ignore UNDEF indices
4483 if (ExpectedElt != static_cast<unsigned>(M[i]))
4484 return false;
4485 }
4486
4487 return true;
4488}
4489
Lang Hames591cdaf2012-03-29 21:56:11 +00004490
Benjamin Kramer339ced42012-01-15 13:16:05 +00004491static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004492 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004493 unsigned NumElts = VT.getVectorNumElements();
4494 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00004495
4496 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4497 if (M[0] < 0)
4498 return false;
4499
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004500 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00004501
4502 // If this is a VEXT shuffle, the immediate value is the index of the first
4503 // element. The other shuffle indices must be the successive elements after
4504 // the first one.
4505 unsigned ExpectedElt = Imm;
4506 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004507 // Increment the expected index. If it wraps around, it may still be
4508 // a VEXT but the source vectors must be swapped.
4509 ExpectedElt += 1;
4510 if (ExpectedElt == NumElts * 2) {
4511 ExpectedElt = 0;
4512 ReverseVEXT = true;
4513 }
4514
Bob Wilson411dfad2010-08-17 05:54:34 +00004515 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004516 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00004517 return false;
4518 }
4519
4520 // Adjust the index value if the source operands will be swapped.
4521 if (ReverseVEXT)
4522 Imm -= NumElts;
4523
Bob Wilson32cd8552009-08-19 17:03:43 +00004524 return true;
4525}
4526
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004527/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4528/// instruction with the specified blocksize. (The order of the elements
4529/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00004530static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004531 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4532 "Only possible block sizes for VREV are: 16, 32, 64");
4533
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004534 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00004535 if (EltSz == 64)
4536 return false;
4537
4538 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004539 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00004540 // If the first shuffle index is UNDEF, be optimistic.
4541 if (M[0] < 0)
4542 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004543
4544 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4545 return false;
4546
4547 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004548 if (M[i] < 0) continue; // ignore UNDEF indices
4549 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004550 return false;
4551 }
4552
4553 return true;
4554}
4555
Benjamin Kramer339ced42012-01-15 13:16:05 +00004556static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00004557 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
4558 // range, then 0 is placed into the resulting vector. So pretty much any mask
4559 // of 8 elements can work here.
4560 return VT == MVT::v8i8 && M.size() == 8;
4561}
4562
Benjamin Kramer339ced42012-01-15 13:16:05 +00004563static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004564 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4565 if (EltSz == 64)
4566 return false;
4567
Bob Wilsona7062312009-08-21 20:54:19 +00004568 unsigned NumElts = VT.getVectorNumElements();
4569 WhichResult = (M[0] == 0 ? 0 : 1);
4570 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004571 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4572 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsona7062312009-08-21 20:54:19 +00004573 return false;
4574 }
4575 return true;
4576}
4577
Bob Wilson0bbd3072009-12-03 06:40:55 +00004578/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
4579/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4580/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004581static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004582 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4583 if (EltSz == 64)
4584 return false;
4585
4586 unsigned NumElts = VT.getVectorNumElements();
4587 WhichResult = (M[0] == 0 ? 0 : 1);
4588 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004589 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4590 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004591 return false;
4592 }
4593 return true;
4594}
4595
Benjamin Kramer339ced42012-01-15 13:16:05 +00004596static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004597 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4598 if (EltSz == 64)
4599 return false;
4600
Bob Wilsona7062312009-08-21 20:54:19 +00004601 unsigned NumElts = VT.getVectorNumElements();
4602 WhichResult = (M[0] == 0 ? 0 : 1);
4603 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004604 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsona7062312009-08-21 20:54:19 +00004605 if ((unsigned) M[i] != 2 * i + WhichResult)
4606 return false;
4607 }
4608
4609 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004610 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004611 return false;
4612
4613 return true;
4614}
4615
Bob Wilson0bbd3072009-12-03 06:40:55 +00004616/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4617/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4618/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00004619static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004620 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4621 if (EltSz == 64)
4622 return false;
4623
4624 unsigned Half = VT.getVectorNumElements() / 2;
4625 WhichResult = (M[0] == 0 ? 0 : 1);
4626 for (unsigned j = 0; j != 2; ++j) {
4627 unsigned Idx = WhichResult;
4628 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004629 int MIdx = M[i + j * Half];
4630 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson0bbd3072009-12-03 06:40:55 +00004631 return false;
4632 Idx += 2;
4633 }
4634 }
4635
4636 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4637 if (VT.is64BitVector() && EltSz == 32)
4638 return false;
4639
4640 return true;
4641}
4642
Benjamin Kramer339ced42012-01-15 13:16:05 +00004643static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004644 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4645 if (EltSz == 64)
4646 return false;
4647
Bob Wilsona7062312009-08-21 20:54:19 +00004648 unsigned NumElts = VT.getVectorNumElements();
4649 WhichResult = (M[0] == 0 ? 0 : 1);
4650 unsigned Idx = WhichResult * NumElts / 2;
4651 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004652 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4653 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsona7062312009-08-21 20:54:19 +00004654 return false;
4655 Idx += 1;
4656 }
4657
4658 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004659 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004660 return false;
4661
4662 return true;
4663}
4664
Bob Wilson0bbd3072009-12-03 06:40:55 +00004665/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4666/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4667/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004668static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004669 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4670 if (EltSz == 64)
4671 return false;
4672
4673 unsigned NumElts = VT.getVectorNumElements();
4674 WhichResult = (M[0] == 0 ? 0 : 1);
4675 unsigned Idx = WhichResult * NumElts / 2;
4676 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004677 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4678 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004679 return false;
4680 Idx += 1;
4681 }
4682
4683 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4684 if (VT.is64BitVector() && EltSz == 32)
4685 return false;
4686
4687 return true;
4688}
4689
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00004690/// \return true if this is a reverse operation on an vector.
4691static bool isReverseMask(ArrayRef<int> M, EVT VT) {
4692 unsigned NumElts = VT.getVectorNumElements();
4693 // Make sure the mask has the right size.
4694 if (NumElts != M.size())
4695 return false;
4696
4697 // Look for <15, ..., 3, -1, 1, 0>.
4698 for (unsigned i = 0; i != NumElts; ++i)
4699 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
4700 return false;
4701
4702 return true;
4703}
4704
Dale Johannesen2bff5052010-07-29 20:10:08 +00004705// If N is an integer constant that can be moved into a register in one
4706// instruction, return an SDValue of such a constant (will become a MOV
4707// instruction). Otherwise return null.
4708static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004709 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00004710 uint64_t Val;
4711 if (!isa<ConstantSDNode>(N))
4712 return SDValue();
4713 Val = cast<ConstantSDNode>(N)->getZExtValue();
4714
4715 if (ST->isThumb1Only()) {
4716 if (Val <= 255 || ~Val <= 255)
4717 return DAG.getConstant(Val, MVT::i32);
4718 } else {
4719 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4720 return DAG.getConstant(Val, MVT::i32);
4721 }
4722 return SDValue();
4723}
4724
Bob Wilson2e076c42009-06-22 23:27:02 +00004725// If this is a case we can't handle, return null and let the default
4726// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00004727SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4728 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00004729 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004730 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004731 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004732
4733 APInt SplatBits, SplatUndef;
4734 unsigned SplatBitSize;
4735 bool HasAnyUndefs;
4736 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00004737 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00004738 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00004739 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00004740 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00004741 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00004742 DAG, VmovVT, VT.is128BitVector(),
4743 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00004744 if (Val.getNode()) {
4745 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00004746 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00004747 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00004748
4749 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00004750 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004751 Val = isNEONModifiedImm(NegatedImm,
4752 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00004753 DAG, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00004754 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00004755 if (Val.getNode()) {
4756 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00004757 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00004758 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00004759
4760 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00004761 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00004762 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00004763 if (ImmVal != -1) {
4764 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4765 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4766 }
4767 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00004768 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00004769 }
4770
Bob Wilson91fdf682010-05-22 00:23:12 +00004771 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00004772 //
4773 // As an optimisation, even if more than one value is used it may be more
4774 // profitable to splat with one value then change some lanes.
4775 //
4776 // Heuristically we decide to do this if the vector has a "dominant" value,
4777 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00004778 unsigned NumElts = VT.getVectorNumElements();
4779 bool isOnlyLowElement = true;
4780 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00004781 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00004782 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00004783
4784 // Map of the number of times a particular SDValue appears in the
4785 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00004786 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00004787 SDValue Value;
4788 for (unsigned i = 0; i < NumElts; ++i) {
4789 SDValue V = Op.getOperand(i);
4790 if (V.getOpcode() == ISD::UNDEF)
4791 continue;
4792 if (i > 0)
4793 isOnlyLowElement = false;
4794 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4795 isConstant = false;
4796
James Molloy49bdbce2012-09-06 09:55:02 +00004797 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00004798 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00004799
James Molloy49bdbce2012-09-06 09:55:02 +00004800 // Is this value dominant? (takes up more than half of the lanes)
4801 if (++Count > (NumElts / 2)) {
4802 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00004803 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00004804 }
Bob Wilson91fdf682010-05-22 00:23:12 +00004805 }
James Molloy49bdbce2012-09-06 09:55:02 +00004806 if (ValueCounts.size() != 1)
4807 usesOnlyOneValue = false;
4808 if (!Value.getNode() && ValueCounts.size() > 0)
4809 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00004810
James Molloy49bdbce2012-09-06 09:55:02 +00004811 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00004812 return DAG.getUNDEF(VT);
4813
Quentin Colombet0f2fe742013-07-23 22:34:47 +00004814 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
4815 // Keep going if we are hitting this case.
4816 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00004817 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4818
Dale Johannesen2bff5052010-07-29 20:10:08 +00004819 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4820
Dale Johannesen710a2d92010-10-19 20:00:17 +00004821 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4822 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00004823 if (hasDominantValue && EltSize <= 32) {
4824 if (!isConstant) {
4825 SDValue N;
4826
4827 // If we are VDUPing a value that comes directly from a vector, that will
4828 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00004829 // just use VDUPLANE. We can only do this if the lane being extracted
4830 // is at a constant index, as the VDUP from lane instructions only have
4831 // constant-index forms.
4832 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4833 isa<ConstantSDNode>(Value->getOperand(1))) {
Silviu Barangab1409702012-10-15 09:41:32 +00004834 // We need to create a new undef vector to use for the VDUPLANE if the
4835 // size of the vector from which we get the value is different than the
4836 // size of the vector that we need to create. We will insert the element
4837 // such that the register coalescer will remove unnecessary copies.
4838 if (VT != Value->getOperand(0).getValueType()) {
4839 ConstantSDNode *constIndex;
4840 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
4841 assert(constIndex && "The index is not a constant!");
4842 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
4843 VT.getVectorNumElements();
4844 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
4845 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
4846 Value, DAG.getConstant(index, MVT::i32)),
4847 DAG.getConstant(index, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00004848 } else
Silviu Barangab1409702012-10-15 09:41:32 +00004849 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00004850 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00004851 } else
James Molloy49bdbce2012-09-06 09:55:02 +00004852 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4853
4854 if (!usesOnlyOneValue) {
4855 // The dominant value was splatted as 'N', but we now have to insert
4856 // all differing elements.
4857 for (unsigned I = 0; I < NumElts; ++I) {
4858 if (Op.getOperand(I) == Value)
4859 continue;
4860 SmallVector<SDValue, 3> Ops;
4861 Ops.push_back(N);
4862 Ops.push_back(Op.getOperand(I));
4863 Ops.push_back(DAG.getConstant(I, MVT::i32));
4864 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, &Ops[0], 3);
4865 }
4866 }
4867 return N;
4868 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00004869 if (VT.getVectorElementType().isFloatingPoint()) {
4870 SmallVector<SDValue, 8> Ops;
4871 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00004872 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00004873 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00004874 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4875 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesenff376752010-10-20 22:03:37 +00004876 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4877 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00004878 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00004879 }
James Molloy49bdbce2012-09-06 09:55:02 +00004880 if (usesOnlyOneValue) {
4881 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4882 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00004883 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00004884 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00004885 }
4886
4887 // If all elements are constants and the case above didn't get hit, fall back
4888 // to the default expansion, which will generate a load from the constant
4889 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00004890 if (isConstant)
4891 return SDValue();
4892
Bob Wilson6f2b8962011-01-07 21:37:30 +00004893 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4894 if (NumElts >= 4) {
4895 SDValue shuffle = ReconstructShuffle(Op, DAG);
4896 if (shuffle != SDValue())
4897 return shuffle;
4898 }
4899
Bob Wilson91fdf682010-05-22 00:23:12 +00004900 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00004901 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4902 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00004903 if (EltSize >= 32) {
4904 // Do the expansion with floating-point types, since that is what the VFP
4905 // registers are defined to use, and since i64 is not legal.
4906 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4907 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00004908 SmallVector<SDValue, 8> Ops;
4909 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00004910 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilsond8a9a042010-06-04 00:04:02 +00004911 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00004912 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00004913 }
4914
Jim Grosbach24e102a2013-07-08 18:18:52 +00004915 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
4916 // know the default expansion would otherwise fall back on something even
4917 // worse. For a vector with one or two non-undef values, that's
4918 // scalar_to_vector for the elements followed by a shuffle (provided the
4919 // shuffle is valid for the target) and materialization element by element
4920 // on the stack followed by a load for everything else.
4921 if (!isConstant && !usesOnlyOneValue) {
4922 SDValue Vec = DAG.getUNDEF(VT);
4923 for (unsigned i = 0 ; i < NumElts; ++i) {
4924 SDValue V = Op.getOperand(i);
4925 if (V.getOpcode() == ISD::UNDEF)
4926 continue;
4927 SDValue LaneIdx = DAG.getConstant(i, MVT::i32);
4928 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
4929 }
4930 return Vec;
4931 }
4932
Bob Wilson2e076c42009-06-22 23:27:02 +00004933 return SDValue();
4934}
4935
Bob Wilson6f2b8962011-01-07 21:37:30 +00004936// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00004937// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00004938SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4939 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004940 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00004941 EVT VT = Op.getValueType();
4942 unsigned NumElts = VT.getVectorNumElements();
4943
4944 SmallVector<SDValue, 2> SourceVecs;
4945 SmallVector<unsigned, 2> MinElts;
4946 SmallVector<unsigned, 2> MaxElts;
Andrew Trick5eb0a302011-01-19 02:26:13 +00004947
Bob Wilson6f2b8962011-01-07 21:37:30 +00004948 for (unsigned i = 0; i < NumElts; ++i) {
4949 SDValue V = Op.getOperand(i);
4950 if (V.getOpcode() == ISD::UNDEF)
4951 continue;
4952 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4953 // A shuffle can only come from building a vector from various
4954 // elements of other vectors.
4955 return SDValue();
Eli Friedman74d1da52011-10-14 23:58:49 +00004956 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4957 VT.getVectorElementType()) {
4958 // This code doesn't know how to handle shuffles where the vector
4959 // element types do not match (this happens because type legalization
4960 // promotes the return type of EXTRACT_VECTOR_ELT).
4961 // FIXME: It might be appropriate to extend this code to handle
4962 // mismatched types.
4963 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00004964 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00004965
Bob Wilson6f2b8962011-01-07 21:37:30 +00004966 // Record this extraction against the appropriate vector if possible...
4967 SDValue SourceVec = V.getOperand(0);
Jim Grosbach6df755c2012-07-25 17:02:47 +00004968 // If the element number isn't a constant, we can't effectively
4969 // analyze what's going on.
4970 if (!isa<ConstantSDNode>(V.getOperand(1)))
4971 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00004972 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4973 bool FoundSource = false;
4974 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4975 if (SourceVecs[j] == SourceVec) {
4976 if (MinElts[j] > EltNo)
4977 MinElts[j] = EltNo;
4978 if (MaxElts[j] < EltNo)
4979 MaxElts[j] = EltNo;
4980 FoundSource = true;
4981 break;
4982 }
4983 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00004984
Bob Wilson6f2b8962011-01-07 21:37:30 +00004985 // Or record a new source if not...
4986 if (!FoundSource) {
4987 SourceVecs.push_back(SourceVec);
4988 MinElts.push_back(EltNo);
4989 MaxElts.push_back(EltNo);
4990 }
4991 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00004992
Bob Wilson6f2b8962011-01-07 21:37:30 +00004993 // Currently only do something sane when at most two source vectors
4994 // involved.
4995 if (SourceVecs.size() > 2)
4996 return SDValue();
4997
4998 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4999 int VEXTOffsets[2] = {0, 0};
Andrew Trick5eb0a302011-01-19 02:26:13 +00005000
Bob Wilson6f2b8962011-01-07 21:37:30 +00005001 // This loop extracts the usage patterns of the source vectors
5002 // and prepares appropriate SDValues for a shuffle if possible.
5003 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
5004 if (SourceVecs[i].getValueType() == VT) {
5005 // No VEXT necessary
5006 ShuffleSrcs[i] = SourceVecs[i];
5007 VEXTOffsets[i] = 0;
5008 continue;
5009 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
5010 // It probably isn't worth padding out a smaller vector just to
5011 // break it down again in a shuffle.
5012 return SDValue();
5013 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005014
Bob Wilson6f2b8962011-01-07 21:37:30 +00005015 // Since only 64-bit and 128-bit vectors are legal on ARM and
5016 // we've eliminated the other cases...
Bob Wilson3fa9c062011-01-07 23:40:46 +00005017 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
5018 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick5eb0a302011-01-19 02:26:13 +00005019
Bob Wilson6f2b8962011-01-07 21:37:30 +00005020 if (MaxElts[i] - MinElts[i] >= NumElts) {
5021 // Span too large for a VEXT to cope
5022 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005023 }
5024
Bob Wilson6f2b8962011-01-07 21:37:30 +00005025 if (MinElts[i] >= NumElts) {
5026 // The extraction can just take the second half
5027 VEXTOffsets[i] = NumElts;
Eric Christopher2af95512011-01-14 23:50:53 +00005028 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5029 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005030 DAG.getIntPtrConstant(NumElts));
5031 } else if (MaxElts[i] < NumElts) {
5032 // The extraction can just take the first half
5033 VEXTOffsets[i] = 0;
Eric Christopher2af95512011-01-14 23:50:53 +00005034 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5035 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005036 DAG.getIntPtrConstant(0));
5037 } else {
5038 // An actual VEXT is needed
5039 VEXTOffsets[i] = MinElts[i];
Eric Christopher2af95512011-01-14 23:50:53 +00005040 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5041 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005042 DAG.getIntPtrConstant(0));
Eric Christopher2af95512011-01-14 23:50:53 +00005043 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5044 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005045 DAG.getIntPtrConstant(NumElts));
5046 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
5047 DAG.getConstant(VEXTOffsets[i], MVT::i32));
5048 }
5049 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005050
Bob Wilson6f2b8962011-01-07 21:37:30 +00005051 SmallVector<int, 8> Mask;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005052
Bob Wilson6f2b8962011-01-07 21:37:30 +00005053 for (unsigned i = 0; i < NumElts; ++i) {
5054 SDValue Entry = Op.getOperand(i);
5055 if (Entry.getOpcode() == ISD::UNDEF) {
5056 Mask.push_back(-1);
5057 continue;
5058 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005059
Bob Wilson6f2b8962011-01-07 21:37:30 +00005060 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher2af95512011-01-14 23:50:53 +00005061 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
5062 .getOperand(1))->getSExtValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005063 if (ExtractVec == SourceVecs[0]) {
5064 Mask.push_back(ExtractElt - VEXTOffsets[0]);
5065 } else {
5066 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
5067 }
5068 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005069
Bob Wilson6f2b8962011-01-07 21:37:30 +00005070 // Final check before we try to produce nonsense...
5071 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher2af95512011-01-14 23:50:53 +00005072 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
5073 &Mask[0]);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005074
Bob Wilson6f2b8962011-01-07 21:37:30 +00005075 return SDValue();
5076}
5077
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005078/// isShuffleMaskLegal - Targets can use this to indicate that they only
5079/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5080/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5081/// are assumed to be legal.
5082bool
5083ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5084 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005085 if (VT.getVectorNumElements() == 4 &&
5086 (VT.is128BitVector() || VT.is64BitVector())) {
5087 unsigned PFIndexes[4];
5088 for (unsigned i = 0; i != 4; ++i) {
5089 if (M[i] < 0)
5090 PFIndexes[i] = 8;
5091 else
5092 PFIndexes[i] = M[i];
5093 }
5094
5095 // Compute the index in the perfect shuffle table.
5096 unsigned PFTableIndex =
5097 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5098 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5099 unsigned Cost = (PFEntry >> 30);
5100
5101 if (Cost <= 4)
5102 return true;
5103 }
5104
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005105 bool ReverseVEXT;
Bob Wilsona7062312009-08-21 20:54:19 +00005106 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005107
Bob Wilson846bd792010-06-07 23:53:38 +00005108 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5109 return (EltSize >= 32 ||
5110 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005111 isVREVMask(M, VT, 64) ||
5112 isVREVMask(M, VT, 32) ||
5113 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005114 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005115 isVTBLMask(M, VT) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005116 isVTRNMask(M, VT, WhichResult) ||
5117 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson0bbd3072009-12-03 06:40:55 +00005118 isVZIPMask(M, VT, WhichResult) ||
5119 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
5120 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005121 isVZIP_v_undef_Mask(M, VT, WhichResult) ||
5122 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005123}
5124
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005125/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5126/// the specified operations to build the shuffle.
5127static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5128 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005129 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005130 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5131 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5132 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5133
5134 enum {
5135 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5136 OP_VREV,
5137 OP_VDUP0,
5138 OP_VDUP1,
5139 OP_VDUP2,
5140 OP_VDUP3,
5141 OP_VEXT1,
5142 OP_VEXT2,
5143 OP_VEXT3,
5144 OP_VUZPL, // VUZP, left result
5145 OP_VUZPR, // VUZP, right result
5146 OP_VZIPL, // VZIP, left result
5147 OP_VZIPR, // VZIP, right result
5148 OP_VTRNL, // VTRN, left result
5149 OP_VTRNR // VTRN, right result
5150 };
5151
5152 if (OpNum == OP_COPY) {
5153 if (LHSID == (1*9+2)*9+3) return LHS;
5154 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5155 return RHS;
5156 }
5157
5158 SDValue OpLHS, OpRHS;
5159 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5160 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5161 EVT VT = OpLHS.getValueType();
5162
5163 switch (OpNum) {
5164 default: llvm_unreachable("Unknown shuffle opcode!");
5165 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00005166 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00005167 if (VT.getVectorElementType() == MVT::i32 ||
5168 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00005169 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
5170 // vrev <4 x i16> -> VREV32
5171 if (VT.getVectorElementType() == MVT::i16)
5172 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
5173 // vrev <4 x i8> -> VREV16
5174 assert(VT.getVectorElementType() == MVT::i8);
5175 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005176 case OP_VDUP0:
5177 case OP_VDUP1:
5178 case OP_VDUP2:
5179 case OP_VDUP3:
5180 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005181 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005182 case OP_VEXT1:
5183 case OP_VEXT2:
5184 case OP_VEXT3:
5185 return DAG.getNode(ARMISD::VEXT, dl, VT,
5186 OpLHS, OpRHS,
5187 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
5188 case OP_VUZPL:
5189 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005190 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005191 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
5192 case OP_VZIPL:
5193 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005194 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005195 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
5196 case OP_VTRNL:
5197 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005198 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5199 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005200 }
5201}
5202
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005203static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005204 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005205 SelectionDAG &DAG) {
5206 // Check to see if we can use the VTBL instruction.
5207 SDValue V1 = Op.getOperand(0);
5208 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005209 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005210
5211 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00005212 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005213 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
5214 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
5215
5216 if (V2.getNode()->getOpcode() == ISD::UNDEF)
5217 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
5218 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
5219 &VTBLMask[0], 8));
Bill Wendlingebecb332011-03-15 20:47:26 +00005220
Owen Anderson77aa2662011-04-05 21:48:57 +00005221 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlingebecb332011-03-15 20:47:26 +00005222 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
5223 &VTBLMask[0], 8));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005224}
5225
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005226static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
5227 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005228 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005229 SDValue OpLHS = Op.getOperand(0);
5230 EVT VT = OpLHS.getValueType();
5231
5232 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
5233 "Expect an v8i16/v16i8 type");
5234 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
5235 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
5236 // extract the first 8 bytes into the top double word and the last 8 bytes
5237 // into the bottom double word. The v8i16 case is similar.
5238 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
5239 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
5240 DAG.getConstant(ExtractNum, MVT::i32));
5241}
5242
Bob Wilson2e076c42009-06-22 23:27:02 +00005243static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005244 SDValue V1 = Op.getOperand(0);
5245 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005246 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00005247 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005248 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00005249
Bob Wilsonc6800b52009-08-13 02:13:04 +00005250 // Convert shuffles that are directly supported on NEON to target-specific
5251 // DAG nodes, instead of keeping them as shuffles and matching them again
5252 // during code selection. This is more efficient and avoids the possibility
5253 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00005254 // FIXME: floating-point vectors should be canonicalized to integer vectors
5255 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005256 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005257
Bob Wilson846bd792010-06-07 23:53:38 +00005258 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5259 if (EltSize <= 32) {
5260 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
5261 int Lane = SVN->getSplatIndex();
5262 // If this is undef splat, generate it via "just" vdup, if possible.
5263 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00005264
Dan Gohman198b7ff2011-11-03 21:49:52 +00005265 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00005266 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5267 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5268 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005269 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
5270 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
5271 // reaches it).
5272 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
5273 !isa<ConstantSDNode>(V1.getOperand(0))) {
5274 bool IsScalarToVector = true;
5275 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
5276 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
5277 IsScalarToVector = false;
5278 break;
5279 }
5280 if (IsScalarToVector)
5281 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5282 }
Bob Wilson846bd792010-06-07 23:53:38 +00005283 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
5284 DAG.getConstant(Lane, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00005285 }
Bob Wilson846bd792010-06-07 23:53:38 +00005286
5287 bool ReverseVEXT;
5288 unsigned Imm;
5289 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
5290 if (ReverseVEXT)
5291 std::swap(V1, V2);
5292 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
5293 DAG.getConstant(Imm, MVT::i32));
5294 }
5295
5296 if (isVREVMask(ShuffleMask, VT, 64))
5297 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
5298 if (isVREVMask(ShuffleMask, VT, 32))
5299 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
5300 if (isVREVMask(ShuffleMask, VT, 16))
5301 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
5302
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005303 if (V2->getOpcode() == ISD::UNDEF &&
5304 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
5305 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
5306 DAG.getConstant(Imm, MVT::i32));
5307 }
5308
Bob Wilson846bd792010-06-07 23:53:38 +00005309 // Check for Neon shuffles that modify both input vectors in place.
5310 // If both results are used, i.e., if there are two shuffles with the same
5311 // source operands and with masks corresponding to both results of one of
5312 // these operations, DAG memoization will ensure that a single node is
5313 // used for both shuffles.
5314 unsigned WhichResult;
5315 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5316 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5317 V1, V2).getValue(WhichResult);
5318 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5319 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5320 V1, V2).getValue(WhichResult);
5321 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5322 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5323 V1, V2).getValue(WhichResult);
5324
5325 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5326 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5327 V1, V1).getValue(WhichResult);
5328 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5329 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5330 V1, V1).getValue(WhichResult);
5331 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5332 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5333 V1, V1).getValue(WhichResult);
Bob Wilsoncce31f62009-08-14 05:08:32 +00005334 }
Bob Wilson32cd8552009-08-19 17:03:43 +00005335
Bob Wilsona7062312009-08-21 20:54:19 +00005336 // If the shuffle is not directly supported and it has 4 elements, use
5337 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00005338 unsigned NumElts = VT.getVectorNumElements();
5339 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005340 unsigned PFIndexes[4];
5341 for (unsigned i = 0; i != 4; ++i) {
5342 if (ShuffleMask[i] < 0)
5343 PFIndexes[i] = 8;
5344 else
5345 PFIndexes[i] = ShuffleMask[i];
5346 }
5347
5348 // Compute the index in the perfect shuffle table.
5349 unsigned PFTableIndex =
5350 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005351 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5352 unsigned Cost = (PFEntry >> 30);
5353
5354 if (Cost <= 4)
5355 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
5356 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00005357
Bob Wilsond8a9a042010-06-04 00:04:02 +00005358 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00005359 if (EltSize >= 32) {
5360 // Do the expansion with floating-point types, since that is what the VFP
5361 // registers are defined to use, and since i64 is not legal.
5362 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5363 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00005364 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
5365 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005366 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00005367 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00005368 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00005369 Ops.push_back(DAG.getUNDEF(EltVT));
5370 else
5371 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
5372 ShuffleMask[i] < (int)NumElts ? V1 : V2,
5373 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
5374 MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00005375 }
Bob Wilsond8a9a042010-06-04 00:04:02 +00005376 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00005377 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00005378 }
5379
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005380 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
5381 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
5382
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005383 if (VT == MVT::v8i8) {
5384 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
5385 if (NewOp.getNode())
5386 return NewOp;
5387 }
5388
Bob Wilson6f34e272009-08-14 05:16:33 +00005389 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005390}
5391
Eli Friedmana5e244c2011-10-24 23:08:52 +00005392static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
5393 // INSERT_VECTOR_ELT is legal only for immediate indexes.
5394 SDValue Lane = Op.getOperand(2);
5395 if (!isa<ConstantSDNode>(Lane))
5396 return SDValue();
5397
5398 return Op;
5399}
5400
Bob Wilson2e076c42009-06-22 23:27:02 +00005401static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00005402 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00005403 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00005404 if (!isa<ConstantSDNode>(Lane))
5405 return SDValue();
5406
5407 SDValue Vec = Op.getOperand(0);
5408 if (Op.getValueType() == MVT::i32 &&
5409 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005410 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00005411 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
5412 }
5413
5414 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00005415}
5416
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005417static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5418 // The only time a CONCAT_VECTORS operation can have legal types is when
5419 // two 64-bit vectors are concatenated to a 128-bit vector.
5420 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
5421 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005422 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005423 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005424 SDValue Op0 = Op.getOperand(0);
5425 SDValue Op1 = Op.getOperand(1);
5426 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005427 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005428 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005429 DAG.getIntPtrConstant(0));
5430 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005431 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005432 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005433 DAG.getIntPtrConstant(1));
Wesley Peck527da1b2010-11-23 03:31:01 +00005434 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005435}
5436
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005437/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
5438/// element has been zero/sign-extended, depending on the isSigned parameter,
5439/// from an integer type half its size.
5440static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
5441 bool isSigned) {
5442 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
5443 EVT VT = N->getValueType(0);
5444 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
5445 SDNode *BVN = N->getOperand(0).getNode();
5446 if (BVN->getValueType(0) != MVT::v4i32 ||
5447 BVN->getOpcode() != ISD::BUILD_VECTOR)
5448 return false;
5449 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5450 unsigned HiElt = 1 - LoElt;
5451 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
5452 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
5453 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
5454 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
5455 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
5456 return false;
5457 if (isSigned) {
5458 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
5459 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
5460 return true;
5461 } else {
5462 if (Hi0->isNullValue() && Hi1->isNullValue())
5463 return true;
5464 }
5465 return false;
5466 }
5467
5468 if (N->getOpcode() != ISD::BUILD_VECTOR)
5469 return false;
5470
5471 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5472 SDNode *Elt = N->getOperand(i).getNode();
5473 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
5474 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5475 unsigned HalfSize = EltSize / 2;
5476 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005477 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005478 return false;
5479 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005480 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005481 return false;
5482 }
5483 continue;
5484 }
5485 return false;
5486 }
5487
5488 return true;
5489}
5490
5491/// isSignExtended - Check if a node is a vector value that is sign-extended
5492/// or a constant BUILD_VECTOR with sign-extended elements.
5493static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
5494 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
5495 return true;
5496 if (isExtendedBUILD_VECTOR(N, DAG, true))
5497 return true;
5498 return false;
5499}
5500
5501/// isZeroExtended - Check if a node is a vector value that is zero-extended
5502/// or a constant BUILD_VECTOR with zero-extended elements.
5503static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
5504 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
5505 return true;
5506 if (isExtendedBUILD_VECTOR(N, DAG, false))
5507 return true;
5508 return false;
5509}
5510
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005511static EVT getExtensionTo64Bits(const EVT &OrigVT) {
5512 if (OrigVT.getSizeInBits() >= 64)
5513 return OrigVT;
5514
5515 assert(OrigVT.isSimple() && "Expecting a simple value type");
5516
5517 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
5518 switch (OrigSimpleTy) {
5519 default: llvm_unreachable("Unexpected Vector Type");
5520 case MVT::v2i8:
5521 case MVT::v2i16:
5522 return MVT::v2i32;
5523 case MVT::v4i8:
5524 return MVT::v4i16;
5525 }
5526}
5527
Sebastian Popa204f722012-11-30 19:08:04 +00005528/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
5529/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
5530/// We insert the required extension here to get the vector to fill a D register.
5531static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
5532 const EVT &OrigTy,
5533 const EVT &ExtTy,
5534 unsigned ExtOpcode) {
5535 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
5536 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
5537 // 64-bits we need to insert a new extension so that it will be 64-bits.
5538 assert(ExtTy.is128BitVector() && "Unexpected extension size");
5539 if (OrigTy.getSizeInBits() >= 64)
5540 return N;
5541
5542 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005543 EVT NewVT = getExtensionTo64Bits(OrigTy);
5544
Andrew Trickef9de2a2013-05-25 02:42:55 +00005545 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00005546}
5547
5548/// SkipLoadExtensionForVMULL - return a load of the original vector size that
5549/// does not do any sign/zero extension. If the original vector is less
5550/// than 64 bits, an appropriate extension will be added after the load to
5551/// reach a total size of 64 bits. We have to add the extension separately
5552/// because ARM does not have a sign/zero extending load for vectors.
5553static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005554 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
5555
5556 // The load already has the right type.
5557 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00005558 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00005559 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
5560 LD->isNonTemporal(), LD->isInvariant(),
5561 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005562
5563 // We need to create a zextload/sextload. We cannot just create a load
5564 // followed by a zext/zext node because LowerMUL is also run during normal
5565 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005566 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005567 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
5568 LD->getMemoryVT(), LD->isVolatile(),
5569 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00005570}
5571
5572/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
5573/// extending load, or BUILD_VECTOR with extended elements, return the
5574/// unextended value. The unextended vector should be 64 bits so that it can
5575/// be used as an operand to a VMULL instruction. If the original vector size
5576/// before extension is less than 64 bits we add a an extension to resize
5577/// the vector to 64 bits.
5578static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00005579 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00005580 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
5581 N->getOperand(0)->getValueType(0),
5582 N->getValueType(0),
5583 N->getOpcode());
5584
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005585 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00005586 return SkipLoadExtensionForVMULL(LD, DAG);
5587
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005588 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
5589 // have been legalized as a BITCAST from v4i32.
5590 if (N->getOpcode() == ISD::BITCAST) {
5591 SDNode *BVN = N->getOperand(0).getNode();
5592 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
5593 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
5594 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00005595 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005596 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
5597 }
5598 // Construct a new BUILD_VECTOR with elements truncated to half the size.
5599 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
5600 EVT VT = N->getValueType(0);
5601 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
5602 unsigned NumElts = VT.getVectorNumElements();
5603 MVT TruncVT = MVT::getIntegerVT(EltSize);
5604 SmallVector<SDValue, 8> Ops;
5605 for (unsigned i = 0; i != NumElts; ++i) {
5606 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
5607 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00005608 // Element types smaller than 32 bits are not legal, so use i32 elements.
5609 // The values are implicitly truncated so sext vs. zext doesn't matter.
5610 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005611 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005612 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005613 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilson38ab35a2010-09-01 23:50:19 +00005614}
5615
Evan Chenge2086e72011-03-29 01:56:09 +00005616static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
5617 unsigned Opcode = N->getOpcode();
5618 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5619 SDNode *N0 = N->getOperand(0).getNode();
5620 SDNode *N1 = N->getOperand(1).getNode();
5621 return N0->hasOneUse() && N1->hasOneUse() &&
5622 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
5623 }
5624 return false;
5625}
5626
5627static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
5628 unsigned Opcode = N->getOpcode();
5629 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5630 SDNode *N0 = N->getOperand(0).getNode();
5631 SDNode *N1 = N->getOperand(1).getNode();
5632 return N0->hasOneUse() && N1->hasOneUse() &&
5633 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
5634 }
5635 return false;
5636}
5637
Bob Wilson38ab35a2010-09-01 23:50:19 +00005638static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
5639 // Multiplications are only custom-lowered for 128-bit vectors so that
5640 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
5641 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00005642 assert(VT.is128BitVector() && VT.isInteger() &&
5643 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00005644 SDNode *N0 = Op.getOperand(0).getNode();
5645 SDNode *N1 = Op.getOperand(1).getNode();
5646 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00005647 bool isMLA = false;
5648 bool isN0SExt = isSignExtended(N0, DAG);
5649 bool isN1SExt = isSignExtended(N1, DAG);
5650 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00005651 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00005652 else {
5653 bool isN0ZExt = isZeroExtended(N0, DAG);
5654 bool isN1ZExt = isZeroExtended(N1, DAG);
5655 if (isN0ZExt && isN1ZExt)
5656 NewOpc = ARMISD::VMULLu;
5657 else if (isN1SExt || isN1ZExt) {
5658 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
5659 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
5660 if (isN1SExt && isAddSubSExt(N0, DAG)) {
5661 NewOpc = ARMISD::VMULLs;
5662 isMLA = true;
5663 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
5664 NewOpc = ARMISD::VMULLu;
5665 isMLA = true;
5666 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
5667 std::swap(N0, N1);
5668 NewOpc = ARMISD::VMULLu;
5669 isMLA = true;
5670 }
5671 }
5672
5673 if (!NewOpc) {
5674 if (VT == MVT::v2i64)
5675 // Fall through to expand this. It is not legal.
5676 return SDValue();
5677 else
5678 // Other vector multiplications are legal.
5679 return Op;
5680 }
5681 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00005682
5683 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005684 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00005685 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00005686 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005687 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00005688 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005689 assert(Op0.getValueType().is64BitVector() &&
5690 Op1.getValueType().is64BitVector() &&
5691 "unexpected types for extended operands to VMULL");
5692 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
5693 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00005694
Evan Chenge2086e72011-03-29 01:56:09 +00005695 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
5696 // isel lowering to take advantage of no-stall back to back vmul + vmla.
5697 // vmull q0, d4, d6
5698 // vmlal q0, d5, d6
5699 // is faster than
5700 // vaddl q0, d4, d5
5701 // vmovl q1, d6
5702 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00005703 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
5704 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005705 EVT Op1VT = Op1.getValueType();
5706 return DAG.getNode(N0->getOpcode(), DL, VT,
5707 DAG.getNode(NewOpc, DL, VT,
5708 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
5709 DAG.getNode(NewOpc, DL, VT,
5710 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00005711}
5712
Owen Anderson77aa2662011-04-05 21:48:57 +00005713static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005714LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00005715 // Convert to float
5716 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
5717 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
5718 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
5719 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
5720 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
5721 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
5722 // Get reciprocal estimate.
5723 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00005724 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005725 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
5726 // Because char has a smaller range than uchar, we can actually get away
5727 // without any newton steps. This requires that we use a weird bias
5728 // of 0xb000, however (again, this has been exhaustively tested).
5729 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
5730 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
5731 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
5732 Y = DAG.getConstant(0xb000, MVT::i32);
5733 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
5734 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
5735 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
5736 // Convert back to short.
5737 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
5738 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
5739 return X;
5740}
5741
Owen Anderson77aa2662011-04-05 21:48:57 +00005742static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005743LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00005744 SDValue N2;
5745 // Convert to float.
5746 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
5747 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
5748 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
5749 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
5750 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
5751 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005752
Nate Begemanfa62d502011-02-11 20:53:29 +00005753 // Use reciprocal estimate and one refinement step.
5754 // float4 recip = vrecpeq_f32(yf);
5755 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00005756 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005757 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005758 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005759 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
5760 N1, N2);
5761 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5762 // Because short has a smaller range than ushort, we can actually get away
5763 // with only a single newton step. This requires that we use a weird bias
5764 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005765 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00005766 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5767 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005768 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00005769 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5770 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5771 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5772 // Convert back to integer and return.
5773 // return vmovn_s32(vcvt_s32_f32(result));
5774 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5775 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5776 return N0;
5777}
5778
5779static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
5780 EVT VT = Op.getValueType();
5781 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5782 "unexpected type for custom-lowering ISD::SDIV");
5783
Andrew Trickef9de2a2013-05-25 02:42:55 +00005784 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00005785 SDValue N0 = Op.getOperand(0);
5786 SDValue N1 = Op.getOperand(1);
5787 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00005788
Nate Begemanfa62d502011-02-11 20:53:29 +00005789 if (VT == MVT::v8i8) {
5790 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
5791 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005792
Nate Begemanfa62d502011-02-11 20:53:29 +00005793 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5794 DAG.getIntPtrConstant(4));
5795 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00005796 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00005797 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5798 DAG.getIntPtrConstant(0));
5799 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5800 DAG.getIntPtrConstant(0));
5801
5802 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5803 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5804
5805 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5806 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00005807
Nate Begemanfa62d502011-02-11 20:53:29 +00005808 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5809 return N0;
5810 }
5811 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5812}
5813
5814static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5815 EVT VT = Op.getValueType();
5816 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5817 "unexpected type for custom-lowering ISD::UDIV");
5818
Andrew Trickef9de2a2013-05-25 02:42:55 +00005819 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00005820 SDValue N0 = Op.getOperand(0);
5821 SDValue N1 = Op.getOperand(1);
5822 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00005823
Nate Begemanfa62d502011-02-11 20:53:29 +00005824 if (VT == MVT::v8i8) {
5825 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5826 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005827
Nate Begemanfa62d502011-02-11 20:53:29 +00005828 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5829 DAG.getIntPtrConstant(4));
5830 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00005831 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00005832 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5833 DAG.getIntPtrConstant(0));
5834 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5835 DAG.getIntPtrConstant(0));
Owen Anderson77aa2662011-04-05 21:48:57 +00005836
Nate Begemanfa62d502011-02-11 20:53:29 +00005837 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5838 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00005839
Nate Begemanfa62d502011-02-11 20:53:29 +00005840 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5841 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00005842
5843 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begemanfa62d502011-02-11 20:53:29 +00005844 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5845 N0);
5846 return N0;
5847 }
Owen Anderson77aa2662011-04-05 21:48:57 +00005848
Nate Begemanfa62d502011-02-11 20:53:29 +00005849 // v4i16 sdiv ... Convert to float.
5850 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5851 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5852 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5853 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5854 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005855 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00005856
5857 // Use reciprocal estimate and two refinement steps.
5858 // float4 recip = vrecpeq_f32(yf);
5859 // recip *= vrecpsq_f32(yf, recip);
5860 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00005861 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005862 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005863 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005864 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005865 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00005866 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00005867 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005868 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005869 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00005870 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5871 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5872 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5873 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005874 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00005875 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5876 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5877 N1 = DAG.getConstant(2, MVT::i32);
5878 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5879 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5880 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5881 // Convert back to integer and return.
5882 // return vmovn_u32(vcvt_s32_f32(result));
5883 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5884 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5885 return N0;
5886}
5887
Evan Chenge8916542011-08-30 01:34:54 +00005888static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5889 EVT VT = Op.getNode()->getValueType(0);
5890 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5891
5892 unsigned Opc;
5893 bool ExtraOp = false;
5894 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00005895 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00005896 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5897 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5898 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5899 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5900 }
5901
5902 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005903 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00005904 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00005905 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00005906 Op.getOperand(1), Op.getOperand(2));
5907}
5908
Bob Wilsone7dde0c2013-11-03 06:14:38 +00005909SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
5910 assert(Subtarget->isTargetDarwin());
5911
5912 // For iOS, we want to call an alternative entry point: __sincos_stret,
5913 // return values are passed via sret.
5914 SDLoc dl(Op);
5915 SDValue Arg = Op.getOperand(0);
5916 EVT ArgVT = Arg.getValueType();
5917 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
5918
5919 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
5920 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5921
5922 // Pair of floats / doubles used to pass the result.
5923 StructType *RetTy = StructType::get(ArgTy, ArgTy, NULL);
5924
5925 // Create stack object for sret.
5926 const uint64_t ByteSize = TLI.getDataLayout()->getTypeAllocSize(RetTy);
5927 const unsigned StackAlign = TLI.getDataLayout()->getPrefTypeAlignment(RetTy);
5928 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
5929 SDValue SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
5930
5931 ArgListTy Args;
5932 ArgListEntry Entry;
5933
5934 Entry.Node = SRet;
5935 Entry.Ty = RetTy->getPointerTo();
5936 Entry.isSExt = false;
5937 Entry.isZExt = false;
5938 Entry.isSRet = true;
5939 Args.push_back(Entry);
5940
5941 Entry.Node = Arg;
5942 Entry.Ty = ArgTy;
5943 Entry.isSExt = false;
5944 Entry.isZExt = false;
5945 Args.push_back(Entry);
5946
5947 const char *LibcallName = (ArgVT == MVT::f64)
5948 ? "__sincos_stret" : "__sincosf_stret";
5949 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
5950
5951 TargetLowering::
5952 CallLoweringInfo CLI(DAG.getEntryNode(), Type::getVoidTy(*DAG.getContext()),
5953 false, false, false, false, 0,
5954 CallingConv::C, /*isTaillCall=*/false,
5955 /*doesNotRet=*/false, /*isReturnValueUsed*/false,
5956 Callee, Args, DAG, dl);
5957 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
5958
5959 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
5960 MachinePointerInfo(), false, false, false, 0);
5961
5962 // Address of cos field.
5963 SDValue Add = DAG.getNode(ISD::ADD, dl, getPointerTy(), SRet,
5964 DAG.getIntPtrConstant(ArgVT.getStoreSize()));
5965 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
5966 MachinePointerInfo(), false, false, false, 0);
5967
5968 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
5969 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
5970 LoadSin.getValue(0), LoadCos.getValue(0));
5971}
5972
Eli Friedman10f9ce22011-09-15 22:26:18 +00005973static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00005974 // Monotonic load/store is legal for all targets
5975 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5976 return Op;
5977
Alp Tokercb402912014-01-24 17:20:08 +00005978 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00005979 // dmb or equivalent available.
5980 return SDValue();
5981}
5982
Tim Northoverbc933082013-05-23 19:11:20 +00005983static void ReplaceREADCYCLECOUNTER(SDNode *N,
5984 SmallVectorImpl<SDValue> &Results,
5985 SelectionDAG &DAG,
5986 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005987 SDLoc DL(N);
Tim Northoverbc933082013-05-23 19:11:20 +00005988 SDValue Cycles32, OutChain;
5989
5990 if (Subtarget->hasPerfMon()) {
5991 // Under Power Management extensions, the cycle-count is:
5992 // mrc p15, #0, <Rt>, c9, c13, #0
5993 SDValue Ops[] = { N->getOperand(0), // Chain
5994 DAG.getConstant(Intrinsic::arm_mrc, MVT::i32),
5995 DAG.getConstant(15, MVT::i32),
5996 DAG.getConstant(0, MVT::i32),
5997 DAG.getConstant(9, MVT::i32),
5998 DAG.getConstant(13, MVT::i32),
5999 DAG.getConstant(0, MVT::i32)
6000 };
6001
6002 Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
6003 DAG.getVTList(MVT::i32, MVT::Other), &Ops[0],
6004 array_lengthof(Ops));
6005 OutChain = Cycles32.getValue(1);
6006 } else {
6007 // Intrinsic is defined to return 0 on unsupported platforms. Technically
6008 // there are older ARM CPUs that have implementation-specific ways of
6009 // obtaining this information (FIXME!).
6010 Cycles32 = DAG.getConstant(0, MVT::i32);
6011 OutChain = DAG.getEntryNode();
6012 }
6013
6014
6015 SDValue Cycles64 = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64,
6016 Cycles32, DAG.getConstant(0, MVT::i32));
6017 Results.push_back(Cycles64);
6018 Results.push_back(OutChain);
6019}
6020
Dan Gohman21cea8a2010-04-17 15:26:15 +00006021SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006022 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006023 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Cheng10043e22007-01-19 07:51:42 +00006024 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006025 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006026 case ISD::GlobalAddress:
Tim Northoverd6a729b2014-01-06 14:28:05 +00006027 return Subtarget->isTargetMachO() ? LowerGlobalAddressDarwin(Op, DAG) :
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006028 LowerGlobalAddressELF(Op, DAG);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006029 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006030 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006031 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6032 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006033 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006034 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006035 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006036 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006037 case ISD::SINT_TO_FP:
6038 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6039 case ISD::FP_TO_SINT:
6040 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006041 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006042 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006043 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006044 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006045 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006046 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006047 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6048 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006049 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006050 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006051 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006052 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng15b80e42009-11-12 07:13:11 +00006053 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00006054 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00006055 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach8546ec92010-01-18 19:58:49 +00006056 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00006057 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00006058 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00006059 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006060 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00006061 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00006062 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006063 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006064 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00006065 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006066 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begemanfa62d502011-02-11 20:53:29 +00006067 case ISD::SDIV: return LowerSDIV(Op, DAG);
6068 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00006069 case ISD::ADDC:
6070 case ISD::ADDE:
6071 case ISD::SUBC:
6072 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00006073 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00006074 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006075 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00006076 case ISD::SDIVREM:
6077 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006078 }
Evan Cheng10043e22007-01-19 07:51:42 +00006079}
6080
Duncan Sands6ed40142008-12-01 11:39:25 +00006081/// ReplaceNodeResults - Replace the results of node with an illegal result
6082/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00006083void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
6084 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006085 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00006086 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006087 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006088 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00006089 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peck527da1b2010-11-23 03:31:01 +00006090 case ISD::BITCAST:
6091 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006092 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006093 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00006094 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00006095 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006096 break;
Tim Northoverbc933082013-05-23 19:11:20 +00006097 case ISD::READCYCLECOUNTER:
6098 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
6099 return;
Duncan Sands6ed40142008-12-01 11:39:25 +00006100 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00006101 if (Res.getNode())
6102 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00006103}
Chris Lattnerf81d5882007-11-24 07:07:01 +00006104
Evan Cheng10043e22007-01-19 07:51:42 +00006105//===----------------------------------------------------------------------===//
6106// ARM Scheduler Hooks
6107//===----------------------------------------------------------------------===//
6108
Bill Wendling030b58e2011-10-06 22:18:16 +00006109/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6110/// registers the function context.
6111void ARMTargetLowering::
6112SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6113 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendling374ee192011-10-03 21:25:38 +00006114 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6115 DebugLoc dl = MI->getDebugLoc();
6116 MachineFunction *MF = MBB->getParent();
6117 MachineRegisterInfo *MRI = &MF->getRegInfo();
6118 MachineConstantPool *MCP = MF->getConstantPool();
6119 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6120 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00006121
Bill Wendling374ee192011-10-03 21:25:38 +00006122 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006123 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00006124
Bill Wendling374ee192011-10-03 21:25:38 +00006125 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006126 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00006127 ARMConstantPoolValue *CPV =
6128 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6129 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6130
Craig Topperc7242e02012-04-20 07:30:17 +00006131 const TargetRegisterClass *TRC = isThumb ?
6132 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6133 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00006134
Bill Wendling030b58e2011-10-06 22:18:16 +00006135 // Grab constant pool and fixed stack memory operands.
6136 MachineMemOperand *CPMMO =
6137 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6138 MachineMemOperand::MOLoad, 4, 4);
6139
6140 MachineMemOperand *FIMMOSt =
6141 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6142 MachineMemOperand::MOStore, 4, 4);
6143
6144 // Load the address of the dispatch MBB into the jump buffer.
6145 if (isThumb2) {
6146 // Incoming value: jbuf
6147 // ldr.n r5, LCPI1_1
6148 // orr r5, r5, #1
6149 // add r5, pc
6150 // str r5, [$jbuf, #+4] ; &jbuf[1]
6151 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6152 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6153 .addConstantPoolIndex(CPI)
6154 .addMemOperand(CPMMO));
6155 // Set the low bit because of thumb mode.
6156 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6157 AddDefaultCC(
6158 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6159 .addReg(NewVReg1, RegState::Kill)
6160 .addImm(0x01)));
6161 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6162 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6163 .addReg(NewVReg2, RegState::Kill)
6164 .addImm(PCLabelId);
6165 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6166 .addReg(NewVReg3, RegState::Kill)
6167 .addFrameIndex(FI)
6168 .addImm(36) // &jbuf[1] :: pc
6169 .addMemOperand(FIMMOSt));
6170 } else if (isThumb) {
6171 // Incoming value: jbuf
6172 // ldr.n r1, LCPI1_4
6173 // add r1, pc
6174 // mov r2, #1
6175 // orrs r1, r2
6176 // add r2, $jbuf, #+4 ; &jbuf[1]
6177 // str r1, [r2]
6178 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6179 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6180 .addConstantPoolIndex(CPI)
6181 .addMemOperand(CPMMO));
6182 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6183 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6184 .addReg(NewVReg1, RegState::Kill)
6185 .addImm(PCLabelId);
6186 // Set the low bit because of thumb mode.
6187 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6188 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6189 .addReg(ARM::CPSR, RegState::Define)
6190 .addImm(1));
6191 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6192 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6193 .addReg(ARM::CPSR, RegState::Define)
6194 .addReg(NewVReg2, RegState::Kill)
6195 .addReg(NewVReg3, RegState::Kill));
6196 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6197 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
6198 .addFrameIndex(FI)
6199 .addImm(36)); // &jbuf[1] :: pc
6200 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6201 .addReg(NewVReg4, RegState::Kill)
6202 .addReg(NewVReg5, RegState::Kill)
6203 .addImm(0)
6204 .addMemOperand(FIMMOSt));
6205 } else {
6206 // Incoming value: jbuf
6207 // ldr r1, LCPI1_1
6208 // add r1, pc, r1
6209 // str r1, [$jbuf, #+4] ; &jbuf[1]
6210 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6211 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6212 .addConstantPoolIndex(CPI)
6213 .addImm(0)
6214 .addMemOperand(CPMMO));
6215 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6216 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6217 .addReg(NewVReg1, RegState::Kill)
6218 .addImm(PCLabelId));
6219 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6220 .addReg(NewVReg2, RegState::Kill)
6221 .addFrameIndex(FI)
6222 .addImm(36) // &jbuf[1] :: pc
6223 .addMemOperand(FIMMOSt));
6224 }
6225}
6226
6227MachineBasicBlock *ARMTargetLowering::
6228EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
6229 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6230 DebugLoc dl = MI->getDebugLoc();
6231 MachineFunction *MF = MBB->getParent();
6232 MachineRegisterInfo *MRI = &MF->getRegInfo();
6233 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6234 MachineFrameInfo *MFI = MF->getFrameInfo();
6235 int FI = MFI->getFunctionContextIndex();
6236
Craig Topperc7242e02012-04-20 07:30:17 +00006237 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
6238 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen691ae332012-05-20 06:38:47 +00006239 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00006240
Bill Wendling362c1b02011-10-06 21:29:56 +00006241 // Get a mapping of the call site numbers to all of the landing pads they're
6242 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00006243 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6244 unsigned MaxCSNum = 0;
6245 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00006246 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6247 ++BB) {
Bill Wendling202803e2011-10-05 00:02:33 +00006248 if (!BB->isLandingPad()) continue;
6249
6250 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6251 // pad.
6252 for (MachineBasicBlock::iterator
6253 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6254 if (!II->isEHLabel()) continue;
6255
6256 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006257 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00006258
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006259 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6260 for (SmallVectorImpl<unsigned>::iterator
6261 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6262 CSI != CSE; ++CSI) {
6263 CallSiteNumToLPad[*CSI].push_back(BB);
6264 MaxCSNum = std::max(MaxCSNum, *CSI);
6265 }
Bill Wendling202803e2011-10-05 00:02:33 +00006266 break;
6267 }
6268 }
6269
6270 // Get an ordered list of the machine basic blocks for the jump table.
6271 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling883ec972011-10-07 23:18:02 +00006272 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00006273 LPadList.reserve(CallSiteNumToLPad.size());
6274 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6275 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6276 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006277 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00006278 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00006279 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6280 }
Bill Wendling202803e2011-10-05 00:02:33 +00006281 }
6282
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006283 assert(!LPadList.empty() &&
6284 "No landing pad destinations for the dispatch jump table!");
6285
Bill Wendling362c1b02011-10-06 21:29:56 +00006286 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00006287 MachineJumpTableInfo *JTI =
6288 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6289 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
6290 unsigned UId = AFI->createJumpTableUId();
Chad Rosier96603432013-03-01 18:30:38 +00006291 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00006292
Bill Wendling362c1b02011-10-06 21:29:56 +00006293 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00006294
6295 // Shove the dispatch's address into the return slot in the function context.
6296 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6297 DispatchBB->setIsLandingPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00006298
Bill Wendling324be982011-10-05 00:39:32 +00006299 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006300 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00006301 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006302 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00006303 else
6304 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
6305
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006306 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00006307 DispatchBB->addSuccessor(TrapBB);
6308
6309 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6310 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00006311
Bill Wendling510fbcd2011-10-17 21:32:56 +00006312 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00006313 MF->insert(MF->end(), DispatchBB);
6314 MF->insert(MF->end(), DispContBB);
6315 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00006316
Bill Wendling030b58e2011-10-06 22:18:16 +00006317 // Insert code into the entry block that creates and registers the function
6318 // context.
6319 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6320
Bill Wendling030b58e2011-10-06 22:18:16 +00006321 MachineMemOperand *FIMMOLd =
Bill Wendling362c1b02011-10-06 21:29:56 +00006322 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendlingb3d46782011-10-06 23:37:36 +00006323 MachineMemOperand::MOLoad |
6324 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00006325
Chad Rosier1ec8e402012-11-06 23:05:24 +00006326 MachineInstrBuilder MIB;
6327 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6328
6329 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6330 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6331
6332 // Add a register mask with no preserved registers. This results in all
6333 // registers being marked as clobbered.
6334 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00006335
Bill Wendling85833f72011-10-18 22:49:07 +00006336 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00006337 if (Subtarget->isThumb2()) {
6338 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6339 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6340 .addFrameIndex(FI)
6341 .addImm(4)
6342 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006343
Bill Wendling85833f72011-10-18 22:49:07 +00006344 if (NumLPads < 256) {
6345 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6346 .addReg(NewVReg1)
6347 .addImm(LPadList.size()));
6348 } else {
6349 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6350 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006351 .addImm(NumLPads & 0xFFFF));
6352
6353 unsigned VReg2 = VReg1;
6354 if ((NumLPads & 0xFFFF0000) != 0) {
6355 VReg2 = MRI->createVirtualRegister(TRC);
6356 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6357 .addReg(VReg1)
6358 .addImm(NumLPads >> 16));
6359 }
6360
Bill Wendling85833f72011-10-18 22:49:07 +00006361 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6362 .addReg(NewVReg1)
6363 .addReg(VReg2));
6364 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006365
Bill Wendling5626c662011-10-06 22:53:00 +00006366 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6367 .addMBB(TrapBB)
6368 .addImm(ARMCC::HI)
6369 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00006370
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006371 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6372 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006373 .addJumpTableIndex(MJTI)
6374 .addImm(UId));
Bill Wendling202803e2011-10-05 00:02:33 +00006375
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006376 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006377 AddDefaultCC(
6378 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006379 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6380 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00006381 .addReg(NewVReg1)
6382 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6383
6384 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006385 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00006386 .addReg(NewVReg1)
Bill Wendling5626c662011-10-06 22:53:00 +00006387 .addJumpTableIndex(MJTI)
6388 .addImm(UId);
6389 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00006390 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6391 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6392 .addFrameIndex(FI)
6393 .addImm(1)
6394 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00006395
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006396 if (NumLPads < 256) {
6397 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6398 .addReg(NewVReg1)
6399 .addImm(NumLPads));
6400 } else {
6401 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00006402 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6403 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6404
6405 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006406 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006407 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006408 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006409 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006410
6411 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6412 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6413 .addReg(VReg1, RegState::Define)
6414 .addConstantPoolIndex(Idx));
6415 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6416 .addReg(NewVReg1)
6417 .addReg(VReg1));
6418 }
6419
Bill Wendlingb3d46782011-10-06 23:37:36 +00006420 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6421 .addMBB(TrapBB)
6422 .addImm(ARMCC::HI)
6423 .addReg(ARM::CPSR);
6424
6425 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6426 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6427 .addReg(ARM::CPSR, RegState::Define)
6428 .addReg(NewVReg1)
6429 .addImm(2));
6430
6431 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00006432 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendlingb3d46782011-10-06 23:37:36 +00006433 .addJumpTableIndex(MJTI)
6434 .addImm(UId));
6435
6436 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6437 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6438 .addReg(ARM::CPSR, RegState::Define)
6439 .addReg(NewVReg2, RegState::Kill)
6440 .addReg(NewVReg3));
6441
6442 MachineMemOperand *JTMMOLd =
6443 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6444 MachineMemOperand::MOLoad, 4, 4);
6445
6446 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6447 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6448 .addReg(NewVReg4, RegState::Kill)
6449 .addImm(0)
6450 .addMemOperand(JTMMOLd));
6451
Chad Rosier96603432013-03-01 18:30:38 +00006452 unsigned NewVReg6 = NewVReg5;
6453 if (RelocM == Reloc::PIC_) {
6454 NewVReg6 = MRI->createVirtualRegister(TRC);
6455 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6456 .addReg(ARM::CPSR, RegState::Define)
6457 .addReg(NewVReg5, RegState::Kill)
6458 .addReg(NewVReg3));
6459 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00006460
6461 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6462 .addReg(NewVReg6, RegState::Kill)
6463 .addJumpTableIndex(MJTI)
6464 .addImm(UId);
Bill Wendling5626c662011-10-06 22:53:00 +00006465 } else {
6466 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6467 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6468 .addFrameIndex(FI)
6469 .addImm(4)
6470 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00006471
Bill Wendling4969dcd2011-10-18 22:52:20 +00006472 if (NumLPads < 256) {
6473 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6474 .addReg(NewVReg1)
6475 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00006476 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00006477 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6478 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006479 .addImm(NumLPads & 0xFFFF));
6480
6481 unsigned VReg2 = VReg1;
6482 if ((NumLPads & 0xFFFF0000) != 0) {
6483 VReg2 = MRI->createVirtualRegister(TRC);
6484 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6485 .addReg(VReg1)
6486 .addImm(NumLPads >> 16));
6487 }
6488
Bill Wendling4969dcd2011-10-18 22:52:20 +00006489 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6490 .addReg(NewVReg1)
6491 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00006492 } else {
6493 MachineConstantPool *ConstantPool = MF->getConstantPool();
6494 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6495 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6496
6497 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006498 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006499 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006500 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006501 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6502
6503 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6504 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6505 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00006506 .addConstantPoolIndex(Idx)
6507 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00006508 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6509 .addReg(NewVReg1)
6510 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00006511 }
6512
Bill Wendling5626c662011-10-06 22:53:00 +00006513 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6514 .addMBB(TrapBB)
6515 .addImm(ARMCC::HI)
6516 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00006517
Bill Wendling973c8172011-10-18 22:11:18 +00006518 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006519 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00006520 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006521 .addReg(NewVReg1)
6522 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00006523 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6524 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006525 .addJumpTableIndex(MJTI)
6526 .addImm(UId));
6527
6528 MachineMemOperand *JTMMOLd =
6529 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6530 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00006531 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006532 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00006533 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6534 .addReg(NewVReg3, RegState::Kill)
6535 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006536 .addImm(0)
6537 .addMemOperand(JTMMOLd));
6538
Chad Rosier96603432013-03-01 18:30:38 +00006539 if (RelocM == Reloc::PIC_) {
6540 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
6541 .addReg(NewVReg5, RegState::Kill)
6542 .addReg(NewVReg4)
6543 .addJumpTableIndex(MJTI)
6544 .addImm(UId);
6545 } else {
6546 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
6547 .addReg(NewVReg5, RegState::Kill)
6548 .addJumpTableIndex(MJTI)
6549 .addImm(UId);
6550 }
Bill Wendling5626c662011-10-06 22:53:00 +00006551 }
Bill Wendling202803e2011-10-05 00:02:33 +00006552
Bill Wendling324be982011-10-05 00:39:32 +00006553 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006554 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00006555 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006556 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6557 MachineBasicBlock *CurMBB = *I;
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006558 if (SeenMBBs.insert(CurMBB))
Bill Wendling883ec972011-10-07 23:18:02 +00006559 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006560 }
6561
Bill Wendling26d27802011-10-17 05:25:09 +00006562 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00006563 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00006564 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling883ec972011-10-07 23:18:02 +00006565 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6566 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6567 MachineBasicBlock *BB = *I;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006568
6569 // Remove the landing pad successor from the invoke block and replace it
6570 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00006571 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6572 BB->succ_end());
6573 while (!Successors.empty()) {
6574 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling883ec972011-10-07 23:18:02 +00006575 if (SMBB->isLandingPad()) {
6576 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00006577 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006578 }
6579 }
6580
6581 BB->addSuccessor(DispatchBB);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006582
6583 // Find the invoke call and mark all of the callee-saved registers as
6584 // 'implicit defined' so that they're spilled. This prevents code from
6585 // moving instructions to before the EH block, where they will never be
6586 // executed.
6587 for (MachineBasicBlock::reverse_iterator
6588 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00006589 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006590
6591 DenseMap<unsigned, bool> DefRegs;
6592 for (MachineInstr::mop_iterator
6593 OI = II->operands_begin(), OE = II->operands_end();
6594 OI != OE; ++OI) {
6595 if (!OI->isReg()) continue;
6596 DefRegs[OI->getReg()] = true;
6597 }
6598
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00006599 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006600
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006601 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00006602 unsigned Reg = SavedRegs[i];
6603 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00006604 !ARM::tGPRRegClass.contains(Reg) &&
6605 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006606 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006607 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006608 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006609 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006610 continue;
6611 if (!DefRegs[Reg])
6612 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006613 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006614
6615 break;
6616 }
Bill Wendling883ec972011-10-07 23:18:02 +00006617 }
Bill Wendling324be982011-10-05 00:39:32 +00006618
Bill Wendling617075f2011-10-18 18:30:49 +00006619 // Mark all former landing pads as non-landing pads. The dispatch is the only
6620 // landing pad now.
6621 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6622 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6623 (*I)->setIsLandingPad(false);
6624
Bill Wendling324be982011-10-05 00:39:32 +00006625 // The instruction is gone now.
6626 MI->eraseFromParent();
6627
Bill Wendling374ee192011-10-03 21:25:38 +00006628 return MBB;
6629}
6630
Evan Cheng0cc4ad92010-07-13 19:27:42 +00006631static
6632MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6633 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6634 E = MBB->succ_end(); I != E; ++I)
6635 if (*I != Succ)
6636 return *I;
6637 llvm_unreachable("Expecting a BB with two successors!");
6638}
6639
Manman Renb504f492013-10-29 22:27:32 +00006640/// Return the load opcode for a given load size. If load size >= 8,
6641/// neon opcode will be returned.
6642static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
6643 if (LdSize >= 8)
6644 return LdSize == 16 ? ARM::VLD1q32wb_fixed
6645 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
6646 if (IsThumb1)
6647 return LdSize == 4 ? ARM::tLDRi
6648 : LdSize == 2 ? ARM::tLDRHi
6649 : LdSize == 1 ? ARM::tLDRBi : 0;
6650 if (IsThumb2)
6651 return LdSize == 4 ? ARM::t2LDR_POST
6652 : LdSize == 2 ? ARM::t2LDRH_POST
6653 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
6654 return LdSize == 4 ? ARM::LDR_POST_IMM
6655 : LdSize == 2 ? ARM::LDRH_POST
6656 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
6657}
6658
6659/// Return the store opcode for a given store size. If store size >= 8,
6660/// neon opcode will be returned.
6661static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
6662 if (StSize >= 8)
6663 return StSize == 16 ? ARM::VST1q32wb_fixed
6664 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
6665 if (IsThumb1)
6666 return StSize == 4 ? ARM::tSTRi
6667 : StSize == 2 ? ARM::tSTRHi
6668 : StSize == 1 ? ARM::tSTRBi : 0;
6669 if (IsThumb2)
6670 return StSize == 4 ? ARM::t2STR_POST
6671 : StSize == 2 ? ARM::t2STRH_POST
6672 : StSize == 1 ? ARM::t2STRB_POST : 0;
6673 return StSize == 4 ? ARM::STR_POST_IMM
6674 : StSize == 2 ? ARM::STRH_POST
6675 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
6676}
6677
6678/// Emit a post-increment load operation with given size. The instructions
6679/// will be added to BB at Pos.
6680static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
6681 const TargetInstrInfo *TII, DebugLoc dl,
6682 unsigned LdSize, unsigned Data, unsigned AddrIn,
6683 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
6684 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
6685 assert(LdOpc != 0 && "Should have a load opcode");
6686 if (LdSize >= 8) {
6687 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6688 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6689 .addImm(0));
6690 } else if (IsThumb1) {
6691 // load + update AddrIn
6692 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6693 .addReg(AddrIn).addImm(0));
6694 MachineInstrBuilder MIB =
6695 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
6696 MIB = AddDefaultT1CC(MIB);
6697 MIB.addReg(AddrIn).addImm(LdSize);
6698 AddDefaultPred(MIB);
6699 } else if (IsThumb2) {
6700 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6701 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6702 .addImm(LdSize));
6703 } else { // arm
6704 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6705 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6706 .addReg(0).addImm(LdSize));
6707 }
6708}
6709
6710/// Emit a post-increment store operation with given size. The instructions
6711/// will be added to BB at Pos.
6712static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
6713 const TargetInstrInfo *TII, DebugLoc dl,
6714 unsigned StSize, unsigned Data, unsigned AddrIn,
6715 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
6716 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
6717 assert(StOpc != 0 && "Should have a store opcode");
6718 if (StSize >= 8) {
6719 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6720 .addReg(AddrIn).addImm(0).addReg(Data));
6721 } else if (IsThumb1) {
6722 // store + update AddrIn
6723 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
6724 .addReg(AddrIn).addImm(0));
6725 MachineInstrBuilder MIB =
6726 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
6727 MIB = AddDefaultT1CC(MIB);
6728 MIB.addReg(AddrIn).addImm(StSize);
6729 AddDefaultPred(MIB);
6730 } else if (IsThumb2) {
6731 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6732 .addReg(Data).addReg(AddrIn).addImm(StSize));
6733 } else { // arm
6734 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6735 .addReg(Data).addReg(AddrIn).addReg(0)
6736 .addImm(StSize));
6737 }
6738}
6739
David Peixottoc32e24a2013-10-17 19:49:22 +00006740MachineBasicBlock *
6741ARMTargetLowering::EmitStructByval(MachineInstr *MI,
6742 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00006743 // This pseudo instruction has 3 operands: dst, src, size
6744 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6745 // Otherwise, we will generate unrolled scalar copies.
6746 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6747 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6748 MachineFunction::iterator It = BB;
6749 ++It;
6750
6751 unsigned dest = MI->getOperand(0).getReg();
6752 unsigned src = MI->getOperand(1).getReg();
6753 unsigned SizeVal = MI->getOperand(2).getImm();
6754 unsigned Align = MI->getOperand(3).getImm();
6755 DebugLoc dl = MI->getDebugLoc();
6756
Manman Rene8735522012-06-01 19:33:18 +00006757 MachineFunction *MF = BB->getParent();
6758 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00006759 unsigned UnitSize = 0;
David Peixottob0653e532013-10-24 16:39:36 +00006760 const TargetRegisterClass *TRC = 0;
6761 const TargetRegisterClass *VecTRC = 0;
6762
6763 bool IsThumb1 = Subtarget->isThumb1Only();
6764 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00006765
6766 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00006767 UnitSize = 1;
6768 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00006769 UnitSize = 2;
6770 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00006771 // Check whether we can use NEON instructions.
Bill Wendling698e84f2012-12-30 10:32:01 +00006772 if (!MF->getFunction()->getAttributes().
6773 hasAttribute(AttributeSet::FunctionIndex,
6774 Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00006775 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00006776 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00006777 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00006778 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00006779 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00006780 }
6781 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00006782 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00006783 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00006784 }
Manman Ren6e1fd462012-06-18 22:23:48 +00006785
David Peixottob0653e532013-10-24 16:39:36 +00006786 // Select the correct opcode and register class for unit size load/store
6787 bool IsNeon = UnitSize >= 8;
6788 TRC = (IsThumb1 || IsThumb2) ? (const TargetRegisterClass *)&ARM::tGPRRegClass
6789 : (const TargetRegisterClass *)&ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00006790 if (IsNeon)
David Peixottob0653e532013-10-24 16:39:36 +00006791 VecTRC = UnitSize == 16
6792 ? (const TargetRegisterClass *)&ARM::DPairRegClass
6793 : UnitSize == 8
6794 ? (const TargetRegisterClass *)&ARM::DPRRegClass
6795 : 0;
David Peixottob0653e532013-10-24 16:39:36 +00006796
Manman Rene8735522012-06-01 19:33:18 +00006797 unsigned BytesLeft = SizeVal % UnitSize;
6798 unsigned LoopSize = SizeVal - BytesLeft;
6799
6800 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6801 // Use LDR and STR to copy.
6802 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6803 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6804 unsigned srcIn = src;
6805 unsigned destIn = dest;
6806 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00006807 unsigned srcOut = MRI.createVirtualRegister(TRC);
6808 unsigned destOut = MRI.createVirtualRegister(TRC);
6809 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00006810 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
6811 IsThumb1, IsThumb2);
6812 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
6813 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00006814 srcIn = srcOut;
6815 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00006816 }
6817
6818 // Handle the leftover bytes with LDRB and STRB.
6819 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6820 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00006821 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00006822 unsigned srcOut = MRI.createVirtualRegister(TRC);
6823 unsigned destOut = MRI.createVirtualRegister(TRC);
6824 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00006825 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
6826 IsThumb1, IsThumb2);
6827 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
6828 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00006829 srcIn = srcOut;
6830 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00006831 }
6832 MI->eraseFromParent(); // The instruction is gone now.
6833 return BB;
6834 }
6835
6836 // Expand the pseudo op to a loop.
6837 // thisMBB:
6838 // ...
6839 // movw varEnd, # --> with thumb2
6840 // movt varEnd, #
6841 // ldrcp varEnd, idx --> without thumb2
6842 // fallthrough --> loopMBB
6843 // loopMBB:
6844 // PHI varPhi, varEnd, varLoop
6845 // PHI srcPhi, src, srcLoop
6846 // PHI destPhi, dst, destLoop
6847 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6848 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
6849 // subs varLoop, varPhi, #UnitSize
6850 // bne loopMBB
6851 // fallthrough --> exitMBB
6852 // exitMBB:
6853 // epilogue to handle left-over bytes
6854 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6855 // [destOut] = STRB_POST(scratch, destLoop, 1)
6856 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6857 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6858 MF->insert(It, loopMBB);
6859 MF->insert(It, exitMBB);
6860
6861 // Transfer the remainder of BB and its successor edges to exitMBB.
6862 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00006863 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00006864 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6865
6866 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00006867 unsigned varEnd = MRI.createVirtualRegister(TRC);
6868 if (IsThumb2) {
6869 unsigned Vtmp = varEnd;
6870 if ((LoopSize & 0xFFFF0000) != 0)
6871 Vtmp = MRI.createVirtualRegister(TRC);
6872 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), Vtmp)
6873 .addImm(LoopSize & 0xFFFF));
6874
6875 if ((LoopSize & 0xFFFF0000) != 0)
6876 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
6877 .addReg(Vtmp).addImm(LoopSize >> 16));
6878 } else {
6879 MachineConstantPool *ConstantPool = MF->getConstantPool();
6880 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6881 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
6882
6883 // MachineConstantPool wants an explicit alignment.
6884 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
6885 if (Align == 0)
6886 Align = getDataLayout()->getTypeAllocSize(C->getType());
6887 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6888
6889 if (IsThumb1)
6890 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
6891 varEnd, RegState::Define).addConstantPoolIndex(Idx));
6892 else
6893 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
6894 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
6895 }
Manman Rene8735522012-06-01 19:33:18 +00006896 BB->addSuccessor(loopMBB);
6897
6898 // Generate the loop body:
6899 // varPhi = PHI(varLoop, varEnd)
6900 // srcPhi = PHI(srcLoop, src)
6901 // destPhi = PHI(destLoop, dst)
6902 MachineBasicBlock *entryBB = BB;
6903 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00006904 unsigned varLoop = MRI.createVirtualRegister(TRC);
6905 unsigned varPhi = MRI.createVirtualRegister(TRC);
6906 unsigned srcLoop = MRI.createVirtualRegister(TRC);
6907 unsigned srcPhi = MRI.createVirtualRegister(TRC);
6908 unsigned destLoop = MRI.createVirtualRegister(TRC);
6909 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00006910
6911 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
6912 .addReg(varLoop).addMBB(loopMBB)
6913 .addReg(varEnd).addMBB(entryBB);
6914 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
6915 .addReg(srcLoop).addMBB(loopMBB)
6916 .addReg(src).addMBB(entryBB);
6917 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
6918 .addReg(destLoop).addMBB(loopMBB)
6919 .addReg(dest).addMBB(entryBB);
6920
6921 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6922 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00006923 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00006924 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
6925 IsThumb1, IsThumb2);
6926 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
6927 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00006928
6929 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00006930 if (IsThumb1) {
6931 MachineInstrBuilder MIB =
6932 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
6933 MIB = AddDefaultT1CC(MIB);
6934 MIB.addReg(varPhi).addImm(UnitSize);
6935 AddDefaultPred(MIB);
6936 } else {
6937 MachineInstrBuilder MIB =
6938 BuildMI(*BB, BB->end(), dl,
6939 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
6940 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
6941 MIB->getOperand(5).setReg(ARM::CPSR);
6942 MIB->getOperand(5).setIsDef(true);
6943 }
6944 BuildMI(*BB, BB->end(), dl,
6945 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
6946 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00006947
6948 // loopMBB can loop back to loopMBB or fall through to exitMBB.
6949 BB->addSuccessor(loopMBB);
6950 BB->addSuccessor(exitMBB);
6951
6952 // Add epilogue to handle BytesLeft.
6953 BB = exitMBB;
6954 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00006955
6956 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6957 // [destOut] = STRB_POST(scratch, destLoop, 1)
6958 unsigned srcIn = srcLoop;
6959 unsigned destIn = destLoop;
6960 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00006961 unsigned srcOut = MRI.createVirtualRegister(TRC);
6962 unsigned destOut = MRI.createVirtualRegister(TRC);
6963 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00006964 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
6965 IsThumb1, IsThumb2);
6966 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
6967 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00006968 srcIn = srcOut;
6969 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00006970 }
6971
6972 MI->eraseFromParent(); // The instruction is gone now.
6973 return BB;
6974}
6975
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00006976MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00006977ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00006978 MachineBasicBlock *BB) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006979 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00006980 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00006981 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00006982 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00006983 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00006984 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00006985 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00006986 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00006987 // The Thumb2 pre-indexed stores have the same MI operands, they just
6988 // define them differently in the .td files from the isel patterns, so
6989 // they need pseudos.
6990 case ARM::t2STR_preidx:
6991 MI->setDesc(TII->get(ARM::t2STR_PRE));
6992 return BB;
6993 case ARM::t2STRB_preidx:
6994 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6995 return BB;
6996 case ARM::t2STRH_preidx:
6997 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6998 return BB;
6999
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007000 case ARM::STRi_preidx:
7001 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00007002 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007003 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7004 // Decode the offset.
7005 unsigned Offset = MI->getOperand(4).getImm();
7006 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7007 Offset = ARM_AM::getAM2Offset(Offset);
7008 if (isSub)
7009 Offset = -Offset;
7010
Jim Grosbachf402f692011-08-12 21:02:34 +00007011 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00007012 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007013 .addOperand(MI->getOperand(0)) // Rn_wb
7014 .addOperand(MI->getOperand(1)) // Rt
7015 .addOperand(MI->getOperand(2)) // Rn
7016 .addImm(Offset) // offset (skip GPR==zero_reg)
7017 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00007018 .addOperand(MI->getOperand(6))
7019 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007020 MI->eraseFromParent();
7021 return BB;
7022 }
7023 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00007024 case ARM::STRBr_preidx:
7025 case ARM::STRH_preidx: {
7026 unsigned NewOpc;
7027 switch (MI->getOpcode()) {
7028 default: llvm_unreachable("unexpected opcode!");
7029 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7030 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7031 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7032 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007033 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7034 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7035 MIB.addOperand(MI->getOperand(i));
7036 MI->eraseFromParent();
7037 return BB;
7038 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00007039
Evan Chengbb2af352009-08-12 05:17:19 +00007040 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00007041 // To "insert" a SELECT_CC instruction, we actually have to insert the
7042 // diamond control-flow pattern. The incoming instruction knows the
7043 // destination vreg to set, the condition code register to branch on, the
7044 // true/false values to select between, and a branch opcode to use.
7045 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00007046 MachineFunction::iterator It = BB;
Evan Cheng10043e22007-01-19 07:51:42 +00007047 ++It;
7048
7049 // thisMBB:
7050 // ...
7051 // TrueVal = ...
7052 // cmpTY ccX, r1, r2
7053 // bCC copy1MBB
7054 // fallthrough --> copy0MBB
7055 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00007056 MachineFunction *F = BB->getParent();
7057 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7058 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00007059 F->insert(It, copy0MBB);
7060 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007061
7062 // Transfer the remainder of BB and its successor edges to sinkMBB.
7063 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007064 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007065 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7066
Dan Gohmanf4f04102010-07-06 15:49:48 +00007067 BB->addSuccessor(copy0MBB);
7068 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00007069
Dan Gohman34396292010-07-06 20:24:04 +00007070 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7071 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7072
Evan Cheng10043e22007-01-19 07:51:42 +00007073 // copy0MBB:
7074 // %FalseValue = ...
7075 // # fallthrough to sinkMBB
7076 BB = copy0MBB;
7077
7078 // Update machine-CFG edges
7079 BB->addSuccessor(sinkMBB);
7080
7081 // sinkMBB:
7082 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7083 // ...
7084 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007085 BuildMI(*BB, BB->begin(), dl,
7086 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00007087 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7088 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7089
Dan Gohman34396292010-07-06 20:24:04 +00007090 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00007091 return BB;
7092 }
Evan Chengb972e562009-08-07 00:34:42 +00007093
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007094 case ARM::BCCi64:
7095 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00007096 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007097 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00007098
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007099 // Compare both parts that make up the double comparison separately for
7100 // equality.
7101 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7102
7103 unsigned LHS1 = MI->getOperand(1).getReg();
7104 unsigned LHS2 = MI->getOperand(2).getReg();
7105 if (RHSisZero) {
7106 AddDefaultPred(BuildMI(BB, dl,
7107 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7108 .addReg(LHS1).addImm(0));
7109 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7110 .addReg(LHS2).addImm(0)
7111 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7112 } else {
7113 unsigned RHS1 = MI->getOperand(3).getReg();
7114 unsigned RHS2 = MI->getOperand(4).getReg();
7115 AddDefaultPred(BuildMI(BB, dl,
7116 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7117 .addReg(LHS1).addReg(RHS1));
7118 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7119 .addReg(LHS2).addReg(RHS2)
7120 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7121 }
7122
7123 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7124 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7125 if (MI->getOperand(0).getImm() == ARMCC::NE)
7126 std::swap(destMBB, exitMBB);
7127
7128 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7129 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00007130 if (isThumb2)
7131 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7132 else
7133 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007134
7135 MI->eraseFromParent(); // The pseudo instruction is gone now.
7136 return BB;
7137 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007138
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007139 case ARM::Int_eh_sjlj_setjmp:
7140 case ARM::Int_eh_sjlj_setjmp_nofp:
7141 case ARM::tInt_eh_sjlj_setjmp:
7142 case ARM::t2Int_eh_sjlj_setjmp:
7143 case ARM::t2Int_eh_sjlj_setjmp_nofp:
7144 EmitSjLjDispatchBlock(MI, BB);
7145 return BB;
7146
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007147 case ARM::ABS:
7148 case ARM::t2ABS: {
7149 // To insert an ABS instruction, we have to insert the
7150 // diamond control-flow pattern. The incoming instruction knows the
7151 // source vreg to test against 0, the destination vreg to set,
7152 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00007153 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007154 // It transforms
7155 // V1 = ABS V0
7156 // into
7157 // V2 = MOVS V0
7158 // BCC (branch to SinkBB if V0 >= 0)
7159 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00007160 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007161 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7162 MachineFunction::iterator BBI = BB;
7163 ++BBI;
7164 MachineFunction *Fn = BB->getParent();
7165 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7166 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7167 Fn->insert(BBI, RSBBB);
7168 Fn->insert(BBI, SinkBB);
7169
7170 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7171 unsigned int ABSDstReg = MI->getOperand(0).getReg();
7172 bool isThumb2 = Subtarget->isThumb2();
7173 MachineRegisterInfo &MRI = Fn->getRegInfo();
7174 // In Thumb mode S must not be specified if source register is the SP or
7175 // PC and if destination register is the SP, so restrict register class
Craig Topperc7242e02012-04-20 07:30:17 +00007176 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
7177 (const TargetRegisterClass*)&ARM::rGPRRegClass :
7178 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007179
7180 // Transfer the remainder of BB and its successor edges to sinkMBB.
7181 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007182 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007183 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7184
7185 BB->addSuccessor(RSBBB);
7186 BB->addSuccessor(SinkBB);
7187
7188 // fall through to SinkMBB
7189 RSBBB->addSuccessor(SinkBB);
7190
Manman Rene0763c72012-06-15 21:32:12 +00007191 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00007192 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00007193 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7194 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007195
7196 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00007197 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007198 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7199 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7200
7201 // insert rsbri in RSBBB
7202 // Note: BCC and rsbri will be converted into predicated rsbmi
7203 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00007204 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007205 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Rene0763c72012-06-15 21:32:12 +00007206 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007207 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7208
Andrew Trick3f07c422011-10-18 18:40:53 +00007209 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007210 // reuse ABSDstReg to not change uses of ABS instruction
7211 BuildMI(*SinkBB, SinkBB->begin(), dl,
7212 TII->get(ARM::PHI), ABSDstReg)
7213 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00007214 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007215
7216 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00007217 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007218
7219 // return last added BB
7220 return SinkBB;
7221 }
Manman Rene8735522012-06-01 19:33:18 +00007222 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00007223 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00007224 return EmitStructByval(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00007225 }
7226}
7227
Evan Chenge6fba772011-08-30 19:09:48 +00007228void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7229 SDNode *Node) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007230 if (!MI->hasPostISelHook()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007231 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
7232 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
7233 return;
7234 }
7235
Evan Cheng7f8e5632011-12-07 07:15:52 +00007236 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00007237 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7238 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7239 // operand is still set to noreg. If needed, set the optional operand's
7240 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00007241 //
Andrew Trick88b24502011-10-18 19:18:52 +00007242 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00007243
Andrew Trick924123a2011-09-21 02:20:46 +00007244 // Rename pseudo opcodes.
7245 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7246 if (NewOpc) {
7247 const ARMBaseInstrInfo *TII =
7248 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick88b24502011-10-18 19:18:52 +00007249 MCID = &TII->get(NewOpc);
7250
7251 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7252 "converted opcode should be the same except for cc_out");
7253
7254 MI->setDesc(*MCID);
7255
7256 // Add the optional cc_out operand
7257 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00007258 }
Andrew Trick88b24502011-10-18 19:18:52 +00007259 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00007260
7261 // Any ARM instruction that sets the 's' bit should specify an optional
7262 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00007263 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007264 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007265 return;
7266 }
Andrew Trick924123a2011-09-21 02:20:46 +00007267 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7268 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007269 bool definesCPSR = false;
7270 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00007271 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00007272 i != e; ++i) {
7273 const MachineOperand &MO = MI->getOperand(i);
7274 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7275 definesCPSR = true;
7276 if (MO.isDead())
7277 deadCPSR = true;
7278 MI->RemoveOperand(i);
7279 break;
Evan Chenge6fba772011-08-30 19:09:48 +00007280 }
7281 }
Andrew Trick8586e622011-09-20 03:17:40 +00007282 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00007283 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007284 return;
7285 }
7286 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00007287 if (deadCPSR) {
7288 assert(!MI->getOperand(ccOutIdx).getReg() &&
7289 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00007290 return;
Andrew Trick924123a2011-09-21 02:20:46 +00007291 }
Andrew Trick8586e622011-09-20 03:17:40 +00007292
Andrew Trick924123a2011-09-21 02:20:46 +00007293 // If this instruction was defined with an optional CPSR def and its dag node
7294 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007295 MachineOperand &MO = MI->getOperand(ccOutIdx);
7296 MO.setReg(ARM::CPSR);
7297 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00007298}
7299
Evan Cheng10043e22007-01-19 07:51:42 +00007300//===----------------------------------------------------------------------===//
7301// ARM Optimization Hooks
7302//===----------------------------------------------------------------------===//
7303
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007304// Helper function that checks if N is a null or all ones constant.
7305static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7306 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7307 if (!C)
7308 return false;
7309 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7310}
7311
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007312// Return true if N is conditionally 0 or all ones.
7313// Detects these expressions where cc is an i1 value:
7314//
7315// (select cc 0, y) [AllOnes=0]
7316// (select cc y, 0) [AllOnes=0]
7317// (zext cc) [AllOnes=0]
7318// (sext cc) [AllOnes=0/1]
7319// (select cc -1, y) [AllOnes=1]
7320// (select cc y, -1) [AllOnes=1]
7321//
7322// Invert is set when N is the null/all ones constant when CC is false.
7323// OtherOp is set to the alternative value of N.
7324static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7325 SDValue &CC, bool &Invert,
7326 SDValue &OtherOp,
7327 SelectionDAG &DAG) {
7328 switch (N->getOpcode()) {
7329 default: return false;
7330 case ISD::SELECT: {
7331 CC = N->getOperand(0);
7332 SDValue N1 = N->getOperand(1);
7333 SDValue N2 = N->getOperand(2);
7334 if (isZeroOrAllOnes(N1, AllOnes)) {
7335 Invert = false;
7336 OtherOp = N2;
7337 return true;
7338 }
7339 if (isZeroOrAllOnes(N2, AllOnes)) {
7340 Invert = true;
7341 OtherOp = N1;
7342 return true;
7343 }
7344 return false;
7345 }
7346 case ISD::ZERO_EXTEND:
7347 // (zext cc) can never be the all ones value.
7348 if (AllOnes)
7349 return false;
7350 // Fall through.
7351 case ISD::SIGN_EXTEND: {
7352 EVT VT = N->getValueType(0);
7353 CC = N->getOperand(0);
7354 if (CC.getValueType() != MVT::i1)
7355 return false;
7356 Invert = !AllOnes;
7357 if (AllOnes)
7358 // When looking for an AllOnes constant, N is an sext, and the 'other'
7359 // value is 0.
7360 OtherOp = DAG.getConstant(0, VT);
7361 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7362 // When looking for a 0 constant, N can be zext or sext.
7363 OtherOp = DAG.getConstant(1, VT);
7364 else
7365 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7366 return true;
7367 }
7368 }
7369}
7370
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007371// Combine a constant select operand into its use:
7372//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007373// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7374// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7375// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7376// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7377// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007378//
7379// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007380// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007381//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007382// Also recognize sext/zext from i1:
7383//
7384// (add (zext cc), x) -> (select cc (add x, 1), x)
7385// (add (sext cc), x) -> (select cc (add x, -1), x)
7386//
7387// These transformations eventually create predicated instructions.
7388//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007389// @param N The node to transform.
7390// @param Slct The N operand that is a select.
7391// @param OtherOp The other N operand (x above).
7392// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007393// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007394// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00007395static
7396SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007397 TargetLowering::DAGCombinerInfo &DCI,
7398 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00007399 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00007400 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007401 SDValue NonConstantVal;
7402 SDValue CCOp;
7403 bool SwapSelectOps;
7404 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7405 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007406 return SDValue();
7407
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007408 // Slct is now know to be the desired identity constant when CC is true.
7409 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007410 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007411 OtherOp, NonConstantVal);
7412 // Unless SwapSelectOps says CC should be false.
7413 if (SwapSelectOps)
7414 std::swap(TrueVal, FalseVal);
7415
Andrew Trickef9de2a2013-05-25 02:42:55 +00007416 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007417 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00007418}
7419
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007420// Attempt combineSelectAndUse on each operand of a commutative operator N.
7421static
7422SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7423 TargetLowering::DAGCombinerInfo &DCI) {
7424 SDValue N0 = N->getOperand(0);
7425 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007426 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007427 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7428 if (Result.getNode())
7429 return Result;
7430 }
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007431 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007432 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7433 if (Result.getNode())
7434 return Result;
7435 }
7436 return SDValue();
7437}
7438
Eric Christopher1b8b94192011-06-29 21:10:36 +00007439// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00007440// (only after legalization).
7441static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7442 TargetLowering::DAGCombinerInfo &DCI,
7443 const ARMSubtarget *Subtarget) {
7444
7445 // Only perform optimization if after legalize, and if NEON is available. We
7446 // also expected both operands to be BUILD_VECTORs.
7447 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7448 || N0.getOpcode() != ISD::BUILD_VECTOR
7449 || N1.getOpcode() != ISD::BUILD_VECTOR)
7450 return SDValue();
7451
7452 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7453 EVT VT = N->getValueType(0);
7454 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7455 return SDValue();
7456
7457 // Check that the vector operands are of the right form.
7458 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7459 // operands, where N is the size of the formed vector.
7460 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7461 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007462
7463 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00007464 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00007465 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00007466 SDValue Vec = N0->getOperand(0)->getOperand(0);
7467 SDNode *V = Vec.getNode();
7468 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00007469
Eric Christopher1b8b94192011-06-29 21:10:36 +00007470 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007471 // check to see if each of their operands are an EXTRACT_VECTOR with
7472 // the same vector and appropriate index.
7473 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7474 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7475 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00007476
Tanya Lattnere9e67052011-06-14 23:48:48 +00007477 SDValue ExtVec0 = N0->getOperand(i);
7478 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007479
Tanya Lattnere9e67052011-06-14 23:48:48 +00007480 // First operand is the vector, verify its the same.
7481 if (V != ExtVec0->getOperand(0).getNode() ||
7482 V != ExtVec1->getOperand(0).getNode())
7483 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00007484
Tanya Lattnere9e67052011-06-14 23:48:48 +00007485 // Second is the constant, verify its correct.
7486 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7487 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00007488
Tanya Lattnere9e67052011-06-14 23:48:48 +00007489 // For the constant, we want to see all the even or all the odd.
7490 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7491 || C1->getZExtValue() != nextIndex+1)
7492 return SDValue();
7493
7494 // Increment index.
7495 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00007496 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00007497 return SDValue();
7498 }
7499
7500 // Create VPADDL node.
7501 SelectionDAG &DAG = DCI.DAG;
7502 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00007503
7504 // Build operand list.
7505 SmallVector<SDValue, 8> Ops;
7506 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7507 TLI.getPointerTy()));
7508
7509 // Input is the vector.
7510 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007511
Tanya Lattnere9e67052011-06-14 23:48:48 +00007512 // Get widened type and narrowed type.
7513 MVT widenType;
7514 unsigned numElem = VT.getVectorNumElements();
Silviu Barangaa3106e62014-04-03 10:44:27 +00007515
7516 EVT inputLaneType = Vec.getValueType().getVectorElementType();
7517 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00007518 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7519 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7520 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7521 default:
Craig Toppere55c5562012-02-07 02:50:20 +00007522 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00007523 }
7524
Andrew Trickef9de2a2013-05-25 02:42:55 +00007525 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
Tanya Lattnere9e67052011-06-14 23:48:48 +00007526 widenType, &Ops[0], Ops.size());
Silviu Barangaa3106e62014-04-03 10:44:27 +00007527 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
7528 return DAG.getNode(ExtOp, SDLoc(N), VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00007529}
7530
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007531static SDValue findMUL_LOHI(SDValue V) {
7532 if (V->getOpcode() == ISD::UMUL_LOHI ||
7533 V->getOpcode() == ISD::SMUL_LOHI)
7534 return V;
7535 return SDValue();
7536}
7537
7538static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7539 TargetLowering::DAGCombinerInfo &DCI,
7540 const ARMSubtarget *Subtarget) {
7541
7542 if (Subtarget->isThumb1Only()) return SDValue();
7543
7544 // Only perform the checks after legalize when the pattern is available.
7545 if (DCI.isBeforeLegalize()) return SDValue();
7546
7547 // Look for multiply add opportunities.
7548 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7549 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7550 // a glue link from the first add to the second add.
7551 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
7552 // a S/UMLAL instruction.
7553 // loAdd UMUL_LOHI
7554 // \ / :lo \ :hi
7555 // \ / \ [no multiline comment]
7556 // ADDC | hiAdd
7557 // \ :glue / /
7558 // \ / /
7559 // ADDE
7560 //
7561 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
7562 SDValue AddcOp0 = AddcNode->getOperand(0);
7563 SDValue AddcOp1 = AddcNode->getOperand(1);
7564
7565 // Check if the two operands are from the same mul_lohi node.
7566 if (AddcOp0.getNode() == AddcOp1.getNode())
7567 return SDValue();
7568
7569 assert(AddcNode->getNumValues() == 2 &&
7570 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00007571 "Expect ADDC with two result values. First: i32");
7572
7573 // Check that we have a glued ADDC node.
7574 if (AddcNode->getValueType(1) != MVT::Glue)
7575 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007576
7577 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
7578 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
7579 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
7580 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
7581 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
7582 return SDValue();
7583
7584 // Look for the glued ADDE.
7585 SDNode* AddeNode = AddcNode->getGluedUser();
7586 if (AddeNode == NULL)
7587 return SDValue();
7588
7589 // Make sure it is really an ADDE.
7590 if (AddeNode->getOpcode() != ISD::ADDE)
7591 return SDValue();
7592
7593 assert(AddeNode->getNumOperands() == 3 &&
7594 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
7595 "ADDE node has the wrong inputs");
7596
7597 // Check for the triangle shape.
7598 SDValue AddeOp0 = AddeNode->getOperand(0);
7599 SDValue AddeOp1 = AddeNode->getOperand(1);
7600
7601 // Make sure that the ADDE operands are not coming from the same node.
7602 if (AddeOp0.getNode() == AddeOp1.getNode())
7603 return SDValue();
7604
7605 // Find the MUL_LOHI node walking up ADDE's operands.
7606 bool IsLeftOperandMUL = false;
7607 SDValue MULOp = findMUL_LOHI(AddeOp0);
7608 if (MULOp == SDValue())
7609 MULOp = findMUL_LOHI(AddeOp1);
7610 else
7611 IsLeftOperandMUL = true;
7612 if (MULOp == SDValue())
7613 return SDValue();
7614
7615 // Figure out the right opcode.
7616 unsigned Opc = MULOp->getOpcode();
7617 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
7618
7619 // Figure out the high and low input values to the MLAL node.
7620 SDValue* HiMul = &MULOp;
7621 SDValue* HiAdd = NULL;
7622 SDValue* LoMul = NULL;
7623 SDValue* LowAdd = NULL;
7624
7625 if (IsLeftOperandMUL)
7626 HiAdd = &AddeOp1;
7627 else
7628 HiAdd = &AddeOp0;
7629
7630
7631 if (AddcOp0->getOpcode() == Opc) {
7632 LoMul = &AddcOp0;
7633 LowAdd = &AddcOp1;
7634 }
7635 if (AddcOp1->getOpcode() == Opc) {
7636 LoMul = &AddcOp1;
7637 LowAdd = &AddcOp0;
7638 }
7639
7640 if (LoMul == NULL)
7641 return SDValue();
7642
7643 if (LoMul->getNode() != HiMul->getNode())
7644 return SDValue();
7645
7646 // Create the merged node.
7647 SelectionDAG &DAG = DCI.DAG;
7648
7649 // Build operand list.
7650 SmallVector<SDValue, 8> Ops;
7651 Ops.push_back(LoMul->getOperand(0));
7652 Ops.push_back(LoMul->getOperand(1));
7653 Ops.push_back(*LowAdd);
7654 Ops.push_back(*HiAdd);
7655
Andrew Trickef9de2a2013-05-25 02:42:55 +00007656 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007657 DAG.getVTList(MVT::i32, MVT::i32),
7658 &Ops[0], Ops.size());
7659
7660 // Replace the ADDs' nodes uses by the MLA node's values.
7661 SDValue HiMLALResult(MLALNode.getNode(), 1);
7662 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
7663
7664 SDValue LoMLALResult(MLALNode.getNode(), 0);
7665 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
7666
7667 // Return original node to notify the driver to stop replacing.
7668 SDValue resNode(AddcNode, 0);
7669 return resNode;
7670}
7671
7672/// PerformADDCCombine - Target-specific dag combine transform from
7673/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
7674static SDValue PerformADDCCombine(SDNode *N,
7675 TargetLowering::DAGCombinerInfo &DCI,
7676 const ARMSubtarget *Subtarget) {
7677
7678 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
7679
7680}
7681
Bob Wilson728eb292010-07-29 20:34:14 +00007682/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7683/// operands N0 and N1. This is a helper for PerformADDCombine that is
7684/// called with the default operands, and if that fails, with commuted
7685/// operands.
7686static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007687 TargetLowering::DAGCombinerInfo &DCI,
7688 const ARMSubtarget *Subtarget){
7689
7690 // Attempt to create vpaddl for this add.
7691 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7692 if (Result.getNode())
7693 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00007694
Chris Lattner4147f082009-03-12 06:52:53 +00007695 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007696 if (N0.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00007697 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7698 if (Result.getNode()) return Result;
7699 }
Chris Lattner4147f082009-03-12 06:52:53 +00007700 return SDValue();
7701}
7702
Bob Wilson728eb292010-07-29 20:34:14 +00007703/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7704///
7705static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007706 TargetLowering::DAGCombinerInfo &DCI,
7707 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00007708 SDValue N0 = N->getOperand(0);
7709 SDValue N1 = N->getOperand(1);
7710
7711 // First try with the default operand order.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007712 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00007713 if (Result.getNode())
7714 return Result;
7715
7716 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007717 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00007718}
7719
Chris Lattner4147f082009-03-12 06:52:53 +00007720/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00007721///
Chris Lattner4147f082009-03-12 06:52:53 +00007722static SDValue PerformSUBCombine(SDNode *N,
7723 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00007724 SDValue N0 = N->getOperand(0);
7725 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00007726
Chris Lattner4147f082009-03-12 06:52:53 +00007727 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007728 if (N1.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00007729 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7730 if (Result.getNode()) return Result;
7731 }
Bob Wilson7117a912009-03-20 22:42:55 +00007732
Chris Lattner4147f082009-03-12 06:52:53 +00007733 return SDValue();
7734}
7735
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007736/// PerformVMULCombine
7737/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7738/// special multiplier accumulator forwarding.
7739/// vmul d3, d0, d2
7740/// vmla d3, d1, d2
7741/// is faster than
7742/// vadd d3, d0, d1
7743/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00007744// However, for (A + B) * (A + B),
7745// vadd d2, d0, d1
7746// vmul d3, d0, d2
7747// vmla d3, d1, d2
7748// is slower than
7749// vadd d2, d0, d1
7750// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007751static SDValue PerformVMULCombine(SDNode *N,
7752 TargetLowering::DAGCombinerInfo &DCI,
7753 const ARMSubtarget *Subtarget) {
7754 if (!Subtarget->hasVMLxForwarding())
7755 return SDValue();
7756
7757 SelectionDAG &DAG = DCI.DAG;
7758 SDValue N0 = N->getOperand(0);
7759 SDValue N1 = N->getOperand(1);
7760 unsigned Opcode = N0.getOpcode();
7761 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7762 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00007763 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007764 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7765 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7766 return SDValue();
7767 std::swap(N0, N1);
7768 }
7769
Weiming Zhao2052f482013-09-25 23:12:06 +00007770 if (N0 == N1)
7771 return SDValue();
7772
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007773 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00007774 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007775 SDValue N00 = N0->getOperand(0);
7776 SDValue N01 = N0->getOperand(1);
7777 return DAG.getNode(Opcode, DL, VT,
7778 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7779 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7780}
7781
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007782static SDValue PerformMULCombine(SDNode *N,
7783 TargetLowering::DAGCombinerInfo &DCI,
7784 const ARMSubtarget *Subtarget) {
7785 SelectionDAG &DAG = DCI.DAG;
7786
7787 if (Subtarget->isThumb1Only())
7788 return SDValue();
7789
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007790 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7791 return SDValue();
7792
7793 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007794 if (VT.is64BitVector() || VT.is128BitVector())
7795 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007796 if (VT != MVT::i32)
7797 return SDValue();
7798
7799 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
7800 if (!C)
7801 return SDValue();
7802
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00007803 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00007804 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00007805
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007806 ShiftAmt = ShiftAmt & (32 - 1);
7807 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00007808 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007809
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00007810 SDValue Res;
7811 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00007812
7813 if (MulAmt >= 0) {
7814 if (isPowerOf2_32(MulAmt - 1)) {
7815 // (mul x, 2^N + 1) => (add (shl x, N), x)
7816 Res = DAG.getNode(ISD::ADD, DL, VT,
7817 V,
7818 DAG.getNode(ISD::SHL, DL, VT,
7819 V,
7820 DAG.getConstant(Log2_32(MulAmt - 1),
7821 MVT::i32)));
7822 } else if (isPowerOf2_32(MulAmt + 1)) {
7823 // (mul x, 2^N - 1) => (sub (shl x, N), x)
7824 Res = DAG.getNode(ISD::SUB, DL, VT,
7825 DAG.getNode(ISD::SHL, DL, VT,
7826 V,
7827 DAG.getConstant(Log2_32(MulAmt + 1),
7828 MVT::i32)),
7829 V);
7830 } else
7831 return SDValue();
7832 } else {
7833 uint64_t MulAmtAbs = -MulAmt;
7834 if (isPowerOf2_32(MulAmtAbs + 1)) {
7835 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
7836 Res = DAG.getNode(ISD::SUB, DL, VT,
7837 V,
7838 DAG.getNode(ISD::SHL, DL, VT,
7839 V,
7840 DAG.getConstant(Log2_32(MulAmtAbs + 1),
7841 MVT::i32)));
7842 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
7843 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
7844 Res = DAG.getNode(ISD::ADD, DL, VT,
7845 V,
7846 DAG.getNode(ISD::SHL, DL, VT,
7847 V,
7848 DAG.getConstant(Log2_32(MulAmtAbs-1),
7849 MVT::i32)));
7850 Res = DAG.getNode(ISD::SUB, DL, VT,
7851 DAG.getConstant(0, MVT::i32),Res);
7852
7853 } else
7854 return SDValue();
7855 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00007856
7857 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00007858 Res = DAG.getNode(ISD::SHL, DL, VT,
7859 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007860
7861 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00007862 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007863 return SDValue();
7864}
7865
Owen Anderson30c48922010-11-05 19:27:46 +00007866static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00007867 TargetLowering::DAGCombinerInfo &DCI,
7868 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00007869
Owen Anderson30c48922010-11-05 19:27:46 +00007870 // Attempt to use immediate-form VBIC
7871 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00007872 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00007873 EVT VT = N->getValueType(0);
7874 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00007875
Tanya Lattner266792a2011-04-07 15:24:20 +00007876 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7877 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00007878
Owen Anderson30c48922010-11-05 19:27:46 +00007879 APInt SplatBits, SplatUndef;
7880 unsigned SplatBitSize;
7881 bool HasAnyUndefs;
7882 if (BVN &&
7883 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7884 if (SplatBitSize <= 64) {
7885 EVT VbicVT;
7886 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
7887 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00007888 DAG, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00007889 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00007890 if (Val.getNode()) {
7891 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00007892 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00007893 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00007894 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00007895 }
7896 }
7897 }
Wesley Peck527da1b2010-11-23 03:31:01 +00007898
Evan Chenge87681c2012-02-23 01:19:06 +00007899 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007900 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
7901 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
7902 if (Result.getNode())
7903 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00007904 }
7905
Owen Anderson30c48922010-11-05 19:27:46 +00007906 return SDValue();
7907}
7908
Jim Grosbach11013ed2010-07-16 23:05:05 +00007909/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
7910static SDValue PerformORCombine(SDNode *N,
7911 TargetLowering::DAGCombinerInfo &DCI,
7912 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00007913 // Attempt to use immediate-form VORR
7914 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00007915 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00007916 EVT VT = N->getValueType(0);
7917 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00007918
Tanya Lattner266792a2011-04-07 15:24:20 +00007919 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7920 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00007921
Owen Andersonbc9b31c2010-11-03 23:15:26 +00007922 APInt SplatBits, SplatUndef;
7923 unsigned SplatBitSize;
7924 bool HasAnyUndefs;
7925 if (BVN && Subtarget->hasNEON() &&
7926 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7927 if (SplatBitSize <= 64) {
7928 EVT VorrVT;
7929 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
7930 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00007931 DAG, VorrVT, VT.is128BitVector(),
7932 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00007933 if (Val.getNode()) {
7934 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00007935 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00007936 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00007937 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00007938 }
7939 }
7940 }
7941
Evan Chenge87681c2012-02-23 01:19:06 +00007942 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007943 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7944 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
7945 if (Result.getNode())
7946 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00007947 }
7948
Nadav Rotem3a94c542012-08-13 18:52:44 +00007949 // The code below optimizes (or (and X, Y), Z).
7950 // The AND operand needs to have a single user to make these optimizations
7951 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00007952 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00007953 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00007954 return SDValue();
7955 SDValue N1 = N->getOperand(1);
7956
7957 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
7958 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
7959 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
7960 APInt SplatUndef;
7961 unsigned SplatBitSize;
7962 bool HasAnyUndefs;
7963
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00007964 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00007965 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00007966 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
7967 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00007968 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00007969 HasAnyUndefs) && !HasAnyUndefs) {
7970 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
7971 HasAnyUndefs) && !HasAnyUndefs) {
7972 // Ensure that the bit width of the constants are the same and that
7973 // the splat arguments are logical inverses as per the pattern we
7974 // are trying to simplify.
7975 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
7976 SplatBits0 == ~SplatBits1) {
7977 // Canonicalize the vector type to make instruction selection
7978 // simpler.
7979 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
7980 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
7981 N0->getOperand(1),
7982 N0->getOperand(0),
7983 N1->getOperand(0));
7984 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
7985 }
7986 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00007987 }
7988 }
7989
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00007990 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
7991 // reasonable.
7992
Jim Grosbach11013ed2010-07-16 23:05:05 +00007993 // BFI is only available on V6T2+
7994 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
7995 return SDValue();
7996
Andrew Trickef9de2a2013-05-25 02:42:55 +00007997 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00007998 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00007999 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008000 //
8001 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008002 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008003 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008004 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008005 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008006 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008007
Jim Grosbach11013ed2010-07-16 23:05:05 +00008008 if (VT != MVT::i32)
8009 return SDValue();
8010
Evan Cheng2e51bb42010-12-13 20:32:54 +00008011 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008012
Jim Grosbach11013ed2010-07-16 23:05:05 +00008013 // The value and the mask need to be constants so we can verify this is
8014 // actually a bitfield set. If the mask is 0xffff, we can do better
8015 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00008016 SDValue MaskOp = N0.getOperand(1);
8017 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8018 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008019 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008020 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008021 if (Mask == 0xffff)
8022 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008023 SDValue Res;
8024 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008025 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8026 if (N1C) {
8027 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00008028 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008029 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008030
Evan Cheng34345752010-12-11 04:11:38 +00008031 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008032 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00008033
Evan Cheng2e51bb42010-12-13 20:32:54 +00008034 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Cheng34345752010-12-11 04:11:38 +00008035 DAG.getConstant(Val, MVT::i32),
8036 DAG.getConstant(Mask, MVT::i32));
8037
8038 // Do not add new nodes to DAG combiner worklist.
8039 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008040 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00008041 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008042 } else if (N1.getOpcode() == ISD::AND) {
8043 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008044 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8045 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008046 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008047 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008048
Eric Christopherd5530962011-03-26 01:21:03 +00008049 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8050 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008051 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008052 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008053 // The pack halfword instruction works better for masks that fit it,
8054 // so use that when it's available.
8055 if (Subtarget->hasT2ExtractPack() &&
8056 (Mask == 0xffff || Mask == 0xffff0000))
8057 return SDValue();
8058 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008059 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008060 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopherd5530962011-03-26 01:21:03 +00008061 DAG.getConstant(amt, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008062 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008063 DAG.getConstant(Mask, MVT::i32));
8064 // Do not add new nodes to DAG combiner worklist.
8065 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008066 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008067 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008068 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008069 // The pack halfword instruction works better for masks that fit it,
8070 // so use that when it's available.
8071 if (Subtarget->hasT2ExtractPack() &&
8072 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8073 return SDValue();
8074 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008075 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008076 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008077 DAG.getConstant(lsb, MVT::i32));
8078 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopherd5530962011-03-26 01:21:03 +00008079 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008080 // Do not add new nodes to DAG combiner worklist.
8081 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008082 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008083 }
8084 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008085
Evan Cheng2e51bb42010-12-13 20:32:54 +00008086 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8087 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8088 ARM::isBitFieldInvertedMask(~Mask)) {
8089 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8090 // where lsb(mask) == #shamt and masked bits of B are known zero.
8091 SDValue ShAmt = N00.getOperand(1);
8092 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008093 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008094 if (ShAmtC != LSB)
8095 return SDValue();
8096
8097 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
8098 DAG.getConstant(~Mask, MVT::i32));
8099
8100 // Do not add new nodes to DAG combiner worklist.
8101 DCI.CombineTo(N, Res, false);
8102 }
8103
Jim Grosbach11013ed2010-07-16 23:05:05 +00008104 return SDValue();
8105}
8106
Evan Chenge87681c2012-02-23 01:19:06 +00008107static SDValue PerformXORCombine(SDNode *N,
8108 TargetLowering::DAGCombinerInfo &DCI,
8109 const ARMSubtarget *Subtarget) {
8110 EVT VT = N->getValueType(0);
8111 SelectionDAG &DAG = DCI.DAG;
8112
8113 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8114 return SDValue();
8115
8116 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008117 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8118 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8119 if (Result.getNode())
8120 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008121 }
8122
8123 return SDValue();
8124}
8125
Evan Cheng6d02d902011-06-15 01:12:31 +00008126/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8127/// the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00008128static SDValue PerformBFICombine(SDNode *N,
8129 TargetLowering::DAGCombinerInfo &DCI) {
8130 SDValue N1 = N->getOperand(1);
8131 if (N1.getOpcode() == ISD::AND) {
8132 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8133 if (!N11C)
8134 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008135 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008136 unsigned LSB = countTrailingZeros(~InvMask);
8137 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Evan Cheng6d02d902011-06-15 01:12:31 +00008138 unsigned Mask = (1 << Width)-1;
Evan Chengc1778132010-12-14 03:22:07 +00008139 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008140 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008141 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00008142 N->getOperand(0), N1.getOperand(0),
8143 N->getOperand(2));
8144 }
8145 return SDValue();
8146}
8147
Bob Wilson22806742010-09-22 22:09:21 +00008148/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8149/// ARMISD::VMOVRRD.
8150static SDValue PerformVMOVRRDCombine(SDNode *N,
8151 TargetLowering::DAGCombinerInfo &DCI) {
8152 // vmovrrd(vmovdrr x, y) -> x,y
8153 SDValue InDouble = N->getOperand(0);
8154 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
8155 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008156
8157 // vmovrrd(load f64) -> (load i32), (load i32)
8158 SDNode *InNode = InDouble.getNode();
8159 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8160 InNode->getValueType(0) == MVT::f64 &&
8161 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8162 !cast<LoadSDNode>(InNode)->isVolatile()) {
8163 // TODO: Should this be done for non-FrameIndex operands?
8164 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8165
8166 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008167 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008168 SDValue BasePtr = LD->getBasePtr();
8169 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8170 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008171 LD->isNonTemporal(), LD->isInvariant(),
8172 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008173
8174 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8175 DAG.getConstant(4, MVT::i32));
8176 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8177 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008178 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008179 std::min(4U, LD->getAlignment() / 2));
8180
8181 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
8182 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
8183 DCI.RemoveFromWorklist(LD);
8184 DAG.DeleteNode(LD);
8185 return Result;
8186 }
8187
Bob Wilson22806742010-09-22 22:09:21 +00008188 return SDValue();
8189}
8190
8191/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8192/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8193static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8194 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8195 SDValue Op0 = N->getOperand(0);
8196 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00008197 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008198 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008199 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008200 Op1 = Op1.getOperand(0);
8201 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8202 Op0.getNode() == Op1.getNode() &&
8203 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008204 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00008205 N->getValueType(0), Op0.getOperand(0));
8206 return SDValue();
8207}
8208
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008209/// PerformSTORECombine - Target-specific dag combine xforms for
8210/// ISD::STORE.
8211static SDValue PerformSTORECombine(SDNode *N,
8212 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008213 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008214 if (St->isVolatile())
8215 return SDValue();
8216
Andrew Trickbc325162012-07-18 18:34:24 +00008217 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosiere0e38f62012-04-09 20:32:02 +00008218 // pack all of the elements in one place. Next, store to memory in fewer
8219 // chunks.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008220 SDValue StVal = St->getValue();
Chad Rosiere0e38f62012-04-09 20:32:02 +00008221 EVT VT = StVal.getValueType();
8222 if (St->isTruncatingStore() && VT.isVector()) {
8223 SelectionDAG &DAG = DCI.DAG;
8224 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8225 EVT StVT = St->getMemoryVT();
8226 unsigned NumElems = VT.getVectorNumElements();
8227 assert(StVT != VT && "Cannot truncate to the same type");
8228 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
8229 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
8230
8231 // From, To sizes and ElemCount must be pow of two
8232 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
8233
8234 // We are going to use the original vector elt for storing.
8235 // Accumulated smaller vector elements must be a multiple of the store size.
8236 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
8237
8238 unsigned SizeRatio = FromEltSz / ToEltSz;
8239 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
8240
8241 // Create a type on which we perform the shuffle.
8242 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
8243 NumElems*SizeRatio);
8244 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
8245
Andrew Trickef9de2a2013-05-25 02:42:55 +00008246 SDLoc DL(St);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008247 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
8248 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
8249 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
8250
8251 // Can't shuffle using an illegal type.
8252 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
8253
8254 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
8255 DAG.getUNDEF(WideVec.getValueType()),
8256 ShuffleVec.data());
8257 // At this point all of the data is stored at the bottom of the
8258 // register. We now need to save it to mem.
8259
8260 // Find the largest store unit
8261 MVT StoreType = MVT::i8;
8262 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
8263 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
8264 MVT Tp = (MVT::SimpleValueType)tp;
8265 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
8266 StoreType = Tp;
8267 }
8268 // Didn't find a legal store type.
8269 if (!TLI.isTypeLegal(StoreType))
8270 return SDValue();
8271
8272 // Bitcast the original vector into a vector of store-size units
8273 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
8274 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
8275 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
8276 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
8277 SmallVector<SDValue, 8> Chains;
8278 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
8279 TLI.getPointerTy());
8280 SDValue BasePtr = St->getBasePtr();
8281
8282 // Perform one or more big stores into memory.
8283 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
8284 for (unsigned I = 0; I < E; I++) {
8285 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
8286 StoreType, ShuffWide,
8287 DAG.getIntPtrConstant(I));
8288 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
8289 St->getPointerInfo(), St->isVolatile(),
8290 St->isNonTemporal(), St->getAlignment());
8291 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
8292 Increment);
8293 Chains.push_back(Ch);
8294 }
8295 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0],
8296 Chains.size());
8297 }
8298
8299 if (!ISD::isNormalStore(St))
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008300 return SDValue();
8301
Chad Rosier99cbde92012-04-09 19:38:15 +00008302 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
8303 // ARM stores of arguments in the same cache line.
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008304 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier99cbde92012-04-09 19:38:15 +00008305 StVal.getNode()->hasOneUse()) {
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008306 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008307 SDLoc DL(St);
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008308 SDValue BasePtr = St->getBasePtr();
8309 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
8310 StVal.getNode()->getOperand(0), BasePtr,
8311 St->getPointerInfo(), St->isVolatile(),
8312 St->isNonTemporal(), St->getAlignment());
8313
8314 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8315 DAG.getConstant(4, MVT::i32));
8316 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
8317 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
8318 St->isNonTemporal(),
8319 std::min(4U, St->getAlignment() / 2));
8320 }
8321
8322 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008323 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8324 return SDValue();
8325
Chad Rosier99cbde92012-04-09 19:38:15 +00008326 // Bitcast an i64 store extracted from a vector to f64.
8327 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008328 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008329 SDLoc dl(StVal);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008330 SDValue IntVec = StVal.getOperand(0);
8331 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8332 IntVec.getValueType().getVectorNumElements());
8333 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
8334 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
8335 Vec, StVal.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008336 dl = SDLoc(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008337 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
8338 // Make the DAGCombiner fold the bitcasts.
8339 DCI.AddToWorklist(Vec.getNode());
8340 DCI.AddToWorklist(ExtElt.getNode());
8341 DCI.AddToWorklist(V.getNode());
8342 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
8343 St->getPointerInfo(), St->isVolatile(),
8344 St->isNonTemporal(), St->getAlignment(),
8345 St->getTBAAInfo());
8346}
8347
8348/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8349/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8350/// i64 vector to have f64 elements, since the value can then be loaded
8351/// directly into a VFP register.
8352static bool hasNormalLoadOperand(SDNode *N) {
8353 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8354 for (unsigned i = 0; i < NumElts; ++i) {
8355 SDNode *Elt = N->getOperand(i).getNode();
8356 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8357 return true;
8358 }
8359 return false;
8360}
8361
Bob Wilsoncb6db982010-09-17 22:59:05 +00008362/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8363/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008364static SDValue PerformBUILD_VECTORCombine(SDNode *N,
8365 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilsoncb6db982010-09-17 22:59:05 +00008366 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8367 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8368 // into a pair of GPRs, which is fine when the value is used as a scalar,
8369 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008370 SelectionDAG &DAG = DCI.DAG;
8371 if (N->getNumOperands() == 2) {
8372 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8373 if (RV.getNode())
8374 return RV;
8375 }
Bob Wilsoncb6db982010-09-17 22:59:05 +00008376
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008377 // Load i64 elements as f64 values so that type legalization does not split
8378 // them up into i32 values.
8379 EVT VT = N->getValueType(0);
8380 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8381 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008382 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008383 SmallVector<SDValue, 8> Ops;
8384 unsigned NumElts = VT.getVectorNumElements();
8385 for (unsigned i = 0; i < NumElts; ++i) {
8386 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8387 Ops.push_back(V);
8388 // Make the DAGCombiner fold the bitcast.
8389 DCI.AddToWorklist(V.getNode());
8390 }
8391 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
8392 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
8393 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8394}
8395
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008396/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
8397static SDValue
8398PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8399 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
8400 // At that time, we may have inserted bitcasts from integer to float.
8401 // If these bitcasts have survived DAGCombine, change the lowering of this
8402 // BUILD_VECTOR in something more vector friendly, i.e., that does not
8403 // force to use floating point types.
8404
8405 // Make sure we can change the type of the vector.
8406 // This is possible iff:
8407 // 1. The vector is only used in a bitcast to a integer type. I.e.,
8408 // 1.1. Vector is used only once.
8409 // 1.2. Use is a bit convert to an integer type.
8410 // 2. The size of its operands are 32-bits (64-bits are not legal).
8411 EVT VT = N->getValueType(0);
8412 EVT EltVT = VT.getVectorElementType();
8413
8414 // Check 1.1. and 2.
8415 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
8416 return SDValue();
8417
8418 // By construction, the input type must be float.
8419 assert(EltVT == MVT::f32 && "Unexpected type!");
8420
8421 // Check 1.2.
8422 SDNode *Use = *N->use_begin();
8423 if (Use->getOpcode() != ISD::BITCAST ||
8424 Use->getValueType(0).isFloatingPoint())
8425 return SDValue();
8426
8427 // Check profitability.
8428 // Model is, if more than half of the relevant operands are bitcast from
8429 // i32, turn the build_vector into a sequence of insert_vector_elt.
8430 // Relevant operands are everything that is not statically
8431 // (i.e., at compile time) bitcasted.
8432 unsigned NumOfBitCastedElts = 0;
8433 unsigned NumElts = VT.getVectorNumElements();
8434 unsigned NumOfRelevantElts = NumElts;
8435 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
8436 SDValue Elt = N->getOperand(Idx);
8437 if (Elt->getOpcode() == ISD::BITCAST) {
8438 // Assume only bit cast to i32 will go away.
8439 if (Elt->getOperand(0).getValueType() == MVT::i32)
8440 ++NumOfBitCastedElts;
8441 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt))
8442 // Constants are statically casted, thus do not count them as
8443 // relevant operands.
8444 --NumOfRelevantElts;
8445 }
8446
8447 // Check if more than half of the elements require a non-free bitcast.
8448 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
8449 return SDValue();
8450
8451 SelectionDAG &DAG = DCI.DAG;
8452 // Create the new vector type.
8453 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
8454 // Check if the type is legal.
8455 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8456 if (!TLI.isTypeLegal(VecVT))
8457 return SDValue();
8458
8459 // Combine:
8460 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
8461 // => BITCAST INSERT_VECTOR_ELT
8462 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
8463 // (BITCAST EN), N.
8464 SDValue Vec = DAG.getUNDEF(VecVT);
8465 SDLoc dl(N);
8466 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
8467 SDValue V = N->getOperand(Idx);
8468 if (V.getOpcode() == ISD::UNDEF)
8469 continue;
8470 if (V.getOpcode() == ISD::BITCAST &&
8471 V->getOperand(0).getValueType() == MVT::i32)
8472 // Fold obvious case.
8473 V = V.getOperand(0);
8474 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00008475 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008476 // Make the DAGCombiner fold the bitcasts.
8477 DCI.AddToWorklist(V.getNode());
8478 }
8479 SDValue LaneIdx = DAG.getConstant(Idx, MVT::i32);
8480 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
8481 }
8482 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
8483 // Make the DAGCombiner fold the bitcasts.
8484 DCI.AddToWorklist(Vec.getNode());
8485 return Vec;
8486}
8487
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008488/// PerformInsertEltCombine - Target-specific dag combine xforms for
8489/// ISD::INSERT_VECTOR_ELT.
8490static SDValue PerformInsertEltCombine(SDNode *N,
8491 TargetLowering::DAGCombinerInfo &DCI) {
8492 // Bitcast an i64 load inserted into a vector to f64.
8493 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8494 EVT VT = N->getValueType(0);
8495 SDNode *Elt = N->getOperand(1).getNode();
8496 if (VT.getVectorElementType() != MVT::i64 ||
8497 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8498 return SDValue();
8499
8500 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008501 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008502 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8503 VT.getVectorNumElements());
8504 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8505 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8506 // Make the DAGCombiner fold the bitcasts.
8507 DCI.AddToWorklist(Vec.getNode());
8508 DCI.AddToWorklist(V.getNode());
8509 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8510 Vec, V, N->getOperand(2));
8511 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00008512}
8513
Bob Wilsonc7334a12010-10-27 20:38:28 +00008514/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8515/// ISD::VECTOR_SHUFFLE.
8516static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8517 // The LLVM shufflevector instruction does not require the shuffle mask
8518 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8519 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8520 // operands do not match the mask length, they are extended by concatenating
8521 // them with undef vectors. That is probably the right thing for other
8522 // targets, but for NEON it is better to concatenate two double-register
8523 // size vector operands into a single quad-register size vector. Do that
8524 // transformation here:
8525 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8526 // shuffle(concat(v1, v2), undef)
8527 SDValue Op0 = N->getOperand(0);
8528 SDValue Op1 = N->getOperand(1);
8529 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8530 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8531 Op0.getNumOperands() != 2 ||
8532 Op1.getNumOperands() != 2)
8533 return SDValue();
8534 SDValue Concat0Op1 = Op0.getOperand(1);
8535 SDValue Concat1Op1 = Op1.getOperand(1);
8536 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8537 Concat1Op1.getOpcode() != ISD::UNDEF)
8538 return SDValue();
8539 // Skip the transformation if any of the types are illegal.
8540 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8541 EVT VT = N->getValueType(0);
8542 if (!TLI.isTypeLegal(VT) ||
8543 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8544 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8545 return SDValue();
8546
Andrew Trickef9de2a2013-05-25 02:42:55 +00008547 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008548 Op0.getOperand(0), Op1.getOperand(0));
8549 // Translate the shuffle mask.
8550 SmallVector<int, 16> NewMask;
8551 unsigned NumElts = VT.getVectorNumElements();
8552 unsigned HalfElts = NumElts/2;
8553 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8554 for (unsigned n = 0; n < NumElts; ++n) {
8555 int MaskElt = SVN->getMaskElt(n);
8556 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00008557 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00008558 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00008559 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00008560 NewElt = HalfElts + MaskElt - NumElts;
8561 NewMask.push_back(NewElt);
8562 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00008563 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008564 DAG.getUNDEF(VT), NewMask.data());
8565}
8566
Bob Wilson06fce872011-02-07 17:43:21 +00008567/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
8568/// NEON load/store intrinsics to merge base address updates.
8569static SDValue CombineBaseUpdate(SDNode *N,
8570 TargetLowering::DAGCombinerInfo &DCI) {
8571 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8572 return SDValue();
8573
8574 SelectionDAG &DAG = DCI.DAG;
8575 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8576 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
8577 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
8578 SDValue Addr = N->getOperand(AddrOpIdx);
8579
8580 // Search for a use of the address operand that is an increment.
8581 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8582 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8583 SDNode *User = *UI;
8584 if (User->getOpcode() != ISD::ADD ||
8585 UI.getUse().getResNo() != Addr.getResNo())
8586 continue;
8587
8588 // Check that the add is independent of the load/store. Otherwise, folding
8589 // it would create a cycle.
8590 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8591 continue;
8592
8593 // Find the new opcode for the updating load/store.
8594 bool isLoad = true;
8595 bool isLaneOp = false;
8596 unsigned NewOpc = 0;
8597 unsigned NumVecs = 0;
8598 if (isIntrinsic) {
8599 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8600 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00008601 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008602 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8603 NumVecs = 1; break;
8604 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8605 NumVecs = 2; break;
8606 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8607 NumVecs = 3; break;
8608 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8609 NumVecs = 4; break;
8610 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8611 NumVecs = 2; isLaneOp = true; break;
8612 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8613 NumVecs = 3; isLaneOp = true; break;
8614 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8615 NumVecs = 4; isLaneOp = true; break;
8616 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
8617 NumVecs = 1; isLoad = false; break;
8618 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
8619 NumVecs = 2; isLoad = false; break;
8620 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8621 NumVecs = 3; isLoad = false; break;
8622 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8623 NumVecs = 4; isLoad = false; break;
8624 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8625 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8626 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8627 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8628 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8629 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8630 }
8631 } else {
8632 isLaneOp = true;
8633 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00008634 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008635 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8636 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8637 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8638 }
8639 }
8640
8641 // Find the size of memory referenced by the load/store.
8642 EVT VecTy;
8643 if (isLoad)
8644 VecTy = N->getValueType(0);
Owen Anderson77aa2662011-04-05 21:48:57 +00008645 else
Bob Wilson06fce872011-02-07 17:43:21 +00008646 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8647 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8648 if (isLaneOp)
8649 NumBytes /= VecTy.getVectorNumElements();
8650
8651 // If the increment is a constant, it must match the memory ref size.
8652 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8653 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8654 uint64_t IncVal = CInc->getZExtValue();
8655 if (IncVal != NumBytes)
8656 continue;
8657 } else if (NumBytes >= 3 * 16) {
8658 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8659 // separate instructions that make it harder to use a non-constant update.
8660 continue;
8661 }
8662
8663 // Create the new updating load/store node.
8664 EVT Tys[6];
8665 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8666 unsigned n;
8667 for (n = 0; n < NumResultVecs; ++n)
8668 Tys[n] = VecTy;
8669 Tys[n++] = MVT::i32;
8670 Tys[n] = MVT::Other;
Craig Topperabb4ac72014-04-16 06:10:51 +00008671 SDVTList SDTys = DAG.getVTList(ArrayRef<EVT>(Tys, NumResultVecs+2));
Bob Wilson06fce872011-02-07 17:43:21 +00008672 SmallVector<SDValue, 8> Ops;
8673 Ops.push_back(N->getOperand(0)); // incoming chain
8674 Ops.push_back(N->getOperand(AddrOpIdx));
8675 Ops.push_back(Inc);
8676 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8677 Ops.push_back(N->getOperand(i));
8678 }
8679 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008680 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, SDLoc(N), SDTys,
Bob Wilson06fce872011-02-07 17:43:21 +00008681 Ops.data(), Ops.size(),
8682 MemInt->getMemoryVT(),
8683 MemInt->getMemOperand());
8684
8685 // Update the uses.
8686 std::vector<SDValue> NewResults;
8687 for (unsigned i = 0; i < NumResultVecs; ++i) {
8688 NewResults.push_back(SDValue(UpdN.getNode(), i));
8689 }
8690 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8691 DCI.CombineTo(N, NewResults);
8692 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8693
8694 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00008695 }
Bob Wilson06fce872011-02-07 17:43:21 +00008696 return SDValue();
8697}
8698
Bob Wilson2d790df2010-11-28 06:51:26 +00008699/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8700/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8701/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8702/// return true.
8703static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8704 SelectionDAG &DAG = DCI.DAG;
8705 EVT VT = N->getValueType(0);
8706 // vldN-dup instructions only support 64-bit vectors for N > 1.
8707 if (!VT.is64BitVector())
8708 return false;
8709
8710 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8711 SDNode *VLD = N->getOperand(0).getNode();
8712 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8713 return false;
8714 unsigned NumVecs = 0;
8715 unsigned NewOpc = 0;
8716 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8717 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8718 NumVecs = 2;
8719 NewOpc = ARMISD::VLD2DUP;
8720 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8721 NumVecs = 3;
8722 NewOpc = ARMISD::VLD3DUP;
8723 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8724 NumVecs = 4;
8725 NewOpc = ARMISD::VLD4DUP;
8726 } else {
8727 return false;
8728 }
8729
8730 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8731 // numbers match the load.
8732 unsigned VLDLaneNo =
8733 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8734 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8735 UI != UE; ++UI) {
8736 // Ignore uses of the chain result.
8737 if (UI.getUse().getResNo() == NumVecs)
8738 continue;
8739 SDNode *User = *UI;
8740 if (User->getOpcode() != ARMISD::VDUPLANE ||
8741 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8742 return false;
8743 }
8744
8745 // Create the vldN-dup node.
8746 EVT Tys[5];
8747 unsigned n;
8748 for (n = 0; n < NumVecs; ++n)
8749 Tys[n] = VT;
8750 Tys[n] = MVT::Other;
Craig Topperabb4ac72014-04-16 06:10:51 +00008751 SDVTList SDTys = DAG.getVTList(ArrayRef<EVT>(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00008752 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8753 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008754 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Bob Wilson2d790df2010-11-28 06:51:26 +00008755 Ops, 2, VLDMemInt->getMemoryVT(),
8756 VLDMemInt->getMemOperand());
8757
8758 // Update the uses.
8759 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8760 UI != UE; ++UI) {
8761 unsigned ResNo = UI.getUse().getResNo();
8762 // Ignore uses of the chain result.
8763 if (ResNo == NumVecs)
8764 continue;
8765 SDNode *User = *UI;
8766 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8767 }
8768
8769 // Now the vldN-lane intrinsic is dead except for its chain result.
8770 // Update uses of the chain.
8771 std::vector<SDValue> VLDDupResults;
8772 for (unsigned n = 0; n < NumVecs; ++n)
8773 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8774 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8775 DCI.CombineTo(VLD, VLDDupResults);
8776
8777 return true;
8778}
8779
Bob Wilson103a0dc2010-07-14 01:22:12 +00008780/// PerformVDUPLANECombine - Target-specific dag combine xforms for
8781/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00008782static SDValue PerformVDUPLANECombine(SDNode *N,
8783 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00008784 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00008785
Bob Wilson2d790df2010-11-28 06:51:26 +00008786 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
8787 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
8788 if (CombineVLDDUP(N, DCI))
8789 return SDValue(N, 0);
8790
8791 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
8792 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00008793 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00008794 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00008795 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00008796 return SDValue();
8797
8798 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
8799 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
8800 // The canonical VMOV for a zero vector uses a 32-bit element size.
8801 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8802 unsigned EltBits;
8803 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
8804 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00008805 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00008806 if (EltSize > VT.getVectorElementType().getSizeInBits())
8807 return SDValue();
8808
Andrew Trickef9de2a2013-05-25 02:42:55 +00008809 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00008810}
8811
Eric Christopher1b8b94192011-06-29 21:10:36 +00008812// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosierfa8d8932011-06-24 19:23:04 +00008813// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
8814static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
8815{
Chad Rosier6b610b32011-06-28 17:26:57 +00008816 integerPart cN;
8817 integerPart c0 = 0;
Chad Rosierfa8d8932011-06-24 19:23:04 +00008818 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
8819 I != E; I++) {
8820 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
8821 if (!C)
8822 return false;
8823
Eric Christopher1b8b94192011-06-29 21:10:36 +00008824 bool isExact;
Chad Rosierfa8d8932011-06-24 19:23:04 +00008825 APFloat APF = C->getValueAPF();
8826 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
8827 != APFloat::opOK || !isExact)
8828 return false;
8829
8830 c0 = (I == 0) ? cN : c0;
8831 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
8832 return false;
8833 }
8834 C = c0;
8835 return true;
8836}
8837
8838/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
8839/// can replace combinations of VMUL and VCVT (floating-point to integer)
8840/// when the VMUL has a constant operand that is a power of 2.
8841///
8842/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8843/// vmul.f32 d16, d17, d16
8844/// vcvt.s32.f32 d16, d16
8845/// becomes:
8846/// vcvt.s32.f32 d16, d16, #3
8847static SDValue PerformVCVTCombine(SDNode *N,
8848 TargetLowering::DAGCombinerInfo &DCI,
8849 const ARMSubtarget *Subtarget) {
8850 SelectionDAG &DAG = DCI.DAG;
8851 SDValue Op = N->getOperand(0);
8852
8853 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
8854 Op.getOpcode() != ISD::FMUL)
8855 return SDValue();
8856
8857 uint64_t C;
8858 SDValue N0 = Op->getOperand(0);
8859 SDValue ConstVec = Op->getOperand(1);
8860 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
8861
Eric Christopher1b8b94192011-06-29 21:10:36 +00008862 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosierfa8d8932011-06-24 19:23:04 +00008863 !isConstVecPow2(ConstVec, isSigned, C))
8864 return SDValue();
8865
Tim Northover7cbc2152013-06-28 15:29:25 +00008866 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
8867 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
8868 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
8869 // These instructions only exist converting from f32 to i32. We can handle
8870 // smaller integers by generating an extra truncate, but larger ones would
8871 // be lossy.
8872 return SDValue();
8873 }
8874
Chad Rosierfa8d8932011-06-24 19:23:04 +00008875 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
8876 Intrinsic::arm_neon_vcvtfp2fxu;
Tim Northover7cbc2152013-06-28 15:29:25 +00008877 unsigned NumLanes = Op.getValueType().getVectorNumElements();
8878 SDValue FixConv = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
8879 NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
8880 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
8881 DAG.getConstant(Log2_64(C), MVT::i32));
8882
8883 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
8884 FixConv = DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), FixConv);
8885
8886 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +00008887}
8888
8889/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
8890/// can replace combinations of VCVT (integer to floating-point) and VDIV
8891/// when the VDIV has a constant operand that is a power of 2.
8892///
8893/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8894/// vcvt.f32.s32 d16, d16
8895/// vdiv.f32 d16, d17, d16
8896/// becomes:
8897/// vcvt.f32.s32 d16, d16, #3
8898static SDValue PerformVDIVCombine(SDNode *N,
8899 TargetLowering::DAGCombinerInfo &DCI,
8900 const ARMSubtarget *Subtarget) {
8901 SelectionDAG &DAG = DCI.DAG;
8902 SDValue Op = N->getOperand(0);
8903 unsigned OpOpcode = Op.getNode()->getOpcode();
8904
8905 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
8906 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
8907 return SDValue();
8908
8909 uint64_t C;
8910 SDValue ConstVec = N->getOperand(1);
8911 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
8912
8913 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
8914 !isConstVecPow2(ConstVec, isSigned, C))
8915 return SDValue();
8916
Tim Northover7cbc2152013-06-28 15:29:25 +00008917 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
8918 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
8919 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
8920 // These instructions only exist converting from i32 to f32. We can handle
8921 // smaller integers by generating an extra extend, but larger ones would
8922 // be lossy.
8923 return SDValue();
8924 }
8925
8926 SDValue ConvInput = Op.getOperand(0);
8927 unsigned NumLanes = Op.getValueType().getVectorNumElements();
8928 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
8929 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
8930 SDLoc(N), NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
8931 ConvInput);
8932
Eric Christopher1b8b94192011-06-29 21:10:36 +00008933 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +00008934 Intrinsic::arm_neon_vcvtfxu2fp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008935 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
Chad Rosierfa8d8932011-06-24 19:23:04 +00008936 Op.getValueType(),
Eric Christopher1b8b94192011-06-29 21:10:36 +00008937 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Tim Northover7cbc2152013-06-28 15:29:25 +00008938 ConvInput, DAG.getConstant(Log2_64(C), MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +00008939}
8940
8941/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +00008942/// operand of a vector shift operation, where all the elements of the
8943/// build_vector must have the same constant integer value.
8944static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
8945 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +00008946 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00008947 Op = Op.getOperand(0);
8948 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
8949 APInt SplatBits, SplatUndef;
8950 unsigned SplatBitSize;
8951 bool HasAnyUndefs;
8952 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
8953 HasAnyUndefs, ElementBits) ||
8954 SplatBitSize > ElementBits)
8955 return false;
8956 Cnt = SplatBits.getSExtValue();
8957 return true;
8958}
8959
8960/// isVShiftLImm - Check if this is a valid build_vector for the immediate
8961/// operand of a vector shift left operation. That value must be in the range:
8962/// 0 <= Value < ElementBits for a left shift; or
8963/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00008964static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +00008965 assert(VT.isVector() && "vector shift count is not a vector type");
8966 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8967 if (! getVShiftImm(Op, ElementBits, Cnt))
8968 return false;
8969 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
8970}
8971
8972/// isVShiftRImm - Check if this is a valid build_vector for the immediate
8973/// operand of a vector shift right operation. For a shift opcode, the value
8974/// is positive, but for an intrinsic the value count must be negative. The
8975/// absolute value must be in the range:
8976/// 1 <= |Value| <= ElementBits for a right shift; or
8977/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00008978static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +00008979 int64_t &Cnt) {
8980 assert(VT.isVector() && "vector shift count is not a vector type");
8981 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8982 if (! getVShiftImm(Op, ElementBits, Cnt))
8983 return false;
8984 if (isIntrinsic)
8985 Cnt = -Cnt;
8986 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
8987}
8988
8989/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
8990static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
8991 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
8992 switch (IntNo) {
8993 default:
8994 // Don't do anything for most intrinsics.
8995 break;
8996
8997 // Vector shifts: check for immediate versions and lower them.
8998 // Note: This is done during DAG combining instead of DAG legalizing because
8999 // the build_vectors for 64-bit vector element shift counts are generally
9000 // not legal, and it is hard to see their values after they get legalized to
9001 // loads from a constant pool.
9002 case Intrinsic::arm_neon_vshifts:
9003 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +00009004 case Intrinsic::arm_neon_vrshifts:
9005 case Intrinsic::arm_neon_vrshiftu:
9006 case Intrinsic::arm_neon_vrshiftn:
9007 case Intrinsic::arm_neon_vqshifts:
9008 case Intrinsic::arm_neon_vqshiftu:
9009 case Intrinsic::arm_neon_vqshiftsu:
9010 case Intrinsic::arm_neon_vqshiftns:
9011 case Intrinsic::arm_neon_vqshiftnu:
9012 case Intrinsic::arm_neon_vqshiftnsu:
9013 case Intrinsic::arm_neon_vqrshiftns:
9014 case Intrinsic::arm_neon_vqrshiftnu:
9015 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009016 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009017 int64_t Cnt;
9018 unsigned VShiftOpc = 0;
9019
9020 switch (IntNo) {
9021 case Intrinsic::arm_neon_vshifts:
9022 case Intrinsic::arm_neon_vshiftu:
9023 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
9024 VShiftOpc = ARMISD::VSHL;
9025 break;
9026 }
9027 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
9028 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
9029 ARMISD::VSHRs : ARMISD::VSHRu);
9030 break;
9031 }
9032 return SDValue();
9033
Bob Wilson2e076c42009-06-22 23:27:02 +00009034 case Intrinsic::arm_neon_vrshifts:
9035 case Intrinsic::arm_neon_vrshiftu:
9036 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9037 break;
9038 return SDValue();
9039
9040 case Intrinsic::arm_neon_vqshifts:
9041 case Intrinsic::arm_neon_vqshiftu:
9042 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9043 break;
9044 return SDValue();
9045
9046 case Intrinsic::arm_neon_vqshiftsu:
9047 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9048 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +00009049 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009050
Bob Wilson2e076c42009-06-22 23:27:02 +00009051 case Intrinsic::arm_neon_vrshiftn:
9052 case Intrinsic::arm_neon_vqshiftns:
9053 case Intrinsic::arm_neon_vqshiftnu:
9054 case Intrinsic::arm_neon_vqshiftnsu:
9055 case Intrinsic::arm_neon_vqrshiftns:
9056 case Intrinsic::arm_neon_vqrshiftnu:
9057 case Intrinsic::arm_neon_vqrshiftnsu:
9058 // Narrowing shifts require an immediate right shift.
9059 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9060 break;
Jim Grosbach84511e12010-06-02 21:53:11 +00009061 llvm_unreachable("invalid shift count for narrowing vector shift "
9062 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009063
9064 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00009065 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00009066 }
9067
9068 switch (IntNo) {
9069 case Intrinsic::arm_neon_vshifts:
9070 case Intrinsic::arm_neon_vshiftu:
9071 // Opcode already set above.
9072 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00009073 case Intrinsic::arm_neon_vrshifts:
9074 VShiftOpc = ARMISD::VRSHRs; break;
9075 case Intrinsic::arm_neon_vrshiftu:
9076 VShiftOpc = ARMISD::VRSHRu; break;
9077 case Intrinsic::arm_neon_vrshiftn:
9078 VShiftOpc = ARMISD::VRSHRN; break;
9079 case Intrinsic::arm_neon_vqshifts:
9080 VShiftOpc = ARMISD::VQSHLs; break;
9081 case Intrinsic::arm_neon_vqshiftu:
9082 VShiftOpc = ARMISD::VQSHLu; break;
9083 case Intrinsic::arm_neon_vqshiftsu:
9084 VShiftOpc = ARMISD::VQSHLsu; break;
9085 case Intrinsic::arm_neon_vqshiftns:
9086 VShiftOpc = ARMISD::VQSHRNs; break;
9087 case Intrinsic::arm_neon_vqshiftnu:
9088 VShiftOpc = ARMISD::VQSHRNu; break;
9089 case Intrinsic::arm_neon_vqshiftnsu:
9090 VShiftOpc = ARMISD::VQSHRNsu; break;
9091 case Intrinsic::arm_neon_vqrshiftns:
9092 VShiftOpc = ARMISD::VQRSHRNs; break;
9093 case Intrinsic::arm_neon_vqrshiftnu:
9094 VShiftOpc = ARMISD::VQRSHRNu; break;
9095 case Intrinsic::arm_neon_vqrshiftnsu:
9096 VShiftOpc = ARMISD::VQRSHRNsu; break;
9097 }
9098
Andrew Trickef9de2a2013-05-25 02:42:55 +00009099 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009100 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009101 }
9102
9103 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009104 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009105 int64_t Cnt;
9106 unsigned VShiftOpc = 0;
9107
9108 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9109 VShiftOpc = ARMISD::VSLI;
9110 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9111 VShiftOpc = ARMISD::VSRI;
9112 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009113 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009114 }
9115
Andrew Trickef9de2a2013-05-25 02:42:55 +00009116 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +00009117 N->getOperand(1), N->getOperand(2),
Owen Anderson9f944592009-08-11 20:47:22 +00009118 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009119 }
9120
9121 case Intrinsic::arm_neon_vqrshifts:
9122 case Intrinsic::arm_neon_vqrshiftu:
9123 // No immediate versions of these to check for.
9124 break;
9125 }
9126
9127 return SDValue();
9128}
9129
9130/// PerformShiftCombine - Checks for immediate versions of vector shifts and
9131/// lowers them. As with the vector shift intrinsics, this is done during DAG
9132/// combining instead of DAG legalizing because the build_vectors for 64-bit
9133/// vector element shift counts are generally not legal, and it is hard to see
9134/// their values after they get legalized to loads from a constant pool.
9135static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9136 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009137 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +00009138 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9139 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9140 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9141 SDValue N1 = N->getOperand(1);
9142 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9143 SDValue N0 = N->getOperand(0);
9144 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9145 DAG.MaskedValueIsZero(N0.getOperand(0),
9146 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009147 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +00009148 }
9149 }
Bob Wilson2e076c42009-06-22 23:27:02 +00009150
9151 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +00009152 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9153 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +00009154 return SDValue();
9155
9156 assert(ST->hasNEON() && "unexpected vector shift");
9157 int64_t Cnt;
9158
9159 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009160 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009161
9162 case ISD::SHL:
9163 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009164 return DAG.getNode(ARMISD::VSHL, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009165 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009166 break;
9167
9168 case ISD::SRA:
9169 case ISD::SRL:
9170 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
9171 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
9172 ARMISD::VSHRs : ARMISD::VSHRu);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009173 return DAG.getNode(VShiftOpc, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009174 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009175 }
9176 }
9177 return SDValue();
9178}
9179
9180/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
9181/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
9182static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
9183 const ARMSubtarget *ST) {
9184 SDValue N0 = N->getOperand(0);
9185
9186 // Check for sign- and zero-extensions of vector extract operations of 8-
9187 // and 16-bit vector elements. NEON supports these directly. They are
9188 // handled during DAG combining because type legalization will promote them
9189 // to 32-bit types and it is messy to recognize the operations after that.
9190 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9191 SDValue Vec = N0.getOperand(0);
9192 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00009193 EVT VT = N->getValueType(0);
9194 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009195 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9196
Owen Anderson9f944592009-08-11 20:47:22 +00009197 if (VT == MVT::i32 &&
9198 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +00009199 TLI.isTypeLegal(Vec.getValueType()) &&
9200 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009201
9202 unsigned Opc = 0;
9203 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009204 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009205 case ISD::SIGN_EXTEND:
9206 Opc = ARMISD::VGETLANEs;
9207 break;
9208 case ISD::ZERO_EXTEND:
9209 case ISD::ANY_EXTEND:
9210 Opc = ARMISD::VGETLANEu;
9211 break;
9212 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009213 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +00009214 }
9215 }
9216
9217 return SDValue();
9218}
9219
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009220/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9221/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9222static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9223 const ARMSubtarget *ST) {
9224 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng55f0c6b2010-07-15 22:07:12 +00009225 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009226 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9227 // a NaN; only do the transformation when it matches that behavior.
9228
9229 // For now only do this when using NEON for FP operations; if using VFP, it
9230 // is not obvious that the benefit outweighs the cost of switching to the
9231 // NEON pipeline.
9232 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9233 N->getValueType(0) != MVT::f32)
9234 return SDValue();
9235
9236 SDValue CondLHS = N->getOperand(0);
9237 SDValue CondRHS = N->getOperand(1);
9238 SDValue LHS = N->getOperand(2);
9239 SDValue RHS = N->getOperand(3);
9240 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9241
9242 unsigned Opcode = 0;
9243 bool IsReversed;
Bob Wilsonba8ac742010-02-24 22:15:53 +00009244 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009245 IsReversed = false; // x CC y ? x : y
Bob Wilsonba8ac742010-02-24 22:15:53 +00009246 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009247 IsReversed = true ; // x CC y ? y : x
9248 } else {
9249 return SDValue();
9250 }
9251
Bob Wilsonba8ac742010-02-24 22:15:53 +00009252 bool IsUnordered;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009253 switch (CC) {
9254 default: break;
9255 case ISD::SETOLT:
9256 case ISD::SETOLE:
9257 case ISD::SETLT:
9258 case ISD::SETLE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009259 case ISD::SETULT:
9260 case ISD::SETULE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009261 // If LHS is NaN, an ordered comparison will be false and the result will
9262 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
9263 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9264 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
9265 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9266 break;
9267 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
9268 // will return -0, so vmin can only be used for unsafe math or if one of
9269 // the operands is known to be nonzero.
9270 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009271 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009272 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9273 break;
9274 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009275 break;
9276
9277 case ISD::SETOGT:
9278 case ISD::SETOGE:
9279 case ISD::SETGT:
9280 case ISD::SETGE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009281 case ISD::SETUGT:
9282 case ISD::SETUGE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009283 // If LHS is NaN, an ordered comparison will be false and the result will
9284 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
9285 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9286 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
9287 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9288 break;
9289 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
9290 // will return +0, so vmax can only be used for unsafe math or if one of
9291 // the operands is known to be nonzero.
9292 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009293 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009294 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9295 break;
9296 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009297 break;
9298 }
9299
9300 if (!Opcode)
9301 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009302 return DAG.getNode(Opcode, SDLoc(N), N->getValueType(0), LHS, RHS);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009303}
9304
Evan Chengf863e3f2011-07-13 00:42:17 +00009305/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
9306SDValue
9307ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
9308 SDValue Cmp = N->getOperand(4);
9309 if (Cmp.getOpcode() != ARMISD::CMPZ)
9310 // Only looking at EQ and NE cases.
9311 return SDValue();
9312
9313 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009314 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +00009315 SDValue LHS = Cmp.getOperand(0);
9316 SDValue RHS = Cmp.getOperand(1);
9317 SDValue FalseVal = N->getOperand(0);
9318 SDValue TrueVal = N->getOperand(1);
9319 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +00009320 ARMCC::CondCodes CC =
9321 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +00009322
9323 // Simplify
9324 // mov r1, r0
9325 // cmp r1, x
9326 // mov r0, y
9327 // moveq r0, x
9328 // to
9329 // cmp r0, x
9330 // movne r0, y
9331 //
9332 // mov r1, r0
9333 // cmp r1, x
9334 // mov r0, x
9335 // movne r0, y
9336 // to
9337 // cmp r0, x
9338 // movne r0, y
9339 /// FIXME: Turn this into a target neutral optimization?
9340 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +00009341 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +00009342 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
9343 N->getOperand(3), Cmp);
9344 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
9345 SDValue ARMcc;
9346 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
9347 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
9348 N->getOperand(3), NewCmp);
9349 }
9350
9351 if (Res.getNode()) {
9352 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00009353 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +00009354 // Capture demanded bits information that would be otherwise lost.
9355 if (KnownZero == 0xfffffffe)
9356 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9357 DAG.getValueType(MVT::i1));
9358 else if (KnownZero == 0xffffff00)
9359 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9360 DAG.getValueType(MVT::i8));
9361 else if (KnownZero == 0xffff0000)
9362 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9363 DAG.getValueType(MVT::i16));
9364 }
9365
9366 return Res;
9367}
9368
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009369SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +00009370 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009371 switch (N->getOpcode()) {
9372 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009373 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +00009374 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009375 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009376 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +00009377 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +00009378 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
9379 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +00009380 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00009381 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson22806742010-09-22 22:09:21 +00009382 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009383 case ISD::STORE: return PerformSTORECombine(N, DCI);
9384 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
9385 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +00009386 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +00009387 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +00009388 case ISD::FP_TO_SINT:
9389 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
9390 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009391 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00009392 case ISD::SHL:
9393 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009394 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00009395 case ISD::SIGN_EXTEND:
9396 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009397 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
9398 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +00009399 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson06fce872011-02-07 17:43:21 +00009400 case ARMISD::VLD2DUP:
9401 case ARMISD::VLD3DUP:
9402 case ARMISD::VLD4DUP:
9403 return CombineBaseUpdate(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009404 case ARMISD::BUILD_VECTOR:
9405 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +00009406 case ISD::INTRINSIC_VOID:
9407 case ISD::INTRINSIC_W_CHAIN:
9408 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9409 case Intrinsic::arm_neon_vld1:
9410 case Intrinsic::arm_neon_vld2:
9411 case Intrinsic::arm_neon_vld3:
9412 case Intrinsic::arm_neon_vld4:
9413 case Intrinsic::arm_neon_vld2lane:
9414 case Intrinsic::arm_neon_vld3lane:
9415 case Intrinsic::arm_neon_vld4lane:
9416 case Intrinsic::arm_neon_vst1:
9417 case Intrinsic::arm_neon_vst2:
9418 case Intrinsic::arm_neon_vst3:
9419 case Intrinsic::arm_neon_vst4:
9420 case Intrinsic::arm_neon_vst2lane:
9421 case Intrinsic::arm_neon_vst3lane:
9422 case Intrinsic::arm_neon_vst4lane:
9423 return CombineBaseUpdate(N, DCI);
9424 default: break;
9425 }
9426 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009427 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009428 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009429}
9430
Evan Chengd42641c2011-02-02 01:06:55 +00009431bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9432 EVT VT) const {
9433 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9434}
9435
Matt Arsenault25793a32014-02-05 23:15:53 +00009436bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT, unsigned,
9437 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009438 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +00009439 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009440
9441 switch (VT.getSimpleVT().SimpleTy) {
9442 default:
9443 return false;
9444 case MVT::i8:
9445 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009446 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009447 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +00009448 if (AllowsUnaligned) {
9449 if (Fast)
9450 *Fast = Subtarget->hasV7Ops();
9451 return true;
9452 }
9453 return false;
9454 }
Evan Chengeec6bc62012-08-15 17:44:53 +00009455 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009456 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009457 // For any little-endian targets with neon, we can support unaligned ld/st
9458 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +00009459 // A big-endian target may also explicitly support unaligned accesses
Evan Cheng79e2ca92012-12-10 23:21:26 +00009460 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) {
9461 if (Fast)
9462 *Fast = true;
9463 return true;
9464 }
9465 return false;
9466 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009467 }
9468}
9469
Lang Hames9929c422011-11-02 22:52:45 +00009470static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
9471 unsigned AlignCheck) {
9472 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
9473 (DstAlign == 0 || DstAlign % AlignCheck == 0));
9474}
9475
9476EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
9477 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00009478 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +00009479 bool MemcpyStrSrc,
9480 MachineFunction &MF) const {
9481 const Function *F = MF.getFunction();
9482
9483 // See if we can use NEON instructions for this...
Evan Cheng962711e2012-12-12 02:34:41 +00009484 if ((!IsMemset || ZeroMemset) &&
Evan Cheng79e2ca92012-12-10 23:21:26 +00009485 Subtarget->hasNEON() &&
Bill Wendling698e84f2012-12-30 10:32:01 +00009486 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
9487 Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009488 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +00009489 if (Size >= 16 &&
9490 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault25793a32014-02-05 23:15:53 +00009491 (allowsUnalignedMemoryAccesses(MVT::v2f64, 0, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009492 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +00009493 } else if (Size >= 8 &&
9494 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault25793a32014-02-05 23:15:53 +00009495 (allowsUnalignedMemoryAccesses(MVT::f64, 0, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009496 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +00009497 }
9498 }
9499
Lang Hamesb85fcd02011-11-08 18:56:23 +00009500 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +00009501 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +00009502 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +00009503 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +00009504 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +00009505
Lang Hames9929c422011-11-02 22:52:45 +00009506 // Let the target-independent logic figure it out.
9507 return MVT::Other;
9508}
9509
Evan Cheng9ec512d2012-12-06 19:13:27 +00009510bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
9511 if (Val.getOpcode() != ISD::LOAD)
9512 return false;
9513
9514 EVT VT1 = Val.getValueType();
9515 if (!VT1.isSimple() || !VT1.isInteger() ||
9516 !VT2.isSimple() || !VT2.isInteger())
9517 return false;
9518
9519 switch (VT1.getSimpleVT().SimpleTy) {
9520 default: break;
9521 case MVT::i1:
9522 case MVT::i8:
9523 case MVT::i16:
9524 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
9525 return true;
9526 }
9527
9528 return false;
9529}
9530
Tim Northovercc2e9032013-08-06 13:58:03 +00009531bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
9532 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
9533 return false;
9534
9535 if (!isTypeLegal(EVT::getEVT(Ty1)))
9536 return false;
9537
9538 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
9539
9540 // Assuming the caller doesn't have a zeroext or signext return parameter,
9541 // truncation all the way down to i1 is valid.
9542 return true;
9543}
9544
9545
Evan Chengdc49a8d2009-08-14 20:09:37 +00009546static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
9547 if (V < 0)
9548 return false;
9549
9550 unsigned Scale = 1;
9551 switch (VT.getSimpleVT().SimpleTy) {
9552 default: return false;
9553 case MVT::i1:
9554 case MVT::i8:
9555 // Scale == 1;
9556 break;
9557 case MVT::i16:
9558 // Scale == 2;
9559 Scale = 2;
9560 break;
9561 case MVT::i32:
9562 // Scale == 4;
9563 Scale = 4;
9564 break;
9565 }
9566
9567 if ((V & (Scale - 1)) != 0)
9568 return false;
9569 V /= Scale;
9570 return V == (V & ((1LL << 5) - 1));
9571}
9572
9573static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
9574 const ARMSubtarget *Subtarget) {
9575 bool isNeg = false;
9576 if (V < 0) {
9577 isNeg = true;
9578 V = - V;
9579 }
9580
9581 switch (VT.getSimpleVT().SimpleTy) {
9582 default: return false;
9583 case MVT::i1:
9584 case MVT::i8:
9585 case MVT::i16:
9586 case MVT::i32:
9587 // + imm12 or - imm8
9588 if (isNeg)
9589 return V == (V & ((1LL << 8) - 1));
9590 return V == (V & ((1LL << 12) - 1));
9591 case MVT::f32:
9592 case MVT::f64:
9593 // Same as ARM mode. FIXME: NEON?
9594 if (!Subtarget->hasVFP2())
9595 return false;
9596 if ((V & 3) != 0)
9597 return false;
9598 V >>= 2;
9599 return V == (V & ((1LL << 8) - 1));
9600 }
9601}
9602
Evan Cheng2150b922007-03-12 23:30:29 +00009603/// isLegalAddressImmediate - Return true if the integer value can be used
9604/// as the offset of the target addressing mode for load / store of the
9605/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009606static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009607 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +00009608 if (V == 0)
9609 return true;
9610
Evan Chengce5dfb62009-03-09 19:15:00 +00009611 if (!VT.isSimple())
9612 return false;
9613
Evan Chengdc49a8d2009-08-14 20:09:37 +00009614 if (Subtarget->isThumb1Only())
9615 return isLegalT1AddressImmediate(V, VT);
9616 else if (Subtarget->isThumb2())
9617 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +00009618
Evan Chengdc49a8d2009-08-14 20:09:37 +00009619 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +00009620 if (V < 0)
9621 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +00009622 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +00009623 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +00009624 case MVT::i1:
9625 case MVT::i8:
9626 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +00009627 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009628 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +00009629 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +00009630 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009631 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +00009632 case MVT::f32:
9633 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009634 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +00009635 return false;
Evan Chengbef131de2007-05-03 02:00:18 +00009636 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +00009637 return false;
9638 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009639 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +00009640 }
Evan Cheng10043e22007-01-19 07:51:42 +00009641}
9642
Evan Chengdc49a8d2009-08-14 20:09:37 +00009643bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
9644 EVT VT) const {
9645 int Scale = AM.Scale;
9646 if (Scale < 0)
9647 return false;
9648
9649 switch (VT.getSimpleVT().SimpleTy) {
9650 default: return false;
9651 case MVT::i1:
9652 case MVT::i8:
9653 case MVT::i16:
9654 case MVT::i32:
9655 if (Scale == 1)
9656 return true;
9657 // r + r << imm
9658 Scale = Scale & ~1;
9659 return Scale == 2 || Scale == 4 || Scale == 8;
9660 case MVT::i64:
9661 // r + r
9662 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
9663 return true;
9664 return false;
9665 case MVT::isVoid:
9666 // Note, we allow "void" uses (basically, uses that aren't loads or
9667 // stores), because arm allows folding a scale into many arithmetic
9668 // operations. This should be made more precise and revisited later.
9669
9670 // Allow r << imm, but the imm has to be a multiple of two.
9671 if (Scale & 1) return false;
9672 return isPowerOf2_32(Scale);
9673 }
9674}
9675
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009676/// isLegalAddressingMode - Return true if the addressing mode represented
9677/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson7117a912009-03-20 22:42:55 +00009678bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +00009679 Type *Ty) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009680 EVT VT = getValueType(Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +00009681 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +00009682 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009683
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009684 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +00009685 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009686 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009687
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009688 switch (AM.Scale) {
9689 case 0: // no scale reg, must be "r+i" or "r", or "i".
9690 break;
9691 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009692 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009693 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +00009694 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009695 default:
Chris Lattner502c3f42007-04-13 06:50:55 +00009696 // ARM doesn't support any R+R*scale+imm addr modes.
9697 if (AM.BaseOffs)
9698 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009699
Bob Wilson866c1742009-04-08 17:55:28 +00009700 if (!VT.isSimple())
9701 return false;
9702
Evan Chengdc49a8d2009-08-14 20:09:37 +00009703 if (Subtarget->isThumb2())
9704 return isLegalT2ScaledAddressingMode(AM, VT);
9705
Chris Lattner9b6d69e2007-04-10 03:48:29 +00009706 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +00009707 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009708 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +00009709 case MVT::i1:
9710 case MVT::i8:
9711 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +00009712 if (Scale < 0) Scale = -Scale;
9713 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009714 return true;
9715 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +00009716 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +00009717 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009718 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009719 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +00009720 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009721 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +00009722 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009723
Owen Anderson9f944592009-08-11 20:47:22 +00009724 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009725 // Note, we allow "void" uses (basically, uses that aren't loads or
9726 // stores), because arm allows folding a scale into many arithmetic
9727 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +00009728
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009729 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +00009730 if (Scale & 1) return false;
9731 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009732 }
Evan Cheng2150b922007-03-12 23:30:29 +00009733 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009734 return true;
Evan Cheng2150b922007-03-12 23:30:29 +00009735}
9736
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009737/// isLegalICmpImmediate - Return true if the specified immediate is legal
9738/// icmp immediate, that is the target has icmp instructions which can compare
9739/// a register against the immediate without having to materialize the
9740/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +00009741bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +00009742 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009743 if (!Subtarget->isThumb())
Chandler Carruth8a102c22012-04-06 20:10:52 +00009744 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009745 if (Subtarget->isThumb2())
Chandler Carruth8a102c22012-04-06 20:10:52 +00009746 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +00009747 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +00009748 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009749}
9750
Andrew Tricka22cdb72012-07-18 18:34:27 +00009751/// isLegalAddImmediate - Return true if the specified immediate is a legal add
9752/// *or sub* immediate, that is the target has add or sub instructions which can
9753/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +00009754/// immediate into a register.
9755bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +00009756 // Same encoding for add/sub, just flip the sign.
9757 int64_t AbsImm = llvm::abs64(Imm);
9758 if (!Subtarget->isThumb())
9759 return ARM_AM::getSOImmVal(AbsImm) != -1;
9760 if (Subtarget->isThumb2())
9761 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
9762 // Thumb1 only has 8-bit unsigned immediate.
9763 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +00009764}
9765
Owen Anderson53aa7a92009-08-10 22:56:29 +00009766static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +00009767 bool isSEXTLoad, SDValue &Base,
9768 SDValue &Offset, bool &isInc,
9769 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +00009770 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9771 return false;
9772
Owen Anderson9f944592009-08-11 20:47:22 +00009773 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +00009774 // AddressingMode 3
9775 Base = Ptr->getOperand(0);
9776 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00009777 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +00009778 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +00009779 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +00009780 isInc = false;
9781 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9782 return true;
9783 }
9784 }
9785 isInc = (Ptr->getOpcode() == ISD::ADD);
9786 Offset = Ptr->getOperand(1);
9787 return true;
Owen Anderson9f944592009-08-11 20:47:22 +00009788 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +00009789 // AddressingMode 2
9790 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00009791 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +00009792 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +00009793 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +00009794 isInc = false;
9795 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9796 Base = Ptr->getOperand(0);
9797 return true;
9798 }
9799 }
9800
9801 if (Ptr->getOpcode() == ISD::ADD) {
9802 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +00009803 ARM_AM::ShiftOpc ShOpcVal=
9804 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +00009805 if (ShOpcVal != ARM_AM::no_shift) {
9806 Base = Ptr->getOperand(1);
9807 Offset = Ptr->getOperand(0);
9808 } else {
9809 Base = Ptr->getOperand(0);
9810 Offset = Ptr->getOperand(1);
9811 }
9812 return true;
9813 }
9814
9815 isInc = (Ptr->getOpcode() == ISD::ADD);
9816 Base = Ptr->getOperand(0);
9817 Offset = Ptr->getOperand(1);
9818 return true;
9819 }
9820
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00009821 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +00009822 return false;
9823}
9824
Owen Anderson53aa7a92009-08-10 22:56:29 +00009825static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +00009826 bool isSEXTLoad, SDValue &Base,
9827 SDValue &Offset, bool &isInc,
9828 SelectionDAG &DAG) {
9829 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9830 return false;
9831
9832 Base = Ptr->getOperand(0);
9833 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
9834 int RHSC = (int)RHS->getZExtValue();
9835 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
9836 assert(Ptr->getOpcode() == ISD::ADD);
9837 isInc = false;
9838 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9839 return true;
9840 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
9841 isInc = Ptr->getOpcode() == ISD::ADD;
9842 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
9843 return true;
9844 }
9845 }
9846
9847 return false;
9848}
9849
Evan Cheng10043e22007-01-19 07:51:42 +00009850/// getPreIndexedAddressParts - returns true by value, base pointer and
9851/// offset pointer and addressing mode by reference if the node's address
9852/// can be legally represented as pre-indexed load / store address.
9853bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009854ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
9855 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +00009856 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +00009857 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +00009858 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +00009859 return false;
9860
Owen Anderson53aa7a92009-08-10 22:56:29 +00009861 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009862 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +00009863 bool isSEXTLoad = false;
9864 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
9865 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00009866 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +00009867 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9868 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
9869 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +00009870 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +00009871 } else
9872 return false;
9873
9874 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +00009875 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +00009876 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +00009877 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
9878 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +00009879 else
Evan Cheng84c6cda2009-07-02 07:28:31 +00009880 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +00009881 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +00009882 if (!isLegal)
9883 return false;
9884
9885 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
9886 return true;
Evan Cheng10043e22007-01-19 07:51:42 +00009887}
9888
9889/// getPostIndexedAddressParts - returns true by value, base pointer and
9890/// offset pointer and addressing mode by reference if this node can be
9891/// combined with a load / store to form a post-indexed load / store.
9892bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009893 SDValue &Base,
9894 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +00009895 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +00009896 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +00009897 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +00009898 return false;
9899
Owen Anderson53aa7a92009-08-10 22:56:29 +00009900 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009901 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +00009902 bool isSEXTLoad = false;
9903 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +00009904 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +00009905 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +00009906 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9907 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +00009908 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +00009909 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +00009910 } else
9911 return false;
9912
9913 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +00009914 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +00009915 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +00009916 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +00009917 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +00009918 else
Evan Cheng84c6cda2009-07-02 07:28:31 +00009919 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
9920 isInc, DAG);
9921 if (!isLegal)
9922 return false;
9923
Evan Chengf19384d2010-05-18 21:31:17 +00009924 if (Ptr != Base) {
9925 // Swap base ptr and offset to catch more post-index load / store when
9926 // it's legal. In Thumb2 mode, offset must be an immediate.
9927 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
9928 !Subtarget->isThumb2())
9929 std::swap(Base, Offset);
9930
9931 // Post-indexed load / store update the base pointer.
9932 if (Ptr != Base)
9933 return false;
9934 }
9935
Evan Cheng84c6cda2009-07-02 07:28:31 +00009936 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
9937 return true;
Evan Cheng10043e22007-01-19 07:51:42 +00009938}
9939
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009940void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Bob Wilson7117a912009-03-20 22:42:55 +00009941 APInt &KnownZero,
Dan Gohmanf990faf2008-02-13 00:35:47 +00009942 APInt &KnownOne,
Dan Gohman309d3d52007-06-22 14:59:07 +00009943 const SelectionDAG &DAG,
Evan Cheng10043e22007-01-19 07:51:42 +00009944 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +00009945 unsigned BitWidth = KnownOne.getBitWidth();
9946 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00009947 switch (Op.getOpcode()) {
9948 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +00009949 case ARMISD::ADDC:
9950 case ARMISD::ADDE:
9951 case ARMISD::SUBC:
9952 case ARMISD::SUBE:
9953 // These nodes' second result is a boolean
9954 if (Op.getResNo() == 0)
9955 break;
9956 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
9957 break;
Evan Cheng10043e22007-01-19 07:51:42 +00009958 case ARMISD::CMOV: {
9959 // Bits are known zero/one if known on the LHS and RHS.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00009960 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +00009961 if (KnownZero == 0 && KnownOne == 0) return;
9962
Dan Gohmanf990faf2008-02-13 00:35:47 +00009963 APInt KnownZeroRHS, KnownOneRHS;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00009964 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +00009965 KnownZero &= KnownZeroRHS;
9966 KnownOne &= KnownOneRHS;
9967 return;
9968 }
Tim Northover01b4aa92014-04-03 15:10:35 +00009969 case ISD::INTRINSIC_W_CHAIN: {
9970 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
9971 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
9972 switch (IntID) {
9973 default: return;
9974 case Intrinsic::arm_ldaex:
9975 case Intrinsic::arm_ldrex: {
9976 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
9977 unsigned MemBits = VT.getScalarType().getSizeInBits();
9978 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
9979 return;
9980 }
9981 }
9982 }
Evan Cheng10043e22007-01-19 07:51:42 +00009983 }
9984}
9985
9986//===----------------------------------------------------------------------===//
9987// ARM Inline Assembly Support
9988//===----------------------------------------------------------------------===//
9989
Evan Cheng078b0b02011-01-08 01:24:27 +00009990bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
9991 // Looking for "rev" which is V6+.
9992 if (!Subtarget->hasV6Ops())
9993 return false;
9994
9995 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9996 std::string AsmStr = IA->getAsmString();
9997 SmallVector<StringRef, 4> AsmPieces;
9998 SplitString(AsmStr, AsmPieces, ";\n");
9999
10000 switch (AsmPieces.size()) {
10001 default: return false;
10002 case 1:
10003 AsmStr = AsmPieces[0];
10004 AsmPieces.clear();
10005 SplitString(AsmStr, AsmPieces, " \t,");
10006
10007 // rev $0, $1
10008 if (AsmPieces.size() == 3 &&
10009 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
10010 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000010011 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000010012 if (Ty && Ty->getBitWidth() == 32)
10013 return IntrinsicLowering::LowerToByteSwap(CI);
10014 }
10015 break;
10016 }
10017
10018 return false;
10019}
10020
Evan Cheng10043e22007-01-19 07:51:42 +000010021/// getConstraintType - Given a constraint letter, return the type of
10022/// constraint it is for this target.
10023ARMTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +000010024ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
10025 if (Constraint.size() == 1) {
10026 switch (Constraint[0]) {
10027 default: break;
10028 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000010029 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000010030 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000010031 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000010032 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000010033 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000010034 // An address with a single base register. Due to the way we
10035 // currently handle addresses it is the same as an 'r' memory constraint.
10036 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000010037 }
Eric Christophere256cd02011-06-21 22:10:57 +000010038 } else if (Constraint.size() == 2) {
10039 switch (Constraint[0]) {
10040 default: break;
10041 // All 'U+' constraints are addresses.
10042 case 'U': return C_Memory;
10043 }
Evan Cheng10043e22007-01-19 07:51:42 +000010044 }
Chris Lattnerd6855142007-03-25 02:14:49 +000010045 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000010046}
10047
John Thompsone8360b72010-10-29 17:29:13 +000010048/// Examine constraint type and operand type and determine a weight value.
10049/// This object must already have been set up with the operand type
10050/// and the current alternative constraint selected.
10051TargetLowering::ConstraintWeight
10052ARMTargetLowering::getSingleConstraintMatchWeight(
10053 AsmOperandInfo &info, const char *constraint) const {
10054 ConstraintWeight weight = CW_Invalid;
10055 Value *CallOperandVal = info.CallOperandVal;
10056 // If we don't have a value, we can't do a match,
10057 // but allow it at the lowest weight.
10058 if (CallOperandVal == NULL)
10059 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000010060 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000010061 // Look at the constraint type.
10062 switch (*constraint) {
10063 default:
10064 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10065 break;
10066 case 'l':
10067 if (type->isIntegerTy()) {
10068 if (Subtarget->isThumb())
10069 weight = CW_SpecificReg;
10070 else
10071 weight = CW_Register;
10072 }
10073 break;
10074 case 'w':
10075 if (type->isFloatingPointTy())
10076 weight = CW_Register;
10077 break;
10078 }
10079 return weight;
10080}
10081
Eric Christophercf2007c2011-06-30 23:50:52 +000010082typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
10083RCPair
Evan Cheng10043e22007-01-19 07:51:42 +000010084ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +000010085 MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000010086 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010087 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000010088 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000010089 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010090 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010091 return RCPair(0U, &ARM::tGPRRegClass);
10092 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000010093 case 'h': // High regs or no regs.
10094 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010095 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000010096 break;
Chris Lattner6223e832007-04-02 17:24:08 +000010097 case 'r':
Craig Topperc7242e02012-04-20 07:30:17 +000010098 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010099 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000010100 if (VT == MVT::Other)
10101 break;
Owen Anderson9f944592009-08-11 20:47:22 +000010102 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010103 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000010104 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010105 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000010106 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010107 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010108 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010109 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000010110 if (VT == MVT::Other)
10111 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010112 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010113 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010114 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010115 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010116 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010117 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010118 break;
Eric Christopherc011d312011-07-01 00:30:46 +000010119 case 't':
10120 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010121 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000010122 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010123 }
10124 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000010125 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000010126 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000010127
Evan Cheng10043e22007-01-19 07:51:42 +000010128 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
10129}
10130
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010131/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10132/// vector. If it is invalid, don't add anything to Ops.
10133void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000010134 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010135 std::vector<SDValue>&Ops,
10136 SelectionDAG &DAG) const {
10137 SDValue Result(0, 0);
10138
Eric Christopherde9399b2011-06-02 23:16:42 +000010139 // Currently only support length 1 constraints.
10140 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000010141
Eric Christopherde9399b2011-06-02 23:16:42 +000010142 char ConstraintLetter = Constraint[0];
10143 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010144 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000010145 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010146 case 'I': case 'J': case 'K': case 'L':
10147 case 'M': case 'N': case 'O':
10148 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10149 if (!C)
10150 return;
10151
10152 int64_t CVal64 = C->getSExtValue();
10153 int CVal = (int) CVal64;
10154 // None of these constraints allow values larger than 32 bits. Check
10155 // that the value fits in an int.
10156 if (CVal != CVal64)
10157 return;
10158
Eric Christopherde9399b2011-06-02 23:16:42 +000010159 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000010160 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000010161 // Constant suitable for movw, must be between 0 and
10162 // 65535.
10163 if (Subtarget->hasV6T2Ops())
10164 if (CVal >= 0 && CVal <= 65535)
10165 break;
10166 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010167 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000010168 if (Subtarget->isThumb1Only()) {
10169 // This must be a constant between 0 and 255, for ADD
10170 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010171 if (CVal >= 0 && CVal <= 255)
10172 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010173 } else if (Subtarget->isThumb2()) {
10174 // A constant that can be used as an immediate value in a
10175 // data-processing instruction.
10176 if (ARM_AM::getT2SOImmVal(CVal) != -1)
10177 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010178 } else {
10179 // A constant that can be used as an immediate value in a
10180 // data-processing instruction.
10181 if (ARM_AM::getSOImmVal(CVal) != -1)
10182 break;
10183 }
10184 return;
10185
10186 case 'J':
David Goodwin22c2fba2009-07-08 23:10:31 +000010187 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010188 // This must be a constant between -255 and -1, for negated ADD
10189 // immediates. This can be used in GCC with an "n" modifier that
10190 // prints the negated value, for use with SUB instructions. It is
10191 // not useful otherwise but is implemented for compatibility.
10192 if (CVal >= -255 && CVal <= -1)
10193 break;
10194 } else {
10195 // This must be a constant between -4095 and 4095. It is not clear
10196 // what this constraint is intended for. Implemented for
10197 // compatibility with GCC.
10198 if (CVal >= -4095 && CVal <= 4095)
10199 break;
10200 }
10201 return;
10202
10203 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000010204 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010205 // A 32-bit value where only one byte has a nonzero value. Exclude
10206 // zero to match GCC. This constraint is used by GCC internally for
10207 // constants that can be loaded with a move/shift combination.
10208 // It is not useful otherwise but is implemented for compatibility.
10209 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
10210 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010211 } else if (Subtarget->isThumb2()) {
10212 // A constant whose bitwise inverse can be used as an immediate
10213 // value in a data-processing instruction. This can be used in GCC
10214 // with a "B" modifier that prints the inverted value, for use with
10215 // BIC and MVN instructions. It is not useful otherwise but is
10216 // implemented for compatibility.
10217 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
10218 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010219 } else {
10220 // A constant whose bitwise inverse can be used as an immediate
10221 // value in a data-processing instruction. This can be used in GCC
10222 // with a "B" modifier that prints the inverted value, for use with
10223 // BIC and MVN instructions. It is not useful otherwise but is
10224 // implemented for compatibility.
10225 if (ARM_AM::getSOImmVal(~CVal) != -1)
10226 break;
10227 }
10228 return;
10229
10230 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000010231 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010232 // This must be a constant between -7 and 7,
10233 // for 3-operand ADD/SUB immediate instructions.
10234 if (CVal >= -7 && CVal < 7)
10235 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010236 } else if (Subtarget->isThumb2()) {
10237 // A constant whose negation can be used as an immediate value in a
10238 // data-processing instruction. This can be used in GCC with an "n"
10239 // modifier that prints the negated value, for use with SUB
10240 // instructions. It is not useful otherwise but is implemented for
10241 // compatibility.
10242 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
10243 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010244 } else {
10245 // A constant whose negation can be used as an immediate value in a
10246 // data-processing instruction. This can be used in GCC with an "n"
10247 // modifier that prints the negated value, for use with SUB
10248 // instructions. It is not useful otherwise but is implemented for
10249 // compatibility.
10250 if (ARM_AM::getSOImmVal(-CVal) != -1)
10251 break;
10252 }
10253 return;
10254
10255 case 'M':
David Goodwin22c2fba2009-07-08 23:10:31 +000010256 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010257 // This must be a multiple of 4 between 0 and 1020, for
10258 // ADD sp + immediate.
10259 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
10260 break;
10261 } else {
10262 // A power of two or a constant between 0 and 32. This is used in
10263 // GCC for the shift amount on shifted register operands, but it is
10264 // useful in general for any shift amounts.
10265 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
10266 break;
10267 }
10268 return;
10269
10270 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000010271 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010272 // This must be a constant between 0 and 31, for shift amounts.
10273 if (CVal >= 0 && CVal <= 31)
10274 break;
10275 }
10276 return;
10277
10278 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000010279 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010280 // This must be a multiple of 4 between -508 and 508, for
10281 // ADD/SUB sp = sp + immediate.
10282 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
10283 break;
10284 }
10285 return;
10286 }
10287 Result = DAG.getTargetConstant(CVal, Op.getValueType());
10288 break;
10289 }
10290
10291 if (Result.getNode()) {
10292 Ops.push_back(Result);
10293 return;
10294 }
Dale Johannesence97d552010-06-25 21:55:36 +000010295 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010296}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010297
Renato Golin87610692013-07-16 09:32:17 +000010298SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
10299 assert(Subtarget->isTargetAEABI() && "Register-based DivRem lowering only");
10300 unsigned Opcode = Op->getOpcode();
10301 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
10302 "Invalid opcode for Div/Rem lowering");
10303 bool isSigned = (Opcode == ISD::SDIVREM);
10304 EVT VT = Op->getValueType(0);
10305 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
10306
10307 RTLIB::Libcall LC;
10308 switch (VT.getSimpleVT().SimpleTy) {
10309 default: llvm_unreachable("Unexpected request for libcall!");
10310 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
10311 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
10312 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
10313 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
10314 }
10315
10316 SDValue InChain = DAG.getEntryNode();
10317
10318 TargetLowering::ArgListTy Args;
10319 TargetLowering::ArgListEntry Entry;
10320 for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) {
10321 EVT ArgVT = Op->getOperand(i).getValueType();
10322 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
10323 Entry.Node = Op->getOperand(i);
10324 Entry.Ty = ArgTy;
10325 Entry.isSExt = isSigned;
10326 Entry.isZExt = !isSigned;
10327 Args.push_back(Entry);
10328 }
10329
10330 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
10331 getPointerTy());
10332
10333 Type *RetTy = (Type*)StructType::get(Ty, Ty, NULL);
10334
10335 SDLoc dl(Op);
10336 TargetLowering::
10337 CallLoweringInfo CLI(InChain, RetTy, isSigned, !isSigned, false, true,
10338 0, getLibcallCallingConv(LC), /*isTailCall=*/false,
10339 /*doesNotReturn=*/false, /*isReturnValueUsed=*/true,
10340 Callee, Args, DAG, dl);
10341 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
10342
10343 return CallInfo.first;
10344}
10345
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010346bool
10347ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
10348 // The ARM target isn't yet aware of offsets.
10349 return false;
10350}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010351
Jim Grosbach11013ed2010-07-16 23:05:05 +000010352bool ARM::isBitFieldInvertedMask(unsigned v) {
10353 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010354 return false;
10355
Jim Grosbach11013ed2010-07-16 23:05:05 +000010356 // there can be 1's on either or both "outsides", all the "inside"
10357 // bits must be 0's
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010358 unsigned TO = CountTrailingOnes_32(v);
10359 unsigned LO = CountLeadingOnes_32(v);
10360 v = (v >> TO) << TO;
10361 v = (v << LO) >> LO;
10362 return v == 0;
Jim Grosbach11013ed2010-07-16 23:05:05 +000010363}
10364
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010365/// isFPImmLegal - Returns true if the target can instruction select the
10366/// specified FP immediate natively. If false, the legalizer will
10367/// materialize the FP immediate as a load from a constant pool.
10368bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
10369 if (!Subtarget->hasVFP3())
10370 return false;
10371 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000010372 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010373 if (VT == MVT::f64)
Jim Grosbachefc761a2011-09-30 00:50:06 +000010374 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010375 return false;
10376}
Bob Wilson5549d492010-09-21 17:56:22 +000010377
Wesley Peck527da1b2010-11-23 03:31:01 +000010378/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000010379/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
10380/// specified in the intrinsic calls.
10381bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
10382 const CallInst &I,
10383 unsigned Intrinsic) const {
10384 switch (Intrinsic) {
10385 case Intrinsic::arm_neon_vld1:
10386 case Intrinsic::arm_neon_vld2:
10387 case Intrinsic::arm_neon_vld3:
10388 case Intrinsic::arm_neon_vld4:
10389 case Intrinsic::arm_neon_vld2lane:
10390 case Intrinsic::arm_neon_vld3lane:
10391 case Intrinsic::arm_neon_vld4lane: {
10392 Info.opc = ISD::INTRINSIC_W_CHAIN;
10393 // Conservatively set memVT to the entire set of vectors loaded.
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010394 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010395 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10396 Info.ptrVal = I.getArgOperand(0);
10397 Info.offset = 0;
10398 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10399 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10400 Info.vol = false; // volatile loads with NEON intrinsics not supported
10401 Info.readMem = true;
10402 Info.writeMem = false;
10403 return true;
10404 }
10405 case Intrinsic::arm_neon_vst1:
10406 case Intrinsic::arm_neon_vst2:
10407 case Intrinsic::arm_neon_vst3:
10408 case Intrinsic::arm_neon_vst4:
10409 case Intrinsic::arm_neon_vst2lane:
10410 case Intrinsic::arm_neon_vst3lane:
10411 case Intrinsic::arm_neon_vst4lane: {
10412 Info.opc = ISD::INTRINSIC_VOID;
10413 // Conservatively set memVT to the entire set of vectors stored.
10414 unsigned NumElts = 0;
10415 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000010416 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000010417 if (!ArgTy->isVectorTy())
10418 break;
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010419 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010420 }
10421 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10422 Info.ptrVal = I.getArgOperand(0);
10423 Info.offset = 0;
10424 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10425 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10426 Info.vol = false; // volatile stores with NEON intrinsics not supported
10427 Info.readMem = false;
10428 Info.writeMem = true;
10429 return true;
10430 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010431 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000010432 case Intrinsic::arm_ldrex: {
10433 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
10434 Info.opc = ISD::INTRINSIC_W_CHAIN;
10435 Info.memVT = MVT::getVT(PtrTy->getElementType());
10436 Info.ptrVal = I.getArgOperand(0);
10437 Info.offset = 0;
10438 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10439 Info.vol = true;
10440 Info.readMem = true;
10441 Info.writeMem = false;
10442 return true;
10443 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010444 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000010445 case Intrinsic::arm_strex: {
10446 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
10447 Info.opc = ISD::INTRINSIC_W_CHAIN;
10448 Info.memVT = MVT::getVT(PtrTy->getElementType());
10449 Info.ptrVal = I.getArgOperand(1);
10450 Info.offset = 0;
10451 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10452 Info.vol = true;
10453 Info.readMem = false;
10454 Info.writeMem = true;
10455 return true;
10456 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010457 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010458 case Intrinsic::arm_strexd: {
10459 Info.opc = ISD::INTRINSIC_W_CHAIN;
10460 Info.memVT = MVT::i64;
10461 Info.ptrVal = I.getArgOperand(2);
10462 Info.offset = 0;
10463 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000010464 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010465 Info.readMem = false;
10466 Info.writeMem = true;
10467 return true;
10468 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010469 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010470 case Intrinsic::arm_ldrexd: {
10471 Info.opc = ISD::INTRINSIC_W_CHAIN;
10472 Info.memVT = MVT::i64;
10473 Info.ptrVal = I.getArgOperand(0);
10474 Info.offset = 0;
10475 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000010476 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010477 Info.readMem = true;
10478 Info.writeMem = false;
10479 return true;
10480 }
Bob Wilson5549d492010-09-21 17:56:22 +000010481 default:
10482 break;
10483 }
10484
10485 return false;
10486}
Juergen Ributzka659ce002014-01-28 01:20:14 +000010487
10488/// \brief Returns true if it is beneficial to convert a load of a constant
10489/// to just the constant itself.
10490bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
10491 Type *Ty) const {
10492 assert(Ty->isIntegerTy());
10493
10494 unsigned Bits = Ty->getPrimitiveSizeInBits();
10495 if (Bits == 0 || Bits > 32)
10496 return false;
10497 return true;
10498}
Tim Northover037f26f22014-04-17 18:22:47 +000010499
10500bool ARMTargetLowering::shouldExpandAtomicInIR(Instruction *Inst) const {
10501 // Loads and stores less than 64-bits are already atomic; ones above that
10502 // are doomed anyway, so defer to the default libcall and blame the OS when
10503 // things go wrong:
10504 if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
10505 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() == 64;
10506 else if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
10507 return LI->getType()->getPrimitiveSizeInBits() == 64;
10508
10509 // For the real atomic operations, we have ldrex/strex up to 64 bits.
10510 return Inst->getType()->getPrimitiveSizeInBits() <= 64;
10511}
10512
10513Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
10514 AtomicOrdering Ord) const {
10515 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
10516 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
10517 bool IsAcquire =
10518 Ord == Acquire || Ord == AcquireRelease || Ord == SequentiallyConsistent;
10519
10520 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
10521 // intrinsic must return {i32, i32} and we have to recombine them into a
10522 // single i64 here.
10523 if (ValTy->getPrimitiveSizeInBits() == 64) {
10524 Intrinsic::ID Int =
10525 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
10526 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
10527
10528 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
10529 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
10530
10531 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
10532 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
10533 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
10534 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
10535 return Builder.CreateOr(
10536 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
10537 }
10538
10539 Type *Tys[] = { Addr->getType() };
10540 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
10541 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
10542
10543 return Builder.CreateTruncOrBitCast(
10544 Builder.CreateCall(Ldrex, Addr),
10545 cast<PointerType>(Addr->getType())->getElementType());
10546}
10547
10548Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
10549 Value *Addr,
10550 AtomicOrdering Ord) const {
10551 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
10552 bool IsRelease =
10553 Ord == Release || Ord == AcquireRelease || Ord == SequentiallyConsistent;
10554
10555 // Since the intrinsics must have legal type, the i64 intrinsics take two
10556 // parameters: "i32, i32". We must marshal Val into the appropriate form
10557 // before the call.
10558 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
10559 Intrinsic::ID Int =
10560 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
10561 Function *Strex = Intrinsic::getDeclaration(M, Int);
10562 Type *Int32Ty = Type::getInt32Ty(M->getContext());
10563
10564 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
10565 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
10566 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
10567 return Builder.CreateCall3(Strex, Lo, Hi, Addr);
10568 }
10569
10570 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
10571 Type *Tys[] = { Addr->getType() };
10572 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
10573
10574 return Builder.CreateCall2(
10575 Strex, Builder.CreateZExtOrBitCast(
10576 Val, Strex->getFunctionType()->getParamType(0)),
10577 Addr);
10578}