blob: 339c85886aea5f55861eb8a84d382b6565bc194f [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Dale Johannesen51e28e62010-06-03 21:09:53 +000015#define DEBUG_TYPE "arm-isel"
Evan Chenga8e29892007-01-19 07:51:42 +000016#include "ARM.h"
17#include "ARMAddressingModes.h"
Eric Christopher6f2ccef2010-09-10 22:42:06 +000018#include "ARMCallingConv.h"
Evan Chenga8e29892007-01-19 07:51:42 +000019#include "ARMConstantPoolValue.h"
20#include "ARMISelLowering.h"
21#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000022#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000023#include "ARMRegisterInfo.h"
24#include "ARMSubtarget.h"
25#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000026#include "ARMTargetObjectFile.h"
Evan Chenga8e29892007-01-19 07:51:42 +000027#include "llvm/CallingConv.h"
28#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000029#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000030#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000031#include "llvm/Instruction.h"
Bob Wilson65ffec42010-09-21 17:56:22 +000032#include "llvm/Instructions.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000033#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000034#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000035#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng55d42002011-01-08 01:24:27 +000036#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Chenga8e29892007-01-19 07:51:42 +000037#include "llvm/CodeGen/MachineBasicBlock.h"
38#include "llvm/CodeGen/MachineFrameInfo.h"
39#include "llvm/CodeGen/MachineFunction.h"
40#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000041#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000042#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000043#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000044#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000045#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000046#include "llvm/ADT/VectorExtras.h"
Evan Cheng55d42002011-01-08 01:24:27 +000047#include "llvm/ADT/StringExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000048#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000049#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000050#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000051#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000052#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000053#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000054using namespace llvm;
55
Dale Johannesen51e28e62010-06-03 21:09:53 +000056STATISTIC(NumTailCalls, "Number of tail calls");
Evan Chengfc8475b2011-01-19 02:16:49 +000057STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Dale Johannesen51e28e62010-06-03 21:09:53 +000058
Bob Wilson703af3a2010-08-13 22:43:33 +000059// This option should go away when tail calls fully work.
60static cl::opt<bool>
61EnableARMTailCalls("arm-tail-calls", cl::Hidden,
62 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
63 cl::init(false));
64
Eric Christopher836c6242010-12-15 23:47:29 +000065cl::opt<bool>
Jim Grosbache7b52522010-04-14 22:28:31 +000066EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000067 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000068 cl::init(false));
69
Evan Cheng46df4eb2010-06-16 07:35:02 +000070static cl::opt<bool>
71ARMInterworking("arm-interworking", cl::Hidden,
72 cl::desc("Enable / disable ARM interworking (for debugging only)"),
73 cl::init(true));
74
Cameron Zwaricha86686e2011-06-10 20:59:24 +000075namespace llvm {
76 class ARMCCState : public CCState {
77 public:
78 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
79 const TargetMachine &TM, SmallVector<CCValAssign, 16> &locs,
80 LLVMContext &C, ParmContext PC)
81 : CCState(CC, isVarArg, MF, TM, locs, C) {
82 assert(((PC == Call) || (PC == Prologue)) &&
83 "ARMCCState users must specify whether their context is call"
84 "or prologue generation.");
85 CallOrPrologue = PC;
86 }
87 };
88}
89
Stuart Hastingsc7315872011-04-20 16:47:52 +000090// The APCS parameter registers.
91static const unsigned GPRArgRegs[] = {
92 ARM::R0, ARM::R1, ARM::R2, ARM::R3
93};
94
Owen Andersone50ed302009-08-10 22:56:29 +000095void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
96 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000097 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000098 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000099 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
100 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000101
Owen Anderson70671842009-08-10 20:18:46 +0000102 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000103 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000104 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000105 }
106
Owen Andersone50ed302009-08-10 22:56:29 +0000107 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000108 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +0000109 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +0000110 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +0000111 if (ElemTy != MVT::i32) {
112 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
113 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
114 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
115 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
116 }
Owen Anderson70671842009-08-10 20:18:46 +0000117 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
118 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000119 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +0000120 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000121 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
122 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000123 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000124 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
125 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
126 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000127 setLoadExtAction(ISD::SEXTLOAD, VT.getSimpleVT(), Expand);
128 setLoadExtAction(ISD::ZEXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson24645a12010-11-01 18:31:39 +0000129 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
130 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
131 setTruncStoreAction(VT.getSimpleVT(),
132 (MVT::SimpleValueType)InnerVT, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000133 }
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000134 setLoadExtAction(ISD::EXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000135
136 // Promote all bit-wise operations.
137 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000138 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000139 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
140 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000141 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000142 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000143 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000144 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000145 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000146 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000147 }
Bob Wilson16330762009-09-16 00:17:28 +0000148
149 // Neon does not support vector divide/remainder operations.
150 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
151 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
152 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
153 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
154 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
155 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000156}
157
Owen Andersone50ed302009-08-10 22:56:29 +0000158void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000159 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000160 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000161}
162
Owen Andersone50ed302009-08-10 22:56:29 +0000163void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000164 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000166}
167
Chris Lattnerf0144122009-07-28 03:13:23 +0000168static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
169 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000170 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000171
Chris Lattner80ec2792009-08-02 00:34:36 +0000172 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000173}
174
Evan Chenga8e29892007-01-19 07:51:42 +0000175ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000176 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000177 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000178 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000179 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000180
Evan Chengb1df8f22007-04-27 08:15:43 +0000181 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000182 // Uses VFP for Thumb libfuncs if available.
183 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
184 // Single-precision floating-point arithmetic.
185 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
186 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
187 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
188 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000189
Evan Chengb1df8f22007-04-27 08:15:43 +0000190 // Double-precision floating-point arithmetic.
191 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
192 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
193 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
194 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000195
Evan Chengb1df8f22007-04-27 08:15:43 +0000196 // Single-precision comparisons.
197 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
198 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
199 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
200 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
201 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
202 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
203 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
204 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000205
Evan Chengb1df8f22007-04-27 08:15:43 +0000206 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000214
Evan Chengb1df8f22007-04-27 08:15:43 +0000215 // Double-precision comparisons.
216 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
217 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
218 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
219 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
220 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
221 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
222 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
223 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000224
Evan Chengb1df8f22007-04-27 08:15:43 +0000225 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
226 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
227 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
228 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
229 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
230 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
231 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
232 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000233
Evan Chengb1df8f22007-04-27 08:15:43 +0000234 // Floating-point to integer conversions.
235 // i64 conversions are done via library routines even when generating VFP
236 // instructions, so use the same ones.
237 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
238 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
239 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
240 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000241
Evan Chengb1df8f22007-04-27 08:15:43 +0000242 // Conversions between floating types.
243 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
244 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
245
246 // Integer to floating-point conversions.
247 // i64 conversions are done via library routines even when generating VFP
248 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000249 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
250 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000251 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
252 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
253 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
254 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
255 }
Evan Chenga8e29892007-01-19 07:51:42 +0000256 }
257
Bob Wilson2f954612009-05-22 17:38:41 +0000258 // These libcalls are not available in 32-bit.
259 setLibcallName(RTLIB::SHL_I128, 0);
260 setLibcallName(RTLIB::SRL_I128, 0);
261 setLibcallName(RTLIB::SRA_I128, 0);
262
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000263 if (Subtarget->isAAPCS_ABI()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000264 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000265 // RTABI chapter 4.1.2, Table 2
266 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
267 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
268 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
269 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
270 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
271 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
272 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
273 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
274
275 // Double-precision floating-point comparison helper functions
276 // RTABI chapter 4.1.2, Table 3
277 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
278 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
279 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
280 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
281 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
282 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
283 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
284 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
285 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
286 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
287 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
288 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
289 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
290 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
291 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
292 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
293 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
294 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
295 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
296 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
297 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
298 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
299 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
300 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
301
302 // Single-precision floating-point arithmetic helper functions
303 // RTABI chapter 4.1.2, Table 4
304 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
305 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
306 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
307 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
308 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
309 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
310 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
311 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
312
313 // Single-precision floating-point comparison helper functions
314 // RTABI chapter 4.1.2, Table 5
315 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
316 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
317 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
318 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
319 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
320 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
321 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
322 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
323 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
324 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
325 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
326 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
327 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
328 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
329 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
330 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
331 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
332 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
333 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
335 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
337 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
338 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
339
340 // Floating-point to integer conversions.
341 // RTABI chapter 4.1.2, Table 6
342 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
343 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
344 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
345 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
346 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
347 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
348 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
349 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
350 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
351 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
352 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
353 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
354 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
355 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
356 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
357 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
358
359 // Conversions between floating types.
360 // RTABI chapter 4.1.2, Table 7
361 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
362 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
363 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000364 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000365
366 // Integer to floating-point conversions.
367 // RTABI chapter 4.1.2, Table 8
368 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
369 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
370 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
371 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
372 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
373 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
374 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
375 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
376 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
377 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
378 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
379 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
380 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
381 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
382 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
383 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
384
385 // Long long helper functions
386 // RTABI chapter 4.2, Table 9
387 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
388 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
389 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
390 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
391 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
392 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
393 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
394 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
395 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
396 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
397 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
398 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
399
400 // Integer division functions
401 // RTABI chapter 4.3.1
402 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
403 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
404 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
405 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
406 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
407 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
408 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
409 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
410 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
411 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
412 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000413 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000414
415 // Memory operations
416 // RTABI chapter 4.3.4
417 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
418 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
419 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000420 }
421
David Goodwinf1daf7d2009-07-08 23:10:31 +0000422 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000423 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000424 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000426 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000427 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000428 if (!Subtarget->isFPOnlySP())
429 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000430
Owen Anderson825b72b2009-08-11 20:47:22 +0000431 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000432 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000433
434 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 addDRTypeForNEON(MVT::v2f32);
436 addDRTypeForNEON(MVT::v8i8);
437 addDRTypeForNEON(MVT::v4i16);
438 addDRTypeForNEON(MVT::v2i32);
439 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000440
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 addQRTypeForNEON(MVT::v4f32);
442 addQRTypeForNEON(MVT::v2f64);
443 addQRTypeForNEON(MVT::v16i8);
444 addQRTypeForNEON(MVT::v8i16);
445 addQRTypeForNEON(MVT::v4i32);
446 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000447
Bob Wilson74dc72e2009-09-15 23:55:57 +0000448 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
449 // neither Neon nor VFP support any arithmetic operations on it.
450 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
451 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
452 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
453 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
454 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
455 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
456 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
457 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
458 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
459 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
460 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
461 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
462 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
463 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
464 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
465 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
466 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
467 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
468 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
469 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
470 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
471 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
472 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
473 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
474
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000475 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
476
Bob Wilson642b3292009-09-16 00:32:15 +0000477 // Neon does not support some operations on v1i64 and v2i64 types.
478 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000479 // Custom handling for some quad-vector types to detect VMULL.
480 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
481 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
482 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000483 // Custom handling for some vector types to avoid expensive expansions
484 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
485 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
486 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
487 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000488 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
489 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000490 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
491 // a destination type that is wider than the source.
492 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
493 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000494
Bob Wilson1c3ef902011-02-07 17:43:21 +0000495 setTargetDAGCombine(ISD::INTRINSIC_VOID);
496 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000497 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
498 setTargetDAGCombine(ISD::SHL);
499 setTargetDAGCombine(ISD::SRL);
500 setTargetDAGCombine(ISD::SRA);
501 setTargetDAGCombine(ISD::SIGN_EXTEND);
502 setTargetDAGCombine(ISD::ZERO_EXTEND);
503 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000504 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000505 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000506 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000507 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
508 setTargetDAGCombine(ISD::STORE);
Bob Wilson5bafff32009-06-22 23:27:02 +0000509 }
510
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000511 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000512
513 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000514 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000515
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000516 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000518
Evan Chenga8e29892007-01-19 07:51:42 +0000519 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000520 if (!Subtarget->isThumb1Only()) {
521 for (unsigned im = (unsigned)ISD::PRE_INC;
522 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000523 setIndexedLoadAction(im, MVT::i1, Legal);
524 setIndexedLoadAction(im, MVT::i8, Legal);
525 setIndexedLoadAction(im, MVT::i16, Legal);
526 setIndexedLoadAction(im, MVT::i32, Legal);
527 setIndexedStoreAction(im, MVT::i1, Legal);
528 setIndexedStoreAction(im, MVT::i8, Legal);
529 setIndexedStoreAction(im, MVT::i16, Legal);
530 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000531 }
Evan Chenga8e29892007-01-19 07:51:42 +0000532 }
533
534 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000535 setOperationAction(ISD::MUL, MVT::i64, Expand);
536 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000537 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
539 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000540 }
Eric Christopher2cc40132011-04-19 18:49:19 +0000541 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops())
542 setOperationAction(ISD::MULHS, MVT::i32, Expand);
543
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000544 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000545 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000546 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 setOperationAction(ISD::SRL, MVT::i64, Custom);
548 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000549
550 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000551 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000552 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000554 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000555 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000556
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000557 // Only ARMv6 has BSWAP.
558 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000559 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000560
Evan Chenga8e29892007-01-19 07:51:42 +0000561 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000562 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000563 // v7M has a hardware divider
564 setOperationAction(ISD::SDIV, MVT::i32, Expand);
565 setOperationAction(ISD::UDIV, MVT::i32, Expand);
566 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000567 setOperationAction(ISD::SREM, MVT::i32, Expand);
568 setOperationAction(ISD::UREM, MVT::i32, Expand);
569 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
570 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000571
Owen Anderson825b72b2009-08-11 20:47:22 +0000572 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
573 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
574 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
575 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000576 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000577
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000578 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000579
Evan Chenga8e29892007-01-19 07:51:42 +0000580 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000581 setOperationAction(ISD::VASTART, MVT::Other, Custom);
582 setOperationAction(ISD::VAARG, MVT::Other, Expand);
583 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
584 setOperationAction(ISD::VAEND, MVT::Other, Expand);
585 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
586 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000587 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000588 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
589 setExceptionPointerRegister(ARM::R0);
590 setExceptionSelectorRegister(ARM::R1);
591
Evan Cheng3a1588a2010-04-15 22:20:34 +0000592 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000593 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
594 // the default expansion.
595 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000596 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000597 // membarrier needs custom lowering; the rest are legal and handled
598 // normally.
599 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
600 } else {
601 // Set them all for expansion, which will force libcalls.
602 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
603 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
604 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
605 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000606 setOperationAction(ISD::ATOMIC_SWAP, MVT::i8, Expand);
607 setOperationAction(ISD::ATOMIC_SWAP, MVT::i16, Expand);
608 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000609 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
610 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
611 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
612 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
613 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
614 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
615 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
616 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
617 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
618 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
619 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
620 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
621 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
622 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
623 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
624 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
625 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
626 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000627 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i8, Expand);
628 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i16, Expand);
629 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
630 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i8, Expand);
631 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i16, Expand);
632 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
633 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i8, Expand);
634 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i16, Expand);
635 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
636 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i8, Expand);
637 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i16, Expand);
638 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000639 // Since the libcalls include locking, fold in the fences
640 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000641 }
642 // 64-bit versions are always libcalls (for now)
643 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000644 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000645 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
646 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
647 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
648 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
649 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
650 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000651
Evan Cheng416941d2010-11-04 05:19:35 +0000652 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000653
Eli Friedmana2c6f452010-06-26 04:36:50 +0000654 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
655 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000656 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
657 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000658 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000659 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000660
Nate Begemand1fb5832010-08-03 21:31:55 +0000661 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000662 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
663 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000664 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000665 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
666 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000667
668 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000669 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000670 if (Subtarget->isTargetDarwin()) {
671 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
672 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000673 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000674 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000675 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000676
Owen Anderson825b72b2009-08-11 20:47:22 +0000677 setOperationAction(ISD::SETCC, MVT::i32, Expand);
678 setOperationAction(ISD::SETCC, MVT::f32, Expand);
679 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000680 setOperationAction(ISD::SELECT, MVT::i32, Custom);
681 setOperationAction(ISD::SELECT, MVT::f32, Custom);
682 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000683 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
684 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
685 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000686
Owen Anderson825b72b2009-08-11 20:47:22 +0000687 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
688 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
689 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
690 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
691 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000692
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000693 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000694 setOperationAction(ISD::FSIN, MVT::f64, Expand);
695 setOperationAction(ISD::FSIN, MVT::f32, Expand);
696 setOperationAction(ISD::FCOS, MVT::f32, Expand);
697 setOperationAction(ISD::FCOS, MVT::f64, Expand);
698 setOperationAction(ISD::FREM, MVT::f64, Expand);
699 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000700 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
702 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000703 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000704 setOperationAction(ISD::FPOW, MVT::f64, Expand);
705 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000706
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000707 // Various VFP goodness
708 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000709 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
710 if (Subtarget->hasVFP2()) {
711 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
712 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
713 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
714 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
715 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000716 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000717 if (!Subtarget->hasFP16()) {
718 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
719 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000720 }
Evan Cheng110cf482008-04-01 01:50:16 +0000721 }
Evan Chenga8e29892007-01-19 07:51:42 +0000722
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000723 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000724 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000725 setTargetDAGCombine(ISD::ADD);
726 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000727 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000728
Owen Anderson080c0922010-11-05 19:27:46 +0000729 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000730 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000731 if (Subtarget->hasNEON())
732 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000733
Evan Chenga8e29892007-01-19 07:51:42 +0000734 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000735
Evan Chengf7d87ee2010-05-21 00:43:17 +0000736 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
737 setSchedulingPreference(Sched::RegPressure);
738 else
739 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000740
Evan Cheng05219282011-01-06 06:52:41 +0000741 //// temporary - rewrite interface to use type
742 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000743
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000744 // On ARM arguments smaller than 4 bytes are extended, so all arguments
745 // are at least 4 bytes aligned.
746 setMinStackArgumentAlignment(4);
747
Evan Chengfff606d2010-09-24 19:07:23 +0000748 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000749
750 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000751}
752
Andrew Trick32cec0a2011-01-19 02:35:27 +0000753// FIXME: It might make sense to define the representative register class as the
754// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
755// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
756// SPR's representative would be DPR_VFP2. This should work well if register
757// pressure tracking were modified such that a register use would increment the
758// pressure of the register class's representative and all of it's super
759// classes' representatives transitively. We have not implemented this because
760// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000761// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000762// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000763std::pair<const TargetRegisterClass*, uint8_t>
764ARMTargetLowering::findRepresentativeClass(EVT VT) const{
765 const TargetRegisterClass *RRC = 0;
766 uint8_t Cost = 1;
767 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000768 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000769 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000770 // Use DPR as representative register class for all floating point
771 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
772 // the cost is 1 for both f32 and f64.
773 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000774 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000775 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000776 // When NEON is used for SP, only half of the register file is available
777 // because operations that define both SP and DP results will be constrained
778 // to the VFP2 class (D0-D15). We currently model this constraint prior to
779 // coalescing by double-counting the SP regs. See the FIXME above.
780 if (Subtarget->useNEONForSinglePrecisionFP())
781 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000782 break;
783 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
784 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000785 RRC = ARM::DPRRegisterClass;
786 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000787 break;
788 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000789 RRC = ARM::DPRRegisterClass;
790 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000791 break;
792 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000793 RRC = ARM::DPRRegisterClass;
794 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000795 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000796 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000797 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000798}
799
Evan Chenga8e29892007-01-19 07:51:42 +0000800const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
801 switch (Opcode) {
802 default: return 0;
803 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000804 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000805 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000806 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
807 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000808 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000809 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
810 case ARMISD::tCALL: return "ARMISD::tCALL";
811 case ARMISD::BRCOND: return "ARMISD::BRCOND";
812 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000813 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000814 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
815 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
816 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000817 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000818 case ARMISD::CMPFP: return "ARMISD::CMPFP";
819 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000820 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000821 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
822 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000823
Jim Grosbach3482c802010-01-18 19:58:49 +0000824 case ARMISD::RBIT: return "ARMISD::RBIT";
825
Bob Wilson76a312b2010-03-19 22:51:32 +0000826 case ARMISD::FTOSI: return "ARMISD::FTOSI";
827 case ARMISD::FTOUI: return "ARMISD::FTOUI";
828 case ARMISD::SITOF: return "ARMISD::SITOF";
829 case ARMISD::UITOF: return "ARMISD::UITOF";
830
Evan Chenga8e29892007-01-19 07:51:42 +0000831 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
832 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
833 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000834
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000835 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
836 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000837
Evan Chengc5942082009-10-28 06:55:03 +0000838 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
839 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000840 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000841
Dale Johannesen51e28e62010-06-03 21:09:53 +0000842 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000843
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000844 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000845
Evan Cheng86198642009-08-07 00:34:42 +0000846 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
847
Jim Grosbach3728e962009-12-10 00:11:09 +0000848 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000849 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000850
Evan Chengdfed19f2010-11-03 06:34:55 +0000851 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
852
Bob Wilson5bafff32009-06-22 23:27:02 +0000853 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000854 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000855 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000856 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
857 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000858 case ARMISD::VCGEU: return "ARMISD::VCGEU";
859 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000860 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
861 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000862 case ARMISD::VCGTU: return "ARMISD::VCGTU";
863 case ARMISD::VTST: return "ARMISD::VTST";
864
865 case ARMISD::VSHL: return "ARMISD::VSHL";
866 case ARMISD::VSHRs: return "ARMISD::VSHRs";
867 case ARMISD::VSHRu: return "ARMISD::VSHRu";
868 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
869 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
870 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
871 case ARMISD::VSHRN: return "ARMISD::VSHRN";
872 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
873 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
874 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
875 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
876 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
877 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
878 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
879 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
880 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
881 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
882 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
883 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
884 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
885 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000886 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000887 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000888 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000889 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000890 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000891 case ARMISD::VREV64: return "ARMISD::VREV64";
892 case ARMISD::VREV32: return "ARMISD::VREV32";
893 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000894 case ARMISD::VZIP: return "ARMISD::VZIP";
895 case ARMISD::VUZP: return "ARMISD::VUZP";
896 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000897 case ARMISD::VTBL1: return "ARMISD::VTBL1";
898 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000899 case ARMISD::VMULLs: return "ARMISD::VMULLs";
900 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000901 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000902 case ARMISD::FMAX: return "ARMISD::FMAX";
903 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000904 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000905 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
906 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000907 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000908 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
909 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
910 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000911 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
912 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
913 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
914 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
915 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
916 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
917 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
918 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
919 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
920 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
921 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
922 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
923 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
924 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
925 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
926 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
927 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000928 }
929}
930
Evan Cheng06b666c2010-05-15 02:18:07 +0000931/// getRegClassFor - Return the register class that should be used for the
932/// specified value type.
933TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
934 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
935 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
936 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000937 if (Subtarget->hasNEON()) {
938 if (VT == MVT::v4i64)
939 return ARM::QQPRRegisterClass;
940 else if (VT == MVT::v8i64)
941 return ARM::QQQQPRRegisterClass;
942 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000943 return TargetLowering::getRegClassFor(VT);
944}
945
Eric Christopherab695882010-07-21 22:26:11 +0000946// Create a fast isel object.
947FastISel *
948ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
949 return ARM::createFastISel(funcInfo);
950}
951
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000952/// getMaximalGlobalOffset - Returns the maximal possible offset which can
953/// be used for loads / stores from the global.
954unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
955 return (Subtarget->isThumb1Only() ? 127 : 4095);
956}
957
Evan Cheng1cc39842010-05-20 23:26:43 +0000958Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000959 unsigned NumVals = N->getNumValues();
960 if (!NumVals)
961 return Sched::RegPressure;
962
963 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000964 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000965 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000966 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000967 if (VT.isFloatingPoint() || VT.isVector())
968 return Sched::Latency;
969 }
Evan Chengc10f5432010-05-28 23:25:23 +0000970
971 if (!N->isMachineOpcode())
972 return Sched::RegPressure;
973
974 // Load are scheduled for latency even if there instruction itinerary
975 // is not available.
976 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
977 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000978
979 if (TID.getNumDefs() == 0)
980 return Sched::RegPressure;
981 if (!Itins->isEmpty() &&
982 Itins->getOperandCycle(TID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000983 return Sched::Latency;
984
Evan Cheng1cc39842010-05-20 23:26:43 +0000985 return Sched::RegPressure;
986}
987
Evan Chenga8e29892007-01-19 07:51:42 +0000988//===----------------------------------------------------------------------===//
989// Lowering Code
990//===----------------------------------------------------------------------===//
991
Evan Chenga8e29892007-01-19 07:51:42 +0000992/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
993static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
994 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000995 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000996 case ISD::SETNE: return ARMCC::NE;
997 case ISD::SETEQ: return ARMCC::EQ;
998 case ISD::SETGT: return ARMCC::GT;
999 case ISD::SETGE: return ARMCC::GE;
1000 case ISD::SETLT: return ARMCC::LT;
1001 case ISD::SETLE: return ARMCC::LE;
1002 case ISD::SETUGT: return ARMCC::HI;
1003 case ISD::SETUGE: return ARMCC::HS;
1004 case ISD::SETULT: return ARMCC::LO;
1005 case ISD::SETULE: return ARMCC::LS;
1006 }
1007}
1008
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001009/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1010static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001011 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001012 CondCode2 = ARMCC::AL;
1013 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001014 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001015 case ISD::SETEQ:
1016 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1017 case ISD::SETGT:
1018 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1019 case ISD::SETGE:
1020 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1021 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001022 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001023 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1024 case ISD::SETO: CondCode = ARMCC::VC; break;
1025 case ISD::SETUO: CondCode = ARMCC::VS; break;
1026 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1027 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1028 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1029 case ISD::SETLT:
1030 case ISD::SETULT: CondCode = ARMCC::LT; break;
1031 case ISD::SETLE:
1032 case ISD::SETULE: CondCode = ARMCC::LE; break;
1033 case ISD::SETNE:
1034 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1035 }
Evan Chenga8e29892007-01-19 07:51:42 +00001036}
1037
Bob Wilson1f595bb2009-04-17 19:07:39 +00001038//===----------------------------------------------------------------------===//
1039// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001040//===----------------------------------------------------------------------===//
1041
1042#include "ARMGenCallingConv.inc"
1043
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001044/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1045/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001046CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001047 bool Return,
1048 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001049 switch (CC) {
1050 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001051 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001052 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001053 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001054 if (!Subtarget->isAAPCS_ABI())
1055 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1056 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1057 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1058 }
1059 // Fallthrough
1060 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001061 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001062 if (!Subtarget->isAAPCS_ABI())
1063 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1064 else if (Subtarget->hasVFP2() &&
1065 FloatABIType == FloatABI::Hard && !isVarArg)
1066 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1067 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1068 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001069 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001070 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001071 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001072 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001073 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001074 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001075 }
1076}
1077
Dan Gohman98ca4f22009-08-05 01:29:28 +00001078/// LowerCallResult - Lower the result values of a call into the
1079/// appropriate copies out of appropriate physical registers.
1080SDValue
1081ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001082 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001083 const SmallVectorImpl<ISD::InputArg> &Ins,
1084 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001085 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001086
Bob Wilson1f595bb2009-04-17 19:07:39 +00001087 // Assign locations to each value returned by this call.
1088 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001089 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1090 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001091 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001092 CCAssignFnForNode(CallConv, /* Return*/ true,
1093 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001094
1095 // Copy all of the result registers out of their specified physreg.
1096 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1097 CCValAssign VA = RVLocs[i];
1098
Bob Wilson80915242009-04-25 00:33:20 +00001099 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001100 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001101 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001102 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001103 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001104 Chain = Lo.getValue(1);
1105 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001106 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001107 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001108 InFlag);
1109 Chain = Hi.getValue(1);
1110 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001111 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001112
Owen Anderson825b72b2009-08-11 20:47:22 +00001113 if (VA.getLocVT() == MVT::v2f64) {
1114 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1115 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1116 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001117
1118 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001119 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001120 Chain = Lo.getValue(1);
1121 InFlag = Lo.getValue(2);
1122 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001123 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001124 Chain = Hi.getValue(1);
1125 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001126 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001127 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1128 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001129 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001130 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001131 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1132 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001133 Chain = Val.getValue(1);
1134 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001135 }
Bob Wilson80915242009-04-25 00:33:20 +00001136
1137 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001138 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001139 case CCValAssign::Full: break;
1140 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001141 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001142 break;
1143 }
1144
Dan Gohman98ca4f22009-08-05 01:29:28 +00001145 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001146 }
1147
Dan Gohman98ca4f22009-08-05 01:29:28 +00001148 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001149}
1150
Bob Wilsondee46d72009-04-17 20:35:10 +00001151/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001152SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001153ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1154 SDValue StackPtr, SDValue Arg,
1155 DebugLoc dl, SelectionDAG &DAG,
1156 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001157 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001158 unsigned LocMemOffset = VA.getLocMemOffset();
1159 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1160 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001161 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001162 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001163 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001164}
1165
Dan Gohman98ca4f22009-08-05 01:29:28 +00001166void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001167 SDValue Chain, SDValue &Arg,
1168 RegsToPassVector &RegsToPass,
1169 CCValAssign &VA, CCValAssign &NextVA,
1170 SDValue &StackPtr,
1171 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001172 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001173
Jim Grosbache5165492009-11-09 00:11:35 +00001174 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001175 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001176 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1177
1178 if (NextVA.isRegLoc())
1179 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1180 else {
1181 assert(NextVA.isMemLoc());
1182 if (StackPtr.getNode() == 0)
1183 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1184
Dan Gohman98ca4f22009-08-05 01:29:28 +00001185 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1186 dl, DAG, NextVA,
1187 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001188 }
1189}
1190
Dan Gohman98ca4f22009-08-05 01:29:28 +00001191/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001192/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1193/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001194SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001195ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001196 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001197 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001198 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001199 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001200 const SmallVectorImpl<ISD::InputArg> &Ins,
1201 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001202 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001203 MachineFunction &MF = DAG.getMachineFunction();
1204 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1205 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001206 // Temporarily disable tail calls so things don't break.
Evan Cheng0b655992011-05-20 17:38:48 +00001207 if (!EnableARMTailCalls)
Bob Wilson703af3a2010-08-13 22:43:33 +00001208 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001209 if (isTailCall) {
1210 // Check if it's really possible to do a tail call.
1211 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1212 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001213 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001214 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1215 // detected sibcalls.
1216 if (isTailCall) {
1217 ++NumTailCalls;
1218 IsSibCall = true;
1219 }
1220 }
Evan Chenga8e29892007-01-19 07:51:42 +00001221
Bob Wilson1f595bb2009-04-17 19:07:39 +00001222 // Analyze operands of the call, assigning locations to each operand.
1223 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001224 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1225 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001226 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001227 CCAssignFnForNode(CallConv, /* Return*/ false,
1228 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001229
Bob Wilson1f595bb2009-04-17 19:07:39 +00001230 // Get a count of how many bytes are to be pushed on the stack.
1231 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001232
Dale Johannesen51e28e62010-06-03 21:09:53 +00001233 // For tail calls, memory operands are available in our caller's stack.
1234 if (IsSibCall)
1235 NumBytes = 0;
1236
Evan Chenga8e29892007-01-19 07:51:42 +00001237 // Adjust the stack pointer for the new arguments...
1238 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001239 if (!IsSibCall)
1240 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001241
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001242 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001243
Bob Wilson5bafff32009-06-22 23:27:02 +00001244 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001245 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001246
Bob Wilson1f595bb2009-04-17 19:07:39 +00001247 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001248 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001249 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1250 i != e;
1251 ++i, ++realArgIdx) {
1252 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001253 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001254 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001255 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001256
Bob Wilson1f595bb2009-04-17 19:07:39 +00001257 // Promote the value if needed.
1258 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001259 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001260 case CCValAssign::Full: break;
1261 case CCValAssign::SExt:
1262 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1263 break;
1264 case CCValAssign::ZExt:
1265 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1266 break;
1267 case CCValAssign::AExt:
1268 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1269 break;
1270 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001271 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001272 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001273 }
1274
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001275 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001276 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001277 if (VA.getLocVT() == MVT::v2f64) {
1278 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1279 DAG.getConstant(0, MVT::i32));
1280 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1281 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001282
Dan Gohman98ca4f22009-08-05 01:29:28 +00001283 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001284 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1285
1286 VA = ArgLocs[++i]; // skip ahead to next loc
1287 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001288 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001289 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1290 } else {
1291 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001292
Dan Gohman98ca4f22009-08-05 01:29:28 +00001293 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1294 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001295 }
1296 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001297 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001298 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001299 }
1300 } else if (VA.isRegLoc()) {
1301 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001302 } else if (isByVal) {
1303 assert(VA.isMemLoc());
1304 unsigned offset = 0;
1305
1306 // True if this byval aggregate will be split between registers
1307 // and memory.
1308 if (CCInfo.isFirstByValRegValid()) {
1309 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1310 unsigned int i, j;
1311 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1312 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1313 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1314 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1315 MachinePointerInfo(),
1316 false, false, 0);
1317 MemOpChains.push_back(Load.getValue(1));
1318 RegsToPass.push_back(std::make_pair(j, Load));
1319 }
1320 offset = ARM::R4 - CCInfo.getFirstByValReg();
1321 CCInfo.clearFirstByValReg();
1322 }
1323
1324 unsigned LocMemOffset = VA.getLocMemOffset();
1325 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1326 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1327 StkPtrOff);
1328 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1329 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1330 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1331 MVT::i32);
1332 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1333 Flags.getByValAlign(),
1334 /*isVolatile=*/false,
1335 /*AlwaysInline=*/false,
1336 MachinePointerInfo(0),
1337 MachinePointerInfo(0)));
1338
1339 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001340 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001341
Dan Gohman98ca4f22009-08-05 01:29:28 +00001342 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1343 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001344 }
Evan Chenga8e29892007-01-19 07:51:42 +00001345 }
1346
1347 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001348 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001349 &MemOpChains[0], MemOpChains.size());
1350
1351 // Build a sequence of copy-to-reg nodes chained together with token chain
1352 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001353 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001354 // Tail call byval lowering might overwrite argument registers so in case of
1355 // tail call optimization the copies to registers are lowered later.
1356 if (!isTailCall)
1357 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1358 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1359 RegsToPass[i].second, InFlag);
1360 InFlag = Chain.getValue(1);
1361 }
Evan Chenga8e29892007-01-19 07:51:42 +00001362
Dale Johannesen51e28e62010-06-03 21:09:53 +00001363 // For tail calls lower the arguments to the 'real' stack slot.
1364 if (isTailCall) {
1365 // Force all the incoming stack arguments to be loaded from the stack
1366 // before any new outgoing arguments are stored to the stack, because the
1367 // outgoing stack slots may alias the incoming argument stack slots, and
1368 // the alias isn't otherwise explicit. This is slightly more conservative
1369 // than necessary, because it means that each store effectively depends
1370 // on every argument instead of just those arguments it would clobber.
1371
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001372 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001373 InFlag = SDValue();
1374 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1375 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1376 RegsToPass[i].second, InFlag);
1377 InFlag = Chain.getValue(1);
1378 }
1379 InFlag =SDValue();
1380 }
1381
Bill Wendling056292f2008-09-16 21:48:12 +00001382 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1383 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1384 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001385 bool isDirect = false;
1386 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001387 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001388 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001389
1390 if (EnableARMLongCalls) {
1391 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1392 && "long-calls with non-static relocation model!");
1393 // Handle a global address or an external symbol. If it's not one of
1394 // those, the target's already in a register, so we don't need to do
1395 // anything extra.
1396 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001397 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001398 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001399 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001400 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1401 ARMPCLabelIndex,
1402 ARMCP::CPValue, 0);
1403 // Get the address of the callee into a register
1404 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1405 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1406 Callee = DAG.getLoad(getPointerTy(), dl,
1407 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001408 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001409 false, false, 0);
1410 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1411 const char *Sym = S->getSymbol();
1412
1413 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001414 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001415 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1416 Sym, ARMPCLabelIndex, 0);
1417 // Get the address of the callee into a register
1418 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1419 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1420 Callee = DAG.getLoad(getPointerTy(), dl,
1421 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001422 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001423 false, false, 0);
1424 }
1425 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001426 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001427 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001428 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001429 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001430 getTargetMachine().getRelocationModel() != Reloc::Static;
1431 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001432 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001433 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001434 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001435 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001436 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001437 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001438 ARMPCLabelIndex,
1439 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001440 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001441 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001442 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001443 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001444 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001445 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001446 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001447 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001448 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001449 } else {
1450 // On ELF targets for PIC code, direct calls should go through the PLT
1451 unsigned OpFlags = 0;
1452 if (Subtarget->isTargetELF() &&
1453 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1454 OpFlags = ARMII::MO_PLT;
1455 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1456 }
Bill Wendling056292f2008-09-16 21:48:12 +00001457 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001458 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001459 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001460 getTargetMachine().getRelocationModel() != Reloc::Static;
1461 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001462 // tBX takes a register source operand.
1463 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001464 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001465 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001466 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001467 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001468 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001469 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001470 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001471 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001472 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001473 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001474 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001475 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001476 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001477 } else {
1478 unsigned OpFlags = 0;
1479 // On ELF targets for PIC code, direct calls should go through the PLT
1480 if (Subtarget->isTargetELF() &&
1481 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1482 OpFlags = ARMII::MO_PLT;
1483 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1484 }
Evan Chenga8e29892007-01-19 07:51:42 +00001485 }
1486
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001487 // FIXME: handle tail calls differently.
1488 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001489 if (Subtarget->isThumb()) {
1490 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001491 CallOpc = ARMISD::CALL_NOLINK;
1492 else
1493 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1494 } else {
1495 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001496 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1497 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001498 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001499
Dan Gohman475871a2008-07-27 21:46:04 +00001500 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001501 Ops.push_back(Chain);
1502 Ops.push_back(Callee);
1503
1504 // Add argument registers to the end of the list so that they are known live
1505 // into the call.
1506 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1507 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1508 RegsToPass[i].second.getValueType()));
1509
Gabor Greifba36cb52008-08-28 21:40:38 +00001510 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001511 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001512
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001513 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001514 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001515 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001516
Duncan Sands4bdcb612008-07-02 17:40:58 +00001517 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001518 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001519 InFlag = Chain.getValue(1);
1520
Chris Lattnere563bbc2008-10-11 22:08:30 +00001521 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1522 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001523 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001524 InFlag = Chain.getValue(1);
1525
Bob Wilson1f595bb2009-04-17 19:07:39 +00001526 // Handle result values, copying them out of physregs into vregs that we
1527 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001528 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1529 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001530}
1531
Stuart Hastingsf222e592011-02-28 17:17:53 +00001532/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001533/// on the stack. Remember the next parameter register to allocate,
1534/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001535/// this.
1536void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001537llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1538 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1539 assert((State->getCallOrPrologue() == Prologue ||
1540 State->getCallOrPrologue() == Call) &&
1541 "unhandled ParmContext");
1542 if ((!State->isFirstByValRegValid()) &&
1543 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1544 State->setFirstByValReg(reg);
1545 // At a call site, a byval parameter that is split between
1546 // registers and memory needs its size truncated here. In a
1547 // function prologue, such byval parameters are reassembled in
1548 // memory, and are not truncated.
1549 if (State->getCallOrPrologue() == Call) {
1550 unsigned excess = 4 * (ARM::R4 - reg);
1551 assert(size >= excess && "expected larger existing stack allocation");
1552 size -= excess;
1553 }
1554 }
1555 // Confiscate any remaining parameter registers to preclude their
1556 // assignment to subsequent parameters.
1557 while (State->AllocateReg(GPRArgRegs, 4))
1558 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001559}
1560
Dale Johannesen51e28e62010-06-03 21:09:53 +00001561/// MatchingStackOffset - Return true if the given stack call argument is
1562/// already available in the same position (relatively) of the caller's
1563/// incoming argument stack.
1564static
1565bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1566 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1567 const ARMInstrInfo *TII) {
1568 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1569 int FI = INT_MAX;
1570 if (Arg.getOpcode() == ISD::CopyFromReg) {
1571 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001572 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001573 return false;
1574 MachineInstr *Def = MRI->getVRegDef(VR);
1575 if (!Def)
1576 return false;
1577 if (!Flags.isByVal()) {
1578 if (!TII->isLoadFromStackSlot(Def, FI))
1579 return false;
1580 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001581 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001582 }
1583 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1584 if (Flags.isByVal())
1585 // ByVal argument is passed in as a pointer but it's now being
1586 // dereferenced. e.g.
1587 // define @foo(%struct.X* %A) {
1588 // tail call @bar(%struct.X* byval %A)
1589 // }
1590 return false;
1591 SDValue Ptr = Ld->getBasePtr();
1592 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1593 if (!FINode)
1594 return false;
1595 FI = FINode->getIndex();
1596 } else
1597 return false;
1598
1599 assert(FI != INT_MAX);
1600 if (!MFI->isFixedObjectIndex(FI))
1601 return false;
1602 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1603}
1604
1605/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1606/// for tail call optimization. Targets which want to do tail call
1607/// optimization should implement this function.
1608bool
1609ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1610 CallingConv::ID CalleeCC,
1611 bool isVarArg,
1612 bool isCalleeStructRet,
1613 bool isCallerStructRet,
1614 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001615 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001616 const SmallVectorImpl<ISD::InputArg> &Ins,
1617 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001618 const Function *CallerF = DAG.getMachineFunction().getFunction();
1619 CallingConv::ID CallerCC = CallerF->getCallingConv();
1620 bool CCMatch = CallerCC == CalleeCC;
1621
1622 // Look for obvious safe cases to perform tail call optimization that do not
1623 // require ABI changes. This is what gcc calls sibcall.
1624
Jim Grosbach7616b642010-06-16 23:45:49 +00001625 // Do not sibcall optimize vararg calls unless the call site is not passing
1626 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001627 if (isVarArg && !Outs.empty())
1628 return false;
1629
1630 // Also avoid sibcall optimization if either caller or callee uses struct
1631 // return semantics.
1632 if (isCalleeStructRet || isCallerStructRet)
1633 return false;
1634
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001635 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Evan Cheng0110ac62010-06-19 01:01:32 +00001636 // emitEpilogue is not ready for them.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001637 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1638 // LR. This means if we need to reload LR, it takes an extra instructions,
1639 // which outweighs the value of the tail call; but here we don't know yet
1640 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001641 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001642 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001643
1644 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1645 // but we need to make sure there are enough registers; the only valid
1646 // registers are the 4 used for parameters. We don't currently do this
1647 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001648 if (Subtarget->isThumb1Only())
1649 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001650
Dale Johannesen51e28e62010-06-03 21:09:53 +00001651 // If the calling conventions do not match, then we'd better make sure the
1652 // results are returned in the same way as what the caller expects.
1653 if (!CCMatch) {
1654 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001655 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1656 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001657 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1658
1659 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001660 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1661 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001662 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1663
1664 if (RVLocs1.size() != RVLocs2.size())
1665 return false;
1666 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1667 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1668 return false;
1669 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1670 return false;
1671 if (RVLocs1[i].isRegLoc()) {
1672 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1673 return false;
1674 } else {
1675 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1676 return false;
1677 }
1678 }
1679 }
1680
1681 // If the callee takes no arguments then go on to check the results of the
1682 // call.
1683 if (!Outs.empty()) {
1684 // Check if stack adjustment is needed. For now, do not do this if any
1685 // argument is passed on the stack.
1686 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001687 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1688 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001689 CCInfo.AnalyzeCallOperands(Outs,
1690 CCAssignFnForNode(CalleeCC, false, isVarArg));
1691 if (CCInfo.getNextStackOffset()) {
1692 MachineFunction &MF = DAG.getMachineFunction();
1693
1694 // Check if the arguments are already laid out in the right way as
1695 // the caller's fixed stack objects.
1696 MachineFrameInfo *MFI = MF.getFrameInfo();
1697 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1698 const ARMInstrInfo *TII =
1699 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001700 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1701 i != e;
1702 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001703 CCValAssign &VA = ArgLocs[i];
1704 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001705 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001706 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001707 if (VA.getLocInfo() == CCValAssign::Indirect)
1708 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001709 if (VA.needsCustom()) {
1710 // f64 and vector types are split into multiple registers or
1711 // register/stack-slot combinations. The types will not match
1712 // the registers; give up on memory f64 refs until we figure
1713 // out what to do about this.
1714 if (!VA.isRegLoc())
1715 return false;
1716 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001717 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001718 if (RegVT == MVT::v2f64) {
1719 if (!ArgLocs[++i].isRegLoc())
1720 return false;
1721 if (!ArgLocs[++i].isRegLoc())
1722 return false;
1723 }
1724 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001725 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1726 MFI, MRI, TII))
1727 return false;
1728 }
1729 }
1730 }
1731 }
1732
1733 return true;
1734}
1735
Dan Gohman98ca4f22009-08-05 01:29:28 +00001736SDValue
1737ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001738 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001739 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001740 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001741 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001742
Bob Wilsondee46d72009-04-17 20:35:10 +00001743 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001744 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001745
Bob Wilsondee46d72009-04-17 20:35:10 +00001746 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001747 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1748 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001749
Dan Gohman98ca4f22009-08-05 01:29:28 +00001750 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001751 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1752 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001753
1754 // If this is the first return lowered for this function, add
1755 // the regs to the liveout set for the function.
1756 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1757 for (unsigned i = 0; i != RVLocs.size(); ++i)
1758 if (RVLocs[i].isRegLoc())
1759 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001760 }
1761
Bob Wilson1f595bb2009-04-17 19:07:39 +00001762 SDValue Flag;
1763
1764 // Copy the result values into the output registers.
1765 for (unsigned i = 0, realRVLocIdx = 0;
1766 i != RVLocs.size();
1767 ++i, ++realRVLocIdx) {
1768 CCValAssign &VA = RVLocs[i];
1769 assert(VA.isRegLoc() && "Can only return in registers!");
1770
Dan Gohmanc9403652010-07-07 15:54:55 +00001771 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001772
1773 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001774 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001775 case CCValAssign::Full: break;
1776 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001777 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001778 break;
1779 }
1780
Bob Wilson1f595bb2009-04-17 19:07:39 +00001781 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001782 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001783 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001784 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1785 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001786 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001787 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001788
1789 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1790 Flag = Chain.getValue(1);
1791 VA = RVLocs[++i]; // skip ahead to next loc
1792 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1793 HalfGPRs.getValue(1), Flag);
1794 Flag = Chain.getValue(1);
1795 VA = RVLocs[++i]; // skip ahead to next loc
1796
1797 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001798 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1799 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001800 }
1801 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1802 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001803 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001804 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001805 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001806 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001807 VA = RVLocs[++i]; // skip ahead to next loc
1808 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1809 Flag);
1810 } else
1811 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1812
Bob Wilsondee46d72009-04-17 20:35:10 +00001813 // Guarantee that all emitted copies are
1814 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001815 Flag = Chain.getValue(1);
1816 }
1817
1818 SDValue result;
1819 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001820 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001821 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001822 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001823
1824 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001825}
1826
Evan Cheng3d2125c2010-11-30 23:55:39 +00001827bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1828 if (N->getNumValues() != 1)
1829 return false;
1830 if (!N->hasNUsesOfValue(1, 0))
1831 return false;
1832
1833 unsigned NumCopies = 0;
1834 SDNode* Copies[2];
1835 SDNode *Use = *N->use_begin();
1836 if (Use->getOpcode() == ISD::CopyToReg) {
1837 Copies[NumCopies++] = Use;
1838 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1839 // f64 returned in a pair of GPRs.
1840 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1841 UI != UE; ++UI) {
1842 if (UI->getOpcode() != ISD::CopyToReg)
1843 return false;
1844 Copies[UI.getUse().getResNo()] = *UI;
1845 ++NumCopies;
1846 }
1847 } else if (Use->getOpcode() == ISD::BITCAST) {
1848 // f32 returned in a single GPR.
1849 if (!Use->hasNUsesOfValue(1, 0))
1850 return false;
1851 Use = *Use->use_begin();
1852 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1853 return false;
1854 Copies[NumCopies++] = Use;
1855 } else {
1856 return false;
1857 }
1858
1859 if (NumCopies != 1 && NumCopies != 2)
1860 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001861
1862 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001863 for (unsigned i = 0; i < NumCopies; ++i) {
1864 SDNode *Copy = Copies[i];
1865 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1866 UI != UE; ++UI) {
1867 if (UI->getOpcode() == ISD::CopyToReg) {
1868 SDNode *Use = *UI;
1869 if (Use == Copies[0] || Use == Copies[1])
1870 continue;
1871 return false;
1872 }
1873 if (UI->getOpcode() != ARMISD::RET_FLAG)
1874 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001875 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001876 }
1877 }
1878
Evan Cheng1bf891a2010-12-01 22:59:46 +00001879 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001880}
1881
Evan Cheng485fafc2011-03-21 01:19:09 +00001882bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1883 if (!EnableARMTailCalls)
1884 return false;
1885
1886 if (!CI->isTailCall())
1887 return false;
1888
1889 return !Subtarget->isThumb1Only();
1890}
1891
Bob Wilsonb62d2572009-11-03 00:02:05 +00001892// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1893// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1894// one of the above mentioned nodes. It has to be wrapped because otherwise
1895// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1896// be used to form addressing mode. These wrapped nodes will be selected
1897// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001898static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001899 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001900 // FIXME there is no actual debug info here
1901 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001902 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001903 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001904 if (CP->isMachineConstantPoolEntry())
1905 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1906 CP->getAlignment());
1907 else
1908 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1909 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001910 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001911}
1912
Jim Grosbache1102ca2010-07-19 17:20:38 +00001913unsigned ARMTargetLowering::getJumpTableEncoding() const {
1914 return MachineJumpTableInfo::EK_Inline;
1915}
1916
Dan Gohmand858e902010-04-17 15:26:15 +00001917SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1918 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001919 MachineFunction &MF = DAG.getMachineFunction();
1920 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1921 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001922 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001923 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001924 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001925 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1926 SDValue CPAddr;
1927 if (RelocM == Reloc::Static) {
1928 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1929 } else {
1930 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001931 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001932 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1933 ARMCP::CPBlockAddress,
1934 PCAdj);
1935 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1936 }
1937 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1938 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001939 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001940 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001941 if (RelocM == Reloc::Static)
1942 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001943 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001944 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001945}
1946
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001947// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001948SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001949ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001950 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001951 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001952 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001953 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001954 MachineFunction &MF = DAG.getMachineFunction();
1955 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001956 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001957 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001958 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001959 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001960 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001961 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001962 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001963 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001964 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001965 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001966
Evan Chenge7e0d622009-11-06 22:24:13 +00001967 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001968 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001969
1970 // call __tls_get_addr.
1971 ArgListTy Args;
1972 ArgListEntry Entry;
1973 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001974 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001975 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001976 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001977 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001978 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1979 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001980 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001981 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001982 return CallResult.first;
1983}
1984
1985// Lower ISD::GlobalTLSAddress using the "initial exec" or
1986// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001987SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001988ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001989 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001990 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001991 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001992 SDValue Offset;
1993 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001994 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001995 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001996 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001997
Chris Lattner4fb63d02009-07-15 04:12:33 +00001998 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001999 MachineFunction &MF = DAG.getMachineFunction();
2000 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002001 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002002 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002003 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2004 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00002005 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002006 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002007 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002008 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002009 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002010 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002011 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002012 Chain = Offset.getValue(1);
2013
Evan Chenge7e0d622009-11-06 22:24:13 +00002014 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002015 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002016
Evan Cheng9eda6892009-10-31 03:39:36 +00002017 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002018 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002019 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002020 } else {
2021 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002022 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002023 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002024 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002025 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002026 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002027 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002028 }
2029
2030 // The address of the thread local variable is the add of the thread
2031 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002032 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002033}
2034
Dan Gohman475871a2008-07-27 21:46:04 +00002035SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002036ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002037 // TODO: implement the "local dynamic" model
2038 assert(Subtarget->isTargetELF() &&
2039 "TLS not implemented for non-ELF targets");
2040 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2041 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2042 // otherwise use the "Local Exec" TLS Model
2043 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2044 return LowerToTLSGeneralDynamicModel(GA, DAG);
2045 else
2046 return LowerToTLSExecModels(GA, DAG);
2047}
2048
Dan Gohman475871a2008-07-27 21:46:04 +00002049SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002050 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002051 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002052 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002053 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002054 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2055 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002056 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002057 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002058 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002059 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002060 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002061 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002062 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002063 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002064 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002065 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002066 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002067 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002068 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002069 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002070 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002071 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002072 }
2073
2074 // If we have T2 ops, we can materialize the address directly via movt/movw
2075 // pair. This is always cheaper.
2076 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002077 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002078 // FIXME: Once remat is capable of dealing with instructions with register
2079 // operands, expand this into two nodes.
2080 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2081 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002082 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002083 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2084 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2085 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2086 MachinePointerInfo::getConstantPool(),
2087 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002088 }
2089}
2090
Dan Gohman475871a2008-07-27 21:46:04 +00002091SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002092 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002093 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002094 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002095 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002096 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002097 MachineFunction &MF = DAG.getMachineFunction();
2098 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2099
Evan Cheng4abce0c2011-05-27 20:11:27 +00002100 // FIXME: Enable this for static codegen when tool issues are fixed.
2101 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002102 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002103 // FIXME: Once remat is capable of dealing with instructions with register
2104 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002105 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002106 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2107 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2108
Evan Cheng53519f02011-01-21 18:55:51 +00002109 unsigned Wrapper = (RelocM == Reloc::PIC_)
2110 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2111 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002112 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002113 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2114 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2115 MachinePointerInfo::getGOT(), false, false, 0);
2116 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002117 }
2118
2119 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002120 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002121 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002122 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002123 } else {
2124 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002125 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2126 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002127 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002128 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002129 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002130 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002131
Evan Cheng9eda6892009-10-31 03:39:36 +00002132 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002133 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002134 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002135 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002136
2137 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002138 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002139 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002140 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002141
Evan Cheng63476a82009-09-03 07:04:02 +00002142 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002143 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002144 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002145
2146 return Result;
2147}
2148
Dan Gohman475871a2008-07-27 21:46:04 +00002149SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002150 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002151 assert(Subtarget->isTargetELF() &&
2152 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002153 MachineFunction &MF = DAG.getMachineFunction();
2154 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002155 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002156 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002157 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002158 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002159 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2160 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002161 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002162 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002163 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002164 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002165 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002166 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002167 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002168 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002169}
2170
Jim Grosbach0e0da732009-05-12 23:59:14 +00002171SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002172ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2173 const {
2174 DebugLoc dl = Op.getDebugLoc();
2175 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
Bill Wendling61512ba2011-05-11 01:11:55 +00002176 Op.getOperand(0), Op.getOperand(1));
Jim Grosbache4ad3872010-10-19 23:27:08 +00002177}
2178
2179SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002180ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2181 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002182 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002183 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2184 Op.getOperand(1), Val);
2185}
2186
2187SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002188ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2189 DebugLoc dl = Op.getDebugLoc();
2190 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2191 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2192}
2193
2194SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002195ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002196 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002197 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002198 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002199 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002200 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002201 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002202 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002203 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2204 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002205 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002206 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002207 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002208 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002209 EVT PtrVT = getPointerTy();
2210 DebugLoc dl = Op.getDebugLoc();
2211 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2212 SDValue CPAddr;
2213 unsigned PCAdj = (RelocM != Reloc::PIC_)
2214 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002215 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002216 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2217 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002218 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002219 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002220 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002221 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002222 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002223 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002224
2225 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002226 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002227 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2228 }
2229 return Result;
2230 }
Evan Cheng92e39162011-03-29 23:06:19 +00002231 case Intrinsic::arm_neon_vmulls:
2232 case Intrinsic::arm_neon_vmullu: {
2233 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2234 ? ARMISD::VMULLs : ARMISD::VMULLu;
2235 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2236 Op.getOperand(1), Op.getOperand(2));
2237 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002238 }
2239}
2240
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002241static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002242 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002243 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002244 if (!Subtarget->hasDataBarrier()) {
2245 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2246 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2247 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002248 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002249 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002250 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002251 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002252 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002253
2254 SDValue Op5 = Op.getOperand(5);
2255 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2256 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2257 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2258 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2259
2260 ARM_MB::MemBOpt DMBOpt;
2261 if (isDeviceBarrier)
2262 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2263 else
2264 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2265 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2266 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002267}
2268
Evan Chengdfed19f2010-11-03 06:34:55 +00002269static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2270 const ARMSubtarget *Subtarget) {
2271 // ARM pre v5TE and Thumb1 does not have preload instructions.
2272 if (!(Subtarget->isThumb2() ||
2273 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2274 // Just preserve the chain.
2275 return Op.getOperand(0);
2276
2277 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002278 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2279 if (!isRead &&
2280 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2281 // ARMv7 with MP extension has PLDW.
2282 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002283
2284 if (Subtarget->isThumb())
2285 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002286 isRead = ~isRead & 1;
2287 unsigned isData = Subtarget->isThumb() ? 0 : 1;
Evan Chengdfed19f2010-11-03 06:34:55 +00002288
Evan Cheng416941d2010-11-04 05:19:35 +00002289 // Currently there is no intrinsic that matches pli.
Evan Chengdfed19f2010-11-03 06:34:55 +00002290 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002291 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2292 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002293}
2294
Dan Gohman1e93df62010-04-17 14:41:14 +00002295static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2296 MachineFunction &MF = DAG.getMachineFunction();
2297 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2298
Evan Chenga8e29892007-01-19 07:51:42 +00002299 // vastart just stores the address of the VarArgsFrameIndex slot into the
2300 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002301 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002302 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002303 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002304 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002305 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2306 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002307}
2308
Dan Gohman475871a2008-07-27 21:46:04 +00002309SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002310ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2311 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002312 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002313 MachineFunction &MF = DAG.getMachineFunction();
2314 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2315
2316 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002317 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002318 RC = ARM::tGPRRegisterClass;
2319 else
2320 RC = ARM::GPRRegisterClass;
2321
2322 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002323 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002324 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002325
2326 SDValue ArgValue2;
2327 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002328 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002329 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002330
2331 // Create load node to retrieve arguments from the stack.
2332 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002333 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002334 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002335 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002336 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002337 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002338 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002339 }
2340
Jim Grosbache5165492009-11-09 00:11:35 +00002341 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002342}
2343
Stuart Hastingsc7315872011-04-20 16:47:52 +00002344void
2345ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2346 unsigned &VARegSize, unsigned &VARegSaveSize)
2347 const {
2348 unsigned NumGPRs;
2349 if (CCInfo.isFirstByValRegValid())
2350 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2351 else {
2352 unsigned int firstUnalloced;
2353 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2354 sizeof(GPRArgRegs) /
2355 sizeof(GPRArgRegs[0]));
2356 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2357 }
2358
2359 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2360 VARegSize = NumGPRs * 4;
2361 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2362}
2363
2364// The remaining GPRs hold either the beginning of variable-argument
2365// data, or the beginning of an aggregate passed by value (usuall
2366// byval). Either way, we allocate stack slots adjacent to the data
2367// provided by our caller, and store the unallocated registers there.
2368// If this is a variadic function, the va_list pointer will begin with
2369// these values; otherwise, this reassembles a (byval) structure that
2370// was split between registers and memory.
2371void
2372ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2373 DebugLoc dl, SDValue &Chain,
2374 unsigned ArgOffset) const {
2375 MachineFunction &MF = DAG.getMachineFunction();
2376 MachineFrameInfo *MFI = MF.getFrameInfo();
2377 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2378 unsigned firstRegToSaveIndex;
2379 if (CCInfo.isFirstByValRegValid())
2380 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2381 else {
2382 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2383 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2384 }
2385
2386 unsigned VARegSize, VARegSaveSize;
2387 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2388 if (VARegSaveSize) {
2389 // If this function is vararg, store any remaining integer argument regs
2390 // to their spots on the stack so that they may be loaded by deferencing
2391 // the result of va_next.
2392 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002393 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2394 ArgOffset + VARegSaveSize
2395 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002396 false));
2397 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2398 getPointerTy());
2399
2400 SmallVector<SDValue, 4> MemOps;
2401 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2402 TargetRegisterClass *RC;
2403 if (AFI->isThumb1OnlyFunction())
2404 RC = ARM::tGPRRegisterClass;
2405 else
2406 RC = ARM::GPRRegisterClass;
2407
2408 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2409 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2410 SDValue Store =
2411 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002412 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002413 false, false, 0);
2414 MemOps.push_back(Store);
2415 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2416 DAG.getConstant(4, getPointerTy()));
2417 }
2418 if (!MemOps.empty())
2419 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2420 &MemOps[0], MemOps.size());
2421 } else
2422 // This will point to the next argument passed via stack.
2423 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2424}
2425
Bob Wilson5bafff32009-06-22 23:27:02 +00002426SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002427ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002428 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002429 const SmallVectorImpl<ISD::InputArg>
2430 &Ins,
2431 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002432 SmallVectorImpl<SDValue> &InVals)
2433 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002434 MachineFunction &MF = DAG.getMachineFunction();
2435 MachineFrameInfo *MFI = MF.getFrameInfo();
2436
Bob Wilson1f595bb2009-04-17 19:07:39 +00002437 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2438
2439 // Assign locations to all of the incoming arguments.
2440 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002441 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2442 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002443 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002444 CCAssignFnForNode(CallConv, /* Return*/ false,
2445 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002446
2447 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002448 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002449
Stuart Hastingsf222e592011-02-28 17:17:53 +00002450 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002451 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2452 CCValAssign &VA = ArgLocs[i];
2453
Bob Wilsondee46d72009-04-17 20:35:10 +00002454 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002455 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002456 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002457
Bob Wilson1f595bb2009-04-17 19:07:39 +00002458 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002459 // f64 and vector types are split up into multiple registers or
2460 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002461 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002462 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002463 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002464 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002465 SDValue ArgValue2;
2466 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002467 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002468 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2469 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002470 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002471 false, false, 0);
2472 } else {
2473 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2474 Chain, DAG, dl);
2475 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002476 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2477 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002478 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002479 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002480 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2481 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002482 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002483
Bob Wilson5bafff32009-06-22 23:27:02 +00002484 } else {
2485 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002486
Owen Anderson825b72b2009-08-11 20:47:22 +00002487 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002488 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002489 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002490 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002491 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002492 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002493 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002494 RC = (AFI->isThumb1OnlyFunction() ?
2495 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002496 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002497 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002498
2499 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002500 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002501 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002502 }
2503
2504 // If this is an 8 or 16-bit value, it is really passed promoted
2505 // to 32 bits. Insert an assert[sz]ext to capture this, then
2506 // truncate to the right size.
2507 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002508 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002509 case CCValAssign::Full: break;
2510 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002511 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002512 break;
2513 case CCValAssign::SExt:
2514 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2515 DAG.getValueType(VA.getValVT()));
2516 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2517 break;
2518 case CCValAssign::ZExt:
2519 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2520 DAG.getValueType(VA.getValVT()));
2521 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2522 break;
2523 }
2524
Dan Gohman98ca4f22009-08-05 01:29:28 +00002525 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002526
2527 } else { // VA.isRegLoc()
2528
2529 // sanity check
2530 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002531 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002532
Stuart Hastingsf222e592011-02-28 17:17:53 +00002533 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002534
Stuart Hastingsf222e592011-02-28 17:17:53 +00002535 // Some Ins[] entries become multiple ArgLoc[] entries.
2536 // Process them only once.
2537 if (index != lastInsIndex)
2538 {
2539 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002540 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002541 // This can be changed with more analysis.
2542 // In case of tail call optimization mark all arguments mutable.
2543 // Since they could be overwritten by lowering of arguments in case of
2544 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002545 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002546 unsigned VARegSize, VARegSaveSize;
2547 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2548 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2549 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002550 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002551 int FI = MFI->CreateFixedObject(Bytes,
2552 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002553 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2554 } else {
2555 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2556 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002557
Stuart Hastingsf222e592011-02-28 17:17:53 +00002558 // Create load nodes to retrieve arguments from the stack.
2559 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2560 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2561 MachinePointerInfo::getFixedStack(FI),
2562 false, false, 0));
2563 }
2564 lastInsIndex = index;
2565 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002566 }
2567 }
2568
2569 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002570 if (isVarArg)
2571 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002572
Dan Gohman98ca4f22009-08-05 01:29:28 +00002573 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002574}
2575
2576/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002577static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002578 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002579 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002580 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002581 // Maybe this has already been legalized into the constant pool?
2582 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002583 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002584 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002585 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002586 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002587 }
2588 }
2589 return false;
2590}
2591
Evan Chenga8e29892007-01-19 07:51:42 +00002592/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2593/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002594SDValue
2595ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002596 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002597 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002598 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002599 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002600 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002601 // Constant does not fit, try adjusting it by one?
2602 switch (CC) {
2603 default: break;
2604 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002605 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002606 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002607 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002608 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002609 }
2610 break;
2611 case ISD::SETULT:
2612 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002613 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002614 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002615 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002616 }
2617 break;
2618 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002619 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002620 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002621 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002622 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002623 }
2624 break;
2625 case ISD::SETULE:
2626 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002627 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002628 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002629 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002630 }
2631 break;
2632 }
2633 }
2634 }
2635
2636 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002637 ARMISD::NodeType CompareType;
2638 switch (CondCode) {
2639 default:
2640 CompareType = ARMISD::CMP;
2641 break;
2642 case ARMCC::EQ:
2643 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002644 // Uses only Z Flag
2645 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002646 break;
2647 }
Evan Cheng218977b2010-07-13 19:27:42 +00002648 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002649 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002650}
2651
2652/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002653SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002654ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002655 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002656 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002657 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002658 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002659 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002660 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2661 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002662}
2663
Bob Wilson79f56c92011-03-08 01:17:20 +00002664/// duplicateCmp - Glue values can have only one use, so this function
2665/// duplicates a comparison node.
2666SDValue
2667ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2668 unsigned Opc = Cmp.getOpcode();
2669 DebugLoc DL = Cmp.getDebugLoc();
2670 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2671 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2672
2673 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2674 Cmp = Cmp.getOperand(0);
2675 Opc = Cmp.getOpcode();
2676 if (Opc == ARMISD::CMPFP)
2677 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2678 else {
2679 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2680 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2681 }
2682 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2683}
2684
Bill Wendlingde2b1512010-08-11 08:43:16 +00002685SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2686 SDValue Cond = Op.getOperand(0);
2687 SDValue SelectTrue = Op.getOperand(1);
2688 SDValue SelectFalse = Op.getOperand(2);
2689 DebugLoc dl = Op.getDebugLoc();
2690
2691 // Convert:
2692 //
2693 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2694 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2695 //
2696 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2697 const ConstantSDNode *CMOVTrue =
2698 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2699 const ConstantSDNode *CMOVFalse =
2700 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2701
2702 if (CMOVTrue && CMOVFalse) {
2703 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2704 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2705
2706 SDValue True;
2707 SDValue False;
2708 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2709 True = SelectTrue;
2710 False = SelectFalse;
2711 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2712 True = SelectFalse;
2713 False = SelectTrue;
2714 }
2715
2716 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002717 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002718 SDValue ARMcc = Cond.getOperand(2);
2719 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002720 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002721 assert(True.getValueType() == VT);
2722 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002723 }
2724 }
2725 }
2726
2727 return DAG.getSelectCC(dl, Cond,
2728 DAG.getConstant(0, Cond.getValueType()),
2729 SelectTrue, SelectFalse, ISD::SETNE);
2730}
2731
Dan Gohmand858e902010-04-17 15:26:15 +00002732SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002733 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002734 SDValue LHS = Op.getOperand(0);
2735 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002736 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002737 SDValue TrueVal = Op.getOperand(2);
2738 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002739 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002740
Owen Anderson825b72b2009-08-11 20:47:22 +00002741 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002742 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002743 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002744 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2745 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002746 }
2747
2748 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002749 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002750
Evan Cheng218977b2010-07-13 19:27:42 +00002751 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2752 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002753 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002754 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002755 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002756 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002757 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002758 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002759 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002760 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002761 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002762 }
2763 return Result;
2764}
2765
Evan Cheng218977b2010-07-13 19:27:42 +00002766/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2767/// to morph to an integer compare sequence.
2768static bool canChangeToInt(SDValue Op, bool &SeenZero,
2769 const ARMSubtarget *Subtarget) {
2770 SDNode *N = Op.getNode();
2771 if (!N->hasOneUse())
2772 // Otherwise it requires moving the value from fp to integer registers.
2773 return false;
2774 if (!N->getNumValues())
2775 return false;
2776 EVT VT = Op.getValueType();
2777 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2778 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2779 // vmrs are very slow, e.g. cortex-a8.
2780 return false;
2781
2782 if (isFloatingPointZero(Op)) {
2783 SeenZero = true;
2784 return true;
2785 }
2786 return ISD::isNormalLoad(N);
2787}
2788
2789static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2790 if (isFloatingPointZero(Op))
2791 return DAG.getConstant(0, MVT::i32);
2792
2793 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2794 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002795 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002796 Ld->isVolatile(), Ld->isNonTemporal(),
2797 Ld->getAlignment());
2798
2799 llvm_unreachable("Unknown VFP cmp argument!");
2800}
2801
2802static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2803 SDValue &RetVal1, SDValue &RetVal2) {
2804 if (isFloatingPointZero(Op)) {
2805 RetVal1 = DAG.getConstant(0, MVT::i32);
2806 RetVal2 = DAG.getConstant(0, MVT::i32);
2807 return;
2808 }
2809
2810 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2811 SDValue Ptr = Ld->getBasePtr();
2812 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2813 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002814 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002815 Ld->isVolatile(), Ld->isNonTemporal(),
2816 Ld->getAlignment());
2817
2818 EVT PtrType = Ptr.getValueType();
2819 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2820 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2821 PtrType, Ptr, DAG.getConstant(4, PtrType));
2822 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2823 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002824 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002825 Ld->isVolatile(), Ld->isNonTemporal(),
2826 NewAlign);
2827 return;
2828 }
2829
2830 llvm_unreachable("Unknown VFP cmp argument!");
2831}
2832
2833/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2834/// f32 and even f64 comparisons to integer ones.
2835SDValue
2836ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2837 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002838 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002839 SDValue LHS = Op.getOperand(2);
2840 SDValue RHS = Op.getOperand(3);
2841 SDValue Dest = Op.getOperand(4);
2842 DebugLoc dl = Op.getDebugLoc();
2843
2844 bool SeenZero = false;
2845 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2846 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002847 // If one of the operand is zero, it's safe to ignore the NaN case since
2848 // we only care about equality comparisons.
2849 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002850 // If unsafe fp math optimization is enabled and there are no other uses of
2851 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002852 // to an integer comparison.
2853 if (CC == ISD::SETOEQ)
2854 CC = ISD::SETEQ;
2855 else if (CC == ISD::SETUNE)
2856 CC = ISD::SETNE;
2857
2858 SDValue ARMcc;
2859 if (LHS.getValueType() == MVT::f32) {
2860 LHS = bitcastf32Toi32(LHS, DAG);
2861 RHS = bitcastf32Toi32(RHS, DAG);
2862 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2863 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2864 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2865 Chain, Dest, ARMcc, CCR, Cmp);
2866 }
2867
2868 SDValue LHS1, LHS2;
2869 SDValue RHS1, RHS2;
2870 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2871 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2872 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2873 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002874 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002875 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2876 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2877 }
2878
2879 return SDValue();
2880}
2881
2882SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2883 SDValue Chain = Op.getOperand(0);
2884 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2885 SDValue LHS = Op.getOperand(2);
2886 SDValue RHS = Op.getOperand(3);
2887 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002888 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002889
Owen Anderson825b72b2009-08-11 20:47:22 +00002890 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002891 SDValue ARMcc;
2892 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002893 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002894 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002895 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002896 }
2897
Owen Anderson825b72b2009-08-11 20:47:22 +00002898 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002899
2900 if (UnsafeFPMath &&
2901 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2902 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2903 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2904 if (Result.getNode())
2905 return Result;
2906 }
2907
Evan Chenga8e29892007-01-19 07:51:42 +00002908 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002909 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002910
Evan Cheng218977b2010-07-13 19:27:42 +00002911 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2912 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002913 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002914 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002915 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002916 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002917 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002918 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2919 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002920 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002921 }
2922 return Res;
2923}
2924
Dan Gohmand858e902010-04-17 15:26:15 +00002925SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002926 SDValue Chain = Op.getOperand(0);
2927 SDValue Table = Op.getOperand(1);
2928 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002929 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002930
Owen Andersone50ed302009-08-10 22:56:29 +00002931 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002932 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2933 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002934 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002935 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002936 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002937 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2938 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002939 if (Subtarget->isThumb2()) {
2940 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2941 // which does another jump to the destination. This also makes it easier
2942 // to translate it to TBB / TBH later.
2943 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002944 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002945 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002946 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002947 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002948 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002949 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002950 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002951 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002952 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002953 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002954 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002955 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002956 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002957 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002958 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002959 }
Evan Chenga8e29892007-01-19 07:51:42 +00002960}
2961
Bob Wilson76a312b2010-03-19 22:51:32 +00002962static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2963 DebugLoc dl = Op.getDebugLoc();
2964 unsigned Opc;
2965
2966 switch (Op.getOpcode()) {
2967 default:
2968 assert(0 && "Invalid opcode!");
2969 case ISD::FP_TO_SINT:
2970 Opc = ARMISD::FTOSI;
2971 break;
2972 case ISD::FP_TO_UINT:
2973 Opc = ARMISD::FTOUI;
2974 break;
2975 }
2976 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002977 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00002978}
2979
Cameron Zwarich3007d332011-03-29 21:41:55 +00002980static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2981 EVT VT = Op.getValueType();
2982 DebugLoc dl = Op.getDebugLoc();
2983
2984 EVT OperandVT = Op.getOperand(0).getValueType();
2985 assert(OperandVT == MVT::v4i16 && "Invalid type for custom lowering!");
2986 if (VT != MVT::v4f32)
2987 return DAG.UnrollVectorOp(Op.getNode());
2988
2989 unsigned CastOpc;
2990 unsigned Opc;
2991 switch (Op.getOpcode()) {
2992 default:
2993 assert(0 && "Invalid opcode!");
2994 case ISD::SINT_TO_FP:
2995 CastOpc = ISD::SIGN_EXTEND;
2996 Opc = ISD::SINT_TO_FP;
2997 break;
2998 case ISD::UINT_TO_FP:
2999 CastOpc = ISD::ZERO_EXTEND;
3000 Opc = ISD::UINT_TO_FP;
3001 break;
3002 }
3003
3004 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3005 return DAG.getNode(Opc, dl, VT, Op);
3006}
3007
Bob Wilson76a312b2010-03-19 22:51:32 +00003008static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3009 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003010 if (VT.isVector())
3011 return LowerVectorINT_TO_FP(Op, DAG);
3012
Bob Wilson76a312b2010-03-19 22:51:32 +00003013 DebugLoc dl = Op.getDebugLoc();
3014 unsigned Opc;
3015
3016 switch (Op.getOpcode()) {
3017 default:
3018 assert(0 && "Invalid opcode!");
3019 case ISD::SINT_TO_FP:
3020 Opc = ARMISD::SITOF;
3021 break;
3022 case ISD::UINT_TO_FP:
3023 Opc = ARMISD::UITOF;
3024 break;
3025 }
3026
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003027 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003028 return DAG.getNode(Opc, dl, VT, Op);
3029}
3030
Evan Cheng515fe3a2010-07-08 02:08:50 +00003031SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003032 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003033 SDValue Tmp0 = Op.getOperand(0);
3034 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003035 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003036 EVT VT = Op.getValueType();
3037 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003038 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3039 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3040 bool UseNEON = !InGPR && Subtarget->hasNEON();
3041
3042 if (UseNEON) {
3043 // Use VBSL to copy the sign bit.
3044 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3045 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3046 DAG.getTargetConstant(EncodedVal, MVT::i32));
3047 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3048 if (VT == MVT::f64)
3049 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3050 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3051 DAG.getConstant(32, MVT::i32));
3052 else /*if (VT == MVT::f32)*/
3053 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3054 if (SrcVT == MVT::f32) {
3055 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3056 if (VT == MVT::f64)
3057 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3058 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3059 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003060 } else if (VT == MVT::f32)
3061 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3062 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3063 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003064 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3065 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3066
3067 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3068 MVT::i32);
3069 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3070 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3071 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003072
Evan Chenge573fb32011-02-23 02:24:55 +00003073 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3074 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3075 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003076 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003077 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3078 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3079 DAG.getConstant(0, MVT::i32));
3080 } else {
3081 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3082 }
3083
3084 return Res;
3085 }
Evan Chengc143dd42011-02-11 02:28:55 +00003086
3087 // Bitcast operand 1 to i32.
3088 if (SrcVT == MVT::f64)
3089 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3090 &Tmp1, 1).getValue(1);
3091 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3092
Evan Chenge573fb32011-02-23 02:24:55 +00003093 // Or in the signbit with integer operations.
3094 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3095 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3096 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3097 if (VT == MVT::f32) {
3098 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3099 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3100 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3101 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003102 }
3103
Evan Chenge573fb32011-02-23 02:24:55 +00003104 // f64: Or the high part with signbit and then combine two parts.
3105 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3106 &Tmp0, 1);
3107 SDValue Lo = Tmp0.getValue(0);
3108 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3109 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3110 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003111}
3112
Evan Cheng2457f2c2010-05-22 01:47:14 +00003113SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3114 MachineFunction &MF = DAG.getMachineFunction();
3115 MachineFrameInfo *MFI = MF.getFrameInfo();
3116 MFI->setReturnAddressIsTaken(true);
3117
3118 EVT VT = Op.getValueType();
3119 DebugLoc dl = Op.getDebugLoc();
3120 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3121 if (Depth) {
3122 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3123 SDValue Offset = DAG.getConstant(4, MVT::i32);
3124 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3125 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003126 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003127 }
3128
3129 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003130 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003131 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3132}
3133
Dan Gohmand858e902010-04-17 15:26:15 +00003134SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003135 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3136 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003137
Owen Andersone50ed302009-08-10 22:56:29 +00003138 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003139 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3140 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003141 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003142 ? ARM::R7 : ARM::R11;
3143 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3144 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003145 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3146 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00003147 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003148 return FrameAddr;
3149}
3150
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003151/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003152/// expand a bit convert where either the source or destination type is i64 to
3153/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3154/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3155/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003156static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003157 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3158 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003159 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003160
Bob Wilson9f3f0612010-04-17 05:30:19 +00003161 // This function is only supposed to be called for i64 types, either as the
3162 // source or destination of the bit convert.
3163 EVT SrcVT = Op.getValueType();
3164 EVT DstVT = N->getValueType(0);
3165 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003166 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003167
Bob Wilson9f3f0612010-04-17 05:30:19 +00003168 // Turn i64->f64 into VMOVDRR.
3169 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003170 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3171 DAG.getConstant(0, MVT::i32));
3172 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3173 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003174 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003175 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003176 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003177
Jim Grosbache5165492009-11-09 00:11:35 +00003178 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003179 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3180 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3181 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3182 // Merge the pieces into a single i64 value.
3183 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3184 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003185
Bob Wilson9f3f0612010-04-17 05:30:19 +00003186 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003187}
3188
Bob Wilson5bafff32009-06-22 23:27:02 +00003189/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003190/// Zero vectors are used to represent vector negation and in those cases
3191/// will be implemented with the NEON VNEG instruction. However, VNEG does
3192/// not support i64 elements, so sometimes the zero vectors will need to be
3193/// explicitly constructed. Regardless, use a canonical VMOV to create the
3194/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003195static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003196 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003197 // The canonical modified immediate encoding of a zero vector is....0!
3198 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3199 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3200 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003201 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003202}
3203
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003204/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3205/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003206SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3207 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003208 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3209 EVT VT = Op.getValueType();
3210 unsigned VTBits = VT.getSizeInBits();
3211 DebugLoc dl = Op.getDebugLoc();
3212 SDValue ShOpLo = Op.getOperand(0);
3213 SDValue ShOpHi = Op.getOperand(1);
3214 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003215 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003216 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003217
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003218 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3219
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003220 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3221 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3222 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3223 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3224 DAG.getConstant(VTBits, MVT::i32));
3225 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3226 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003227 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003228
3229 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3230 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003231 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003232 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003233 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003234 CCR, Cmp);
3235
3236 SDValue Ops[2] = { Lo, Hi };
3237 return DAG.getMergeValues(Ops, 2, dl);
3238}
3239
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003240/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3241/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003242SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3243 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003244 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3245 EVT VT = Op.getValueType();
3246 unsigned VTBits = VT.getSizeInBits();
3247 DebugLoc dl = Op.getDebugLoc();
3248 SDValue ShOpLo = Op.getOperand(0);
3249 SDValue ShOpHi = Op.getOperand(1);
3250 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003251 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003252
3253 assert(Op.getOpcode() == ISD::SHL_PARTS);
3254 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3255 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3256 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3257 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3258 DAG.getConstant(VTBits, MVT::i32));
3259 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3260 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3261
3262 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3263 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3264 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003265 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003266 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003267 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003268 CCR, Cmp);
3269
3270 SDValue Ops[2] = { Lo, Hi };
3271 return DAG.getMergeValues(Ops, 2, dl);
3272}
3273
Jim Grosbach4725ca72010-09-08 03:54:02 +00003274SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003275 SelectionDAG &DAG) const {
3276 // The rounding mode is in bits 23:22 of the FPSCR.
3277 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3278 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3279 // so that the shift + and get folded into a bitfield extract.
3280 DebugLoc dl = Op.getDebugLoc();
3281 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3282 DAG.getConstant(Intrinsic::arm_get_fpscr,
3283 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003284 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003285 DAG.getConstant(1U << 22, MVT::i32));
3286 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3287 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003288 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003289 DAG.getConstant(3, MVT::i32));
3290}
3291
Jim Grosbach3482c802010-01-18 19:58:49 +00003292static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3293 const ARMSubtarget *ST) {
3294 EVT VT = N->getValueType(0);
3295 DebugLoc dl = N->getDebugLoc();
3296
3297 if (!ST->hasV6T2Ops())
3298 return SDValue();
3299
3300 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3301 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3302}
3303
Bob Wilson5bafff32009-06-22 23:27:02 +00003304static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3305 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003306 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003307 DebugLoc dl = N->getDebugLoc();
3308
Bob Wilsond5448bb2010-11-18 21:16:28 +00003309 if (!VT.isVector())
3310 return SDValue();
3311
Bob Wilson5bafff32009-06-22 23:27:02 +00003312 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003313 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003314
Bob Wilsond5448bb2010-11-18 21:16:28 +00003315 // Left shifts translate directly to the vshiftu intrinsic.
3316 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003317 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003318 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3319 N->getOperand(0), N->getOperand(1));
3320
3321 assert((N->getOpcode() == ISD::SRA ||
3322 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3323
3324 // NEON uses the same intrinsics for both left and right shifts. For
3325 // right shifts, the shift amounts are negative, so negate the vector of
3326 // shift amounts.
3327 EVT ShiftVT = N->getOperand(1).getValueType();
3328 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3329 getZeroVector(ShiftVT, DAG, dl),
3330 N->getOperand(1));
3331 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3332 Intrinsic::arm_neon_vshifts :
3333 Intrinsic::arm_neon_vshiftu);
3334 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3335 DAG.getConstant(vshiftInt, MVT::i32),
3336 N->getOperand(0), NegatedCount);
3337}
3338
3339static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3340 const ARMSubtarget *ST) {
3341 EVT VT = N->getValueType(0);
3342 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003343
Eli Friedmance392eb2009-08-22 03:13:10 +00003344 // We can get here for a node like i32 = ISD::SHL i32, i64
3345 if (VT != MVT::i64)
3346 return SDValue();
3347
3348 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003349 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003350
Chris Lattner27a6c732007-11-24 07:07:01 +00003351 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3352 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003353 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003354 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003355
Chris Lattner27a6c732007-11-24 07:07:01 +00003356 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003357 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003358
Chris Lattner27a6c732007-11-24 07:07:01 +00003359 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003360 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003361 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003362 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003363 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003364
Chris Lattner27a6c732007-11-24 07:07:01 +00003365 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3366 // captures the result into a carry flag.
3367 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003368 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003369
Chris Lattner27a6c732007-11-24 07:07:01 +00003370 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003371 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003372
Chris Lattner27a6c732007-11-24 07:07:01 +00003373 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003374 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003375}
3376
Bob Wilson5bafff32009-06-22 23:27:02 +00003377static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3378 SDValue TmpOp0, TmpOp1;
3379 bool Invert = false;
3380 bool Swap = false;
3381 unsigned Opc = 0;
3382
3383 SDValue Op0 = Op.getOperand(0);
3384 SDValue Op1 = Op.getOperand(1);
3385 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003386 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003387 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3388 DebugLoc dl = Op.getDebugLoc();
3389
3390 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3391 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003392 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003393 case ISD::SETUNE:
3394 case ISD::SETNE: Invert = true; // Fallthrough
3395 case ISD::SETOEQ:
3396 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3397 case ISD::SETOLT:
3398 case ISD::SETLT: Swap = true; // Fallthrough
3399 case ISD::SETOGT:
3400 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3401 case ISD::SETOLE:
3402 case ISD::SETLE: Swap = true; // Fallthrough
3403 case ISD::SETOGE:
3404 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3405 case ISD::SETUGE: Swap = true; // Fallthrough
3406 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3407 case ISD::SETUGT: Swap = true; // Fallthrough
3408 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3409 case ISD::SETUEQ: Invert = true; // Fallthrough
3410 case ISD::SETONE:
3411 // Expand this to (OLT | OGT).
3412 TmpOp0 = Op0;
3413 TmpOp1 = Op1;
3414 Opc = ISD::OR;
3415 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3416 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3417 break;
3418 case ISD::SETUO: Invert = true; // Fallthrough
3419 case ISD::SETO:
3420 // Expand this to (OLT | OGE).
3421 TmpOp0 = Op0;
3422 TmpOp1 = Op1;
3423 Opc = ISD::OR;
3424 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3425 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3426 break;
3427 }
3428 } else {
3429 // Integer comparisons.
3430 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003431 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003432 case ISD::SETNE: Invert = true;
3433 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3434 case ISD::SETLT: Swap = true;
3435 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3436 case ISD::SETLE: Swap = true;
3437 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3438 case ISD::SETULT: Swap = true;
3439 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3440 case ISD::SETULE: Swap = true;
3441 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3442 }
3443
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003444 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003445 if (Opc == ARMISD::VCEQ) {
3446
3447 SDValue AndOp;
3448 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3449 AndOp = Op0;
3450 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3451 AndOp = Op1;
3452
3453 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003454 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003455 AndOp = AndOp.getOperand(0);
3456
3457 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3458 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003459 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3460 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003461 Invert = !Invert;
3462 }
3463 }
3464 }
3465
3466 if (Swap)
3467 std::swap(Op0, Op1);
3468
Owen Andersonc24cb352010-11-08 23:21:22 +00003469 // If one of the operands is a constant vector zero, attempt to fold the
3470 // comparison to a specialized compare-against-zero form.
3471 SDValue SingleOp;
3472 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3473 SingleOp = Op0;
3474 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3475 if (Opc == ARMISD::VCGE)
3476 Opc = ARMISD::VCLEZ;
3477 else if (Opc == ARMISD::VCGT)
3478 Opc = ARMISD::VCLTZ;
3479 SingleOp = Op1;
3480 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003481
Owen Andersonc24cb352010-11-08 23:21:22 +00003482 SDValue Result;
3483 if (SingleOp.getNode()) {
3484 switch (Opc) {
3485 case ARMISD::VCEQ:
3486 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3487 case ARMISD::VCGE:
3488 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3489 case ARMISD::VCLEZ:
3490 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3491 case ARMISD::VCGT:
3492 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3493 case ARMISD::VCLTZ:
3494 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3495 default:
3496 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3497 }
3498 } else {
3499 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3500 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003501
3502 if (Invert)
3503 Result = DAG.getNOT(dl, Result, VT);
3504
3505 return Result;
3506}
3507
Bob Wilsond3c42842010-06-14 22:19:57 +00003508/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3509/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003510/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003511static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3512 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003513 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003514 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003515
Bob Wilson827b2102010-06-15 19:05:35 +00003516 // SplatBitSize is set to the smallest size that splats the vector, so a
3517 // zero vector will always have SplatBitSize == 8. However, NEON modified
3518 // immediate instructions others than VMOV do not support the 8-bit encoding
3519 // of a zero vector, and the default encoding of zero is supposed to be the
3520 // 32-bit version.
3521 if (SplatBits == 0)
3522 SplatBitSize = 32;
3523
Bob Wilson5bafff32009-06-22 23:27:02 +00003524 switch (SplatBitSize) {
3525 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003526 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003527 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003528 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003529 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003530 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003531 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003532 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003533 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003534
3535 case 16:
3536 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003537 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003538 if ((SplatBits & ~0xff) == 0) {
3539 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003540 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003541 Imm = SplatBits;
3542 break;
3543 }
3544 if ((SplatBits & ~0xff00) == 0) {
3545 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003546 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003547 Imm = SplatBits >> 8;
3548 break;
3549 }
3550 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003551
3552 case 32:
3553 // NEON's 32-bit VMOV supports splat values where:
3554 // * only one byte is nonzero, or
3555 // * the least significant byte is 0xff and the second byte is nonzero, or
3556 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003557 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003558 if ((SplatBits & ~0xff) == 0) {
3559 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003560 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003561 Imm = SplatBits;
3562 break;
3563 }
3564 if ((SplatBits & ~0xff00) == 0) {
3565 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003566 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003567 Imm = SplatBits >> 8;
3568 break;
3569 }
3570 if ((SplatBits & ~0xff0000) == 0) {
3571 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003572 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003573 Imm = SplatBits >> 16;
3574 break;
3575 }
3576 if ((SplatBits & ~0xff000000) == 0) {
3577 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003578 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003579 Imm = SplatBits >> 24;
3580 break;
3581 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003582
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003583 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3584 if (type == OtherModImm) return SDValue();
3585
Bob Wilson5bafff32009-06-22 23:27:02 +00003586 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003587 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3588 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003589 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003590 Imm = SplatBits >> 8;
3591 SplatBits |= 0xff;
3592 break;
3593 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003594
3595 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003596 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3597 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003598 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003599 Imm = SplatBits >> 16;
3600 SplatBits |= 0xffff;
3601 break;
3602 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003603
3604 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3605 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3606 // VMOV.I32. A (very) minor optimization would be to replicate the value
3607 // and fall through here to test for a valid 64-bit splat. But, then the
3608 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003609 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003610
3611 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003612 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003613 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003614 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003615 uint64_t BitMask = 0xff;
3616 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003617 unsigned ImmMask = 1;
3618 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003619 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003620 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003621 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003622 Imm |= ImmMask;
3623 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003624 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003625 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003626 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003627 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003628 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003629 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003630 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003631 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003632 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003633 break;
3634 }
3635
Bob Wilson1a913ed2010-06-11 21:34:50 +00003636 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003637 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003638 return SDValue();
3639 }
3640
Bob Wilsoncba270d2010-07-13 21:16:48 +00003641 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3642 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003643}
3644
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003645static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3646 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003647 unsigned NumElts = VT.getVectorNumElements();
3648 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003649
3650 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3651 if (M[0] < 0)
3652 return false;
3653
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003654 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003655
3656 // If this is a VEXT shuffle, the immediate value is the index of the first
3657 // element. The other shuffle indices must be the successive elements after
3658 // the first one.
3659 unsigned ExpectedElt = Imm;
3660 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003661 // Increment the expected index. If it wraps around, it may still be
3662 // a VEXT but the source vectors must be swapped.
3663 ExpectedElt += 1;
3664 if (ExpectedElt == NumElts * 2) {
3665 ExpectedElt = 0;
3666 ReverseVEXT = true;
3667 }
3668
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003669 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003670 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003671 return false;
3672 }
3673
3674 // Adjust the index value if the source operands will be swapped.
3675 if (ReverseVEXT)
3676 Imm -= NumElts;
3677
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003678 return true;
3679}
3680
Bob Wilson8bb9e482009-07-26 00:39:34 +00003681/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3682/// instruction with the specified blocksize. (The order of the elements
3683/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003684static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3685 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003686 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3687 "Only possible block sizes for VREV are: 16, 32, 64");
3688
Bob Wilson8bb9e482009-07-26 00:39:34 +00003689 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003690 if (EltSz == 64)
3691 return false;
3692
3693 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003694 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003695 // If the first shuffle index is UNDEF, be optimistic.
3696 if (M[0] < 0)
3697 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003698
3699 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3700 return false;
3701
3702 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003703 if (M[i] < 0) continue; // ignore UNDEF indices
3704 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003705 return false;
3706 }
3707
3708 return true;
3709}
3710
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003711static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3712 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3713 // range, then 0 is placed into the resulting vector. So pretty much any mask
3714 // of 8 elements can work here.
3715 return VT == MVT::v8i8 && M.size() == 8;
3716}
3717
Bob Wilsonc692cb72009-08-21 20:54:19 +00003718static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3719 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003720 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3721 if (EltSz == 64)
3722 return false;
3723
Bob Wilsonc692cb72009-08-21 20:54:19 +00003724 unsigned NumElts = VT.getVectorNumElements();
3725 WhichResult = (M[0] == 0 ? 0 : 1);
3726 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003727 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3728 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003729 return false;
3730 }
3731 return true;
3732}
3733
Bob Wilson324f4f12009-12-03 06:40:55 +00003734/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3735/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3736/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3737static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3738 unsigned &WhichResult) {
3739 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3740 if (EltSz == 64)
3741 return false;
3742
3743 unsigned NumElts = VT.getVectorNumElements();
3744 WhichResult = (M[0] == 0 ? 0 : 1);
3745 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003746 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3747 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003748 return false;
3749 }
3750 return true;
3751}
3752
Bob Wilsonc692cb72009-08-21 20:54:19 +00003753static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3754 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003755 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3756 if (EltSz == 64)
3757 return false;
3758
Bob Wilsonc692cb72009-08-21 20:54:19 +00003759 unsigned NumElts = VT.getVectorNumElements();
3760 WhichResult = (M[0] == 0 ? 0 : 1);
3761 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003762 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003763 if ((unsigned) M[i] != 2 * i + WhichResult)
3764 return false;
3765 }
3766
3767 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003768 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003769 return false;
3770
3771 return true;
3772}
3773
Bob Wilson324f4f12009-12-03 06:40:55 +00003774/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3775/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3776/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3777static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3778 unsigned &WhichResult) {
3779 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3780 if (EltSz == 64)
3781 return false;
3782
3783 unsigned Half = VT.getVectorNumElements() / 2;
3784 WhichResult = (M[0] == 0 ? 0 : 1);
3785 for (unsigned j = 0; j != 2; ++j) {
3786 unsigned Idx = WhichResult;
3787 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003788 int MIdx = M[i + j * Half];
3789 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003790 return false;
3791 Idx += 2;
3792 }
3793 }
3794
3795 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3796 if (VT.is64BitVector() && EltSz == 32)
3797 return false;
3798
3799 return true;
3800}
3801
Bob Wilsonc692cb72009-08-21 20:54:19 +00003802static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3803 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003804 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3805 if (EltSz == 64)
3806 return false;
3807
Bob Wilsonc692cb72009-08-21 20:54:19 +00003808 unsigned NumElts = VT.getVectorNumElements();
3809 WhichResult = (M[0] == 0 ? 0 : 1);
3810 unsigned Idx = WhichResult * NumElts / 2;
3811 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003812 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3813 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003814 return false;
3815 Idx += 1;
3816 }
3817
3818 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003819 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003820 return false;
3821
3822 return true;
3823}
3824
Bob Wilson324f4f12009-12-03 06:40:55 +00003825/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3826/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3827/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3828static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3829 unsigned &WhichResult) {
3830 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3831 if (EltSz == 64)
3832 return false;
3833
3834 unsigned NumElts = VT.getVectorNumElements();
3835 WhichResult = (M[0] == 0 ? 0 : 1);
3836 unsigned Idx = WhichResult * NumElts / 2;
3837 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003838 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3839 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003840 return false;
3841 Idx += 1;
3842 }
3843
3844 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3845 if (VT.is64BitVector() && EltSz == 32)
3846 return false;
3847
3848 return true;
3849}
3850
Dale Johannesenf630c712010-07-29 20:10:08 +00003851// If N is an integer constant that can be moved into a register in one
3852// instruction, return an SDValue of such a constant (will become a MOV
3853// instruction). Otherwise return null.
3854static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3855 const ARMSubtarget *ST, DebugLoc dl) {
3856 uint64_t Val;
3857 if (!isa<ConstantSDNode>(N))
3858 return SDValue();
3859 Val = cast<ConstantSDNode>(N)->getZExtValue();
3860
3861 if (ST->isThumb1Only()) {
3862 if (Val <= 255 || ~Val <= 255)
3863 return DAG.getConstant(Val, MVT::i32);
3864 } else {
3865 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3866 return DAG.getConstant(Val, MVT::i32);
3867 }
3868 return SDValue();
3869}
3870
Bob Wilson5bafff32009-06-22 23:27:02 +00003871// If this is a case we can't handle, return null and let the default
3872// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003873SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3874 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003875 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003876 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003877 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003878
3879 APInt SplatBits, SplatUndef;
3880 unsigned SplatBitSize;
3881 bool HasAnyUndefs;
3882 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003883 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003884 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003885 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003886 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003887 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003888 DAG, VmovVT, VT.is128BitVector(),
3889 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003890 if (Val.getNode()) {
3891 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003892 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003893 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003894
3895 // Try an immediate VMVN.
3896 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3897 ((1LL << SplatBitSize) - 1));
3898 Val = isNEONModifiedImm(NegatedImm,
3899 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003900 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003901 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003902 if (Val.getNode()) {
3903 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003904 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003905 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003906 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003907 }
3908
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003909 // Scan through the operands to see if only one value is used.
3910 unsigned NumElts = VT.getVectorNumElements();
3911 bool isOnlyLowElement = true;
3912 bool usesOnlyOneValue = true;
3913 bool isConstant = true;
3914 SDValue Value;
3915 for (unsigned i = 0; i < NumElts; ++i) {
3916 SDValue V = Op.getOperand(i);
3917 if (V.getOpcode() == ISD::UNDEF)
3918 continue;
3919 if (i > 0)
3920 isOnlyLowElement = false;
3921 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3922 isConstant = false;
3923
3924 if (!Value.getNode())
3925 Value = V;
3926 else if (V != Value)
3927 usesOnlyOneValue = false;
3928 }
3929
3930 if (!Value.getNode())
3931 return DAG.getUNDEF(VT);
3932
3933 if (isOnlyLowElement)
3934 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3935
Dale Johannesenf630c712010-07-29 20:10:08 +00003936 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3937
Dale Johannesen575cd142010-10-19 20:00:17 +00003938 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3939 // i32 and try again.
3940 if (usesOnlyOneValue && EltSize <= 32) {
3941 if (!isConstant)
3942 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3943 if (VT.getVectorElementType().isFloatingPoint()) {
3944 SmallVector<SDValue, 8> Ops;
3945 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003946 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003947 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003948 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3949 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003950 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3951 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003952 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003953 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003954 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3955 if (Val.getNode())
3956 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003957 }
3958
3959 // If all elements are constants and the case above didn't get hit, fall back
3960 // to the default expansion, which will generate a load from the constant
3961 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003962 if (isConstant)
3963 return SDValue();
3964
Bob Wilson11a1dff2011-01-07 21:37:30 +00003965 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3966 if (NumElts >= 4) {
3967 SDValue shuffle = ReconstructShuffle(Op, DAG);
3968 if (shuffle != SDValue())
3969 return shuffle;
3970 }
3971
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003972 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003973 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3974 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003975 if (EltSize >= 32) {
3976 // Do the expansion with floating-point types, since that is what the VFP
3977 // registers are defined to use, and since i64 is not legal.
3978 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3979 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003980 SmallVector<SDValue, 8> Ops;
3981 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003982 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003983 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003984 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003985 }
3986
3987 return SDValue();
3988}
3989
Bob Wilson11a1dff2011-01-07 21:37:30 +00003990// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003991// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00003992SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
3993 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00003994 DebugLoc dl = Op.getDebugLoc();
3995 EVT VT = Op.getValueType();
3996 unsigned NumElts = VT.getVectorNumElements();
3997
3998 SmallVector<SDValue, 2> SourceVecs;
3999 SmallVector<unsigned, 2> MinElts;
4000 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004001
Bob Wilson11a1dff2011-01-07 21:37:30 +00004002 for (unsigned i = 0; i < NumElts; ++i) {
4003 SDValue V = Op.getOperand(i);
4004 if (V.getOpcode() == ISD::UNDEF)
4005 continue;
4006 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4007 // A shuffle can only come from building a vector from various
4008 // elements of other vectors.
4009 return SDValue();
4010 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004011
Bob Wilson11a1dff2011-01-07 21:37:30 +00004012 // Record this extraction against the appropriate vector if possible...
4013 SDValue SourceVec = V.getOperand(0);
4014 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4015 bool FoundSource = false;
4016 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4017 if (SourceVecs[j] == SourceVec) {
4018 if (MinElts[j] > EltNo)
4019 MinElts[j] = EltNo;
4020 if (MaxElts[j] < EltNo)
4021 MaxElts[j] = EltNo;
4022 FoundSource = true;
4023 break;
4024 }
4025 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004026
Bob Wilson11a1dff2011-01-07 21:37:30 +00004027 // Or record a new source if not...
4028 if (!FoundSource) {
4029 SourceVecs.push_back(SourceVec);
4030 MinElts.push_back(EltNo);
4031 MaxElts.push_back(EltNo);
4032 }
4033 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004034
Bob Wilson11a1dff2011-01-07 21:37:30 +00004035 // Currently only do something sane when at most two source vectors
4036 // involved.
4037 if (SourceVecs.size() > 2)
4038 return SDValue();
4039
4040 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4041 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004042
Bob Wilson11a1dff2011-01-07 21:37:30 +00004043 // This loop extracts the usage patterns of the source vectors
4044 // and prepares appropriate SDValues for a shuffle if possible.
4045 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4046 if (SourceVecs[i].getValueType() == VT) {
4047 // No VEXT necessary
4048 ShuffleSrcs[i] = SourceVecs[i];
4049 VEXTOffsets[i] = 0;
4050 continue;
4051 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4052 // It probably isn't worth padding out a smaller vector just to
4053 // break it down again in a shuffle.
4054 return SDValue();
4055 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004056
Bob Wilson11a1dff2011-01-07 21:37:30 +00004057 // Since only 64-bit and 128-bit vectors are legal on ARM and
4058 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004059 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4060 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004061
Bob Wilson11a1dff2011-01-07 21:37:30 +00004062 if (MaxElts[i] - MinElts[i] >= NumElts) {
4063 // Span too large for a VEXT to cope
4064 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004065 }
4066
Bob Wilson11a1dff2011-01-07 21:37:30 +00004067 if (MinElts[i] >= NumElts) {
4068 // The extraction can just take the second half
4069 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004070 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4071 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004072 DAG.getIntPtrConstant(NumElts));
4073 } else if (MaxElts[i] < NumElts) {
4074 // The extraction can just take the first half
4075 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004076 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4077 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004078 DAG.getIntPtrConstant(0));
4079 } else {
4080 // An actual VEXT is needed
4081 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004082 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4083 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004084 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004085 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4086 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004087 DAG.getIntPtrConstant(NumElts));
4088 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4089 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4090 }
4091 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004092
Bob Wilson11a1dff2011-01-07 21:37:30 +00004093 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004094
Bob Wilson11a1dff2011-01-07 21:37:30 +00004095 for (unsigned i = 0; i < NumElts; ++i) {
4096 SDValue Entry = Op.getOperand(i);
4097 if (Entry.getOpcode() == ISD::UNDEF) {
4098 Mask.push_back(-1);
4099 continue;
4100 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004101
Bob Wilson11a1dff2011-01-07 21:37:30 +00004102 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004103 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4104 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004105 if (ExtractVec == SourceVecs[0]) {
4106 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4107 } else {
4108 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4109 }
4110 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004111
Bob Wilson11a1dff2011-01-07 21:37:30 +00004112 // Final check before we try to produce nonsense...
4113 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004114 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4115 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004116
Bob Wilson11a1dff2011-01-07 21:37:30 +00004117 return SDValue();
4118}
4119
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004120/// isShuffleMaskLegal - Targets can use this to indicate that they only
4121/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4122/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4123/// are assumed to be legal.
4124bool
4125ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4126 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004127 if (VT.getVectorNumElements() == 4 &&
4128 (VT.is128BitVector() || VT.is64BitVector())) {
4129 unsigned PFIndexes[4];
4130 for (unsigned i = 0; i != 4; ++i) {
4131 if (M[i] < 0)
4132 PFIndexes[i] = 8;
4133 else
4134 PFIndexes[i] = M[i];
4135 }
4136
4137 // Compute the index in the perfect shuffle table.
4138 unsigned PFTableIndex =
4139 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4140 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4141 unsigned Cost = (PFEntry >> 30);
4142
4143 if (Cost <= 4)
4144 return true;
4145 }
4146
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004147 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004148 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004149
Bob Wilson53dd2452010-06-07 23:53:38 +00004150 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4151 return (EltSize >= 32 ||
4152 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004153 isVREVMask(M, VT, 64) ||
4154 isVREVMask(M, VT, 32) ||
4155 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004156 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004157 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004158 isVTRNMask(M, VT, WhichResult) ||
4159 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004160 isVZIPMask(M, VT, WhichResult) ||
4161 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4162 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4163 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004164}
4165
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004166/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4167/// the specified operations to build the shuffle.
4168static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4169 SDValue RHS, SelectionDAG &DAG,
4170 DebugLoc dl) {
4171 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4172 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4173 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4174
4175 enum {
4176 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4177 OP_VREV,
4178 OP_VDUP0,
4179 OP_VDUP1,
4180 OP_VDUP2,
4181 OP_VDUP3,
4182 OP_VEXT1,
4183 OP_VEXT2,
4184 OP_VEXT3,
4185 OP_VUZPL, // VUZP, left result
4186 OP_VUZPR, // VUZP, right result
4187 OP_VZIPL, // VZIP, left result
4188 OP_VZIPR, // VZIP, right result
4189 OP_VTRNL, // VTRN, left result
4190 OP_VTRNR // VTRN, right result
4191 };
4192
4193 if (OpNum == OP_COPY) {
4194 if (LHSID == (1*9+2)*9+3) return LHS;
4195 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4196 return RHS;
4197 }
4198
4199 SDValue OpLHS, OpRHS;
4200 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4201 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4202 EVT VT = OpLHS.getValueType();
4203
4204 switch (OpNum) {
4205 default: llvm_unreachable("Unknown shuffle opcode!");
4206 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004207 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004208 if (VT.getVectorElementType() == MVT::i32 ||
4209 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004210 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4211 // vrev <4 x i16> -> VREV32
4212 if (VT.getVectorElementType() == MVT::i16)
4213 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4214 // vrev <4 x i8> -> VREV16
4215 assert(VT.getVectorElementType() == MVT::i8);
4216 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004217 case OP_VDUP0:
4218 case OP_VDUP1:
4219 case OP_VDUP2:
4220 case OP_VDUP3:
4221 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004222 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004223 case OP_VEXT1:
4224 case OP_VEXT2:
4225 case OP_VEXT3:
4226 return DAG.getNode(ARMISD::VEXT, dl, VT,
4227 OpLHS, OpRHS,
4228 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4229 case OP_VUZPL:
4230 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004231 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004232 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4233 case OP_VZIPL:
4234 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004235 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004236 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4237 case OP_VTRNL:
4238 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004239 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4240 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004241 }
4242}
4243
Bill Wendling69a05a72011-03-14 23:02:38 +00004244static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4245 SmallVectorImpl<int> &ShuffleMask,
4246 SelectionDAG &DAG) {
4247 // Check to see if we can use the VTBL instruction.
4248 SDValue V1 = Op.getOperand(0);
4249 SDValue V2 = Op.getOperand(1);
4250 DebugLoc DL = Op.getDebugLoc();
4251
4252 SmallVector<SDValue, 8> VTBLMask;
4253 for (SmallVectorImpl<int>::iterator
4254 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4255 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4256
4257 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4258 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4259 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4260 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004261
Owen Anderson76706012011-04-05 21:48:57 +00004262 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004263 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4264 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004265}
4266
Bob Wilson5bafff32009-06-22 23:27:02 +00004267static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004268 SDValue V1 = Op.getOperand(0);
4269 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004270 DebugLoc dl = Op.getDebugLoc();
4271 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004272 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004273 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004274
Bob Wilson28865062009-08-13 02:13:04 +00004275 // Convert shuffles that are directly supported on NEON to target-specific
4276 // DAG nodes, instead of keeping them as shuffles and matching them again
4277 // during code selection. This is more efficient and avoids the possibility
4278 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004279 // FIXME: floating-point vectors should be canonicalized to integer vectors
4280 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004281 SVN->getMask(ShuffleMask);
4282
Bob Wilson53dd2452010-06-07 23:53:38 +00004283 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4284 if (EltSize <= 32) {
4285 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4286 int Lane = SVN->getSplatIndex();
4287 // If this is undef splat, generate it via "just" vdup, if possible.
4288 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004289
Bob Wilson53dd2452010-06-07 23:53:38 +00004290 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4291 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4292 }
4293 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4294 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004295 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004296
4297 bool ReverseVEXT;
4298 unsigned Imm;
4299 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4300 if (ReverseVEXT)
4301 std::swap(V1, V2);
4302 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4303 DAG.getConstant(Imm, MVT::i32));
4304 }
4305
4306 if (isVREVMask(ShuffleMask, VT, 64))
4307 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4308 if (isVREVMask(ShuffleMask, VT, 32))
4309 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4310 if (isVREVMask(ShuffleMask, VT, 16))
4311 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4312
4313 // Check for Neon shuffles that modify both input vectors in place.
4314 // If both results are used, i.e., if there are two shuffles with the same
4315 // source operands and with masks corresponding to both results of one of
4316 // these operations, DAG memoization will ensure that a single node is
4317 // used for both shuffles.
4318 unsigned WhichResult;
4319 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4320 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4321 V1, V2).getValue(WhichResult);
4322 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4323 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4324 V1, V2).getValue(WhichResult);
4325 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4326 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4327 V1, V2).getValue(WhichResult);
4328
4329 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4330 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4331 V1, V1).getValue(WhichResult);
4332 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4333 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4334 V1, V1).getValue(WhichResult);
4335 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4336 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4337 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004338 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004339
Bob Wilsonc692cb72009-08-21 20:54:19 +00004340 // If the shuffle is not directly supported and it has 4 elements, use
4341 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004342 unsigned NumElts = VT.getVectorNumElements();
4343 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004344 unsigned PFIndexes[4];
4345 for (unsigned i = 0; i != 4; ++i) {
4346 if (ShuffleMask[i] < 0)
4347 PFIndexes[i] = 8;
4348 else
4349 PFIndexes[i] = ShuffleMask[i];
4350 }
4351
4352 // Compute the index in the perfect shuffle table.
4353 unsigned PFTableIndex =
4354 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004355 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4356 unsigned Cost = (PFEntry >> 30);
4357
4358 if (Cost <= 4)
4359 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4360 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004361
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004362 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004363 if (EltSize >= 32) {
4364 // Do the expansion with floating-point types, since that is what the VFP
4365 // registers are defined to use, and since i64 is not legal.
4366 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4367 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004368 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4369 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004370 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004371 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004372 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004373 Ops.push_back(DAG.getUNDEF(EltVT));
4374 else
4375 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4376 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4377 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4378 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004379 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004380 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004381 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004382 }
4383
Bill Wendling69a05a72011-03-14 23:02:38 +00004384 if (VT == MVT::v8i8) {
4385 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4386 if (NewOp.getNode())
4387 return NewOp;
4388 }
4389
Bob Wilson22cac0d2009-08-14 05:16:33 +00004390 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004391}
4392
Bob Wilson5bafff32009-06-22 23:27:02 +00004393static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004394 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004395 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004396 if (!isa<ConstantSDNode>(Lane))
4397 return SDValue();
4398
4399 SDValue Vec = Op.getOperand(0);
4400 if (Op.getValueType() == MVT::i32 &&
4401 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4402 DebugLoc dl = Op.getDebugLoc();
4403 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4404 }
4405
4406 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004407}
4408
Bob Wilsona6d65862009-08-03 20:36:38 +00004409static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4410 // The only time a CONCAT_VECTORS operation can have legal types is when
4411 // two 64-bit vectors are concatenated to a 128-bit vector.
4412 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4413 "unexpected CONCAT_VECTORS");
4414 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004415 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004416 SDValue Op0 = Op.getOperand(0);
4417 SDValue Op1 = Op.getOperand(1);
4418 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004419 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004420 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004421 DAG.getIntPtrConstant(0));
4422 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004423 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004424 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004425 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004426 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004427}
4428
Bob Wilson626613d2010-11-23 19:38:38 +00004429/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4430/// element has been zero/sign-extended, depending on the isSigned parameter,
4431/// from an integer type half its size.
4432static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4433 bool isSigned) {
4434 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4435 EVT VT = N->getValueType(0);
4436 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4437 SDNode *BVN = N->getOperand(0).getNode();
4438 if (BVN->getValueType(0) != MVT::v4i32 ||
4439 BVN->getOpcode() != ISD::BUILD_VECTOR)
4440 return false;
4441 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4442 unsigned HiElt = 1 - LoElt;
4443 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4444 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4445 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4446 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4447 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4448 return false;
4449 if (isSigned) {
4450 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4451 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4452 return true;
4453 } else {
4454 if (Hi0->isNullValue() && Hi1->isNullValue())
4455 return true;
4456 }
4457 return false;
4458 }
4459
4460 if (N->getOpcode() != ISD::BUILD_VECTOR)
4461 return false;
4462
4463 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4464 SDNode *Elt = N->getOperand(i).getNode();
4465 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4466 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4467 unsigned HalfSize = EltSize / 2;
4468 if (isSigned) {
4469 int64_t SExtVal = C->getSExtValue();
4470 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4471 return false;
4472 } else {
4473 if ((C->getZExtValue() >> HalfSize) != 0)
4474 return false;
4475 }
4476 continue;
4477 }
4478 return false;
4479 }
4480
4481 return true;
4482}
4483
4484/// isSignExtended - Check if a node is a vector value that is sign-extended
4485/// or a constant BUILD_VECTOR with sign-extended elements.
4486static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4487 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4488 return true;
4489 if (isExtendedBUILD_VECTOR(N, DAG, true))
4490 return true;
4491 return false;
4492}
4493
4494/// isZeroExtended - Check if a node is a vector value that is zero-extended
4495/// or a constant BUILD_VECTOR with zero-extended elements.
4496static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4497 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4498 return true;
4499 if (isExtendedBUILD_VECTOR(N, DAG, false))
4500 return true;
4501 return false;
4502}
4503
4504/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4505/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004506static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4507 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4508 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004509 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4510 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4511 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4512 LD->isNonTemporal(), LD->getAlignment());
4513 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4514 // have been legalized as a BITCAST from v4i32.
4515 if (N->getOpcode() == ISD::BITCAST) {
4516 SDNode *BVN = N->getOperand(0).getNode();
4517 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4518 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4519 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4520 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4521 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4522 }
4523 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4524 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4525 EVT VT = N->getValueType(0);
4526 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4527 unsigned NumElts = VT.getVectorNumElements();
4528 MVT TruncVT = MVT::getIntegerVT(EltSize);
4529 SmallVector<SDValue, 8> Ops;
4530 for (unsigned i = 0; i != NumElts; ++i) {
4531 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4532 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004533 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004534 }
4535 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4536 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004537}
4538
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004539static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4540 unsigned Opcode = N->getOpcode();
4541 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4542 SDNode *N0 = N->getOperand(0).getNode();
4543 SDNode *N1 = N->getOperand(1).getNode();
4544 return N0->hasOneUse() && N1->hasOneUse() &&
4545 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4546 }
4547 return false;
4548}
4549
4550static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4551 unsigned Opcode = N->getOpcode();
4552 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4553 SDNode *N0 = N->getOperand(0).getNode();
4554 SDNode *N1 = N->getOperand(1).getNode();
4555 return N0->hasOneUse() && N1->hasOneUse() &&
4556 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4557 }
4558 return false;
4559}
4560
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004561static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4562 // Multiplications are only custom-lowered for 128-bit vectors so that
4563 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4564 EVT VT = Op.getValueType();
4565 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4566 SDNode *N0 = Op.getOperand(0).getNode();
4567 SDNode *N1 = Op.getOperand(1).getNode();
4568 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004569 bool isMLA = false;
4570 bool isN0SExt = isSignExtended(N0, DAG);
4571 bool isN1SExt = isSignExtended(N1, DAG);
4572 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004573 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004574 else {
4575 bool isN0ZExt = isZeroExtended(N0, DAG);
4576 bool isN1ZExt = isZeroExtended(N1, DAG);
4577 if (isN0ZExt && isN1ZExt)
4578 NewOpc = ARMISD::VMULLu;
4579 else if (isN1SExt || isN1ZExt) {
4580 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4581 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4582 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4583 NewOpc = ARMISD::VMULLs;
4584 isMLA = true;
4585 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4586 NewOpc = ARMISD::VMULLu;
4587 isMLA = true;
4588 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4589 std::swap(N0, N1);
4590 NewOpc = ARMISD::VMULLu;
4591 isMLA = true;
4592 }
4593 }
4594
4595 if (!NewOpc) {
4596 if (VT == MVT::v2i64)
4597 // Fall through to expand this. It is not legal.
4598 return SDValue();
4599 else
4600 // Other vector multiplications are legal.
4601 return Op;
4602 }
4603 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004604
4605 // Legalize to a VMULL instruction.
4606 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004607 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004608 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004609 if (!isMLA) {
4610 Op0 = SkipExtension(N0, DAG);
4611 assert(Op0.getValueType().is64BitVector() &&
4612 Op1.getValueType().is64BitVector() &&
4613 "unexpected types for extended operands to VMULL");
4614 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4615 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004616
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004617 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4618 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4619 // vmull q0, d4, d6
4620 // vmlal q0, d5, d6
4621 // is faster than
4622 // vaddl q0, d4, d5
4623 // vmovl q1, d6
4624 // vmul q0, q0, q1
4625 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4626 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4627 EVT Op1VT = Op1.getValueType();
4628 return DAG.getNode(N0->getOpcode(), DL, VT,
4629 DAG.getNode(NewOpc, DL, VT,
4630 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4631 DAG.getNode(NewOpc, DL, VT,
4632 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004633}
4634
Owen Anderson76706012011-04-05 21:48:57 +00004635static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004636LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4637 // Convert to float
4638 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4639 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4640 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4641 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4642 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4643 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4644 // Get reciprocal estimate.
4645 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004646 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004647 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4648 // Because char has a smaller range than uchar, we can actually get away
4649 // without any newton steps. This requires that we use a weird bias
4650 // of 0xb000, however (again, this has been exhaustively tested).
4651 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4652 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4653 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4654 Y = DAG.getConstant(0xb000, MVT::i32);
4655 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4656 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4657 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4658 // Convert back to short.
4659 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4660 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4661 return X;
4662}
4663
Owen Anderson76706012011-04-05 21:48:57 +00004664static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004665LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4666 SDValue N2;
4667 // Convert to float.
4668 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4669 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4670 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4671 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4672 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4673 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004674
Nate Begeman7973f352011-02-11 20:53:29 +00004675 // Use reciprocal estimate and one refinement step.
4676 // float4 recip = vrecpeq_f32(yf);
4677 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004678 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004679 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004680 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004681 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4682 N1, N2);
4683 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4684 // Because short has a smaller range than ushort, we can actually get away
4685 // with only a single newton step. This requires that we use a weird bias
4686 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004687 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004688 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4689 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004690 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004691 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4692 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4693 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4694 // Convert back to integer and return.
4695 // return vmovn_s32(vcvt_s32_f32(result));
4696 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4697 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4698 return N0;
4699}
4700
4701static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4702 EVT VT = Op.getValueType();
4703 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4704 "unexpected type for custom-lowering ISD::SDIV");
4705
4706 DebugLoc dl = Op.getDebugLoc();
4707 SDValue N0 = Op.getOperand(0);
4708 SDValue N1 = Op.getOperand(1);
4709 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004710
Nate Begeman7973f352011-02-11 20:53:29 +00004711 if (VT == MVT::v8i8) {
4712 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4713 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004714
Nate Begeman7973f352011-02-11 20:53:29 +00004715 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4716 DAG.getIntPtrConstant(4));
4717 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004718 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004719 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4720 DAG.getIntPtrConstant(0));
4721 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4722 DAG.getIntPtrConstant(0));
4723
4724 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4725 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4726
4727 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4728 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004729
Nate Begeman7973f352011-02-11 20:53:29 +00004730 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4731 return N0;
4732 }
4733 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4734}
4735
4736static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4737 EVT VT = Op.getValueType();
4738 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4739 "unexpected type for custom-lowering ISD::UDIV");
4740
4741 DebugLoc dl = Op.getDebugLoc();
4742 SDValue N0 = Op.getOperand(0);
4743 SDValue N1 = Op.getOperand(1);
4744 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004745
Nate Begeman7973f352011-02-11 20:53:29 +00004746 if (VT == MVT::v8i8) {
4747 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4748 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004749
Nate Begeman7973f352011-02-11 20:53:29 +00004750 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4751 DAG.getIntPtrConstant(4));
4752 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004753 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004754 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4755 DAG.getIntPtrConstant(0));
4756 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4757 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004758
Nate Begeman7973f352011-02-11 20:53:29 +00004759 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4760 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004761
Nate Begeman7973f352011-02-11 20:53:29 +00004762 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4763 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004764
4765 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004766 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4767 N0);
4768 return N0;
4769 }
Owen Anderson76706012011-04-05 21:48:57 +00004770
Nate Begeman7973f352011-02-11 20:53:29 +00004771 // v4i16 sdiv ... Convert to float.
4772 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4773 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4774 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4775 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4776 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004777 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004778
4779 // Use reciprocal estimate and two refinement steps.
4780 // float4 recip = vrecpeq_f32(yf);
4781 // recip *= vrecpsq_f32(yf, recip);
4782 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004783 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004784 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00004785 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004786 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004787 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004788 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004789 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004790 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004791 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004792 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4793 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4794 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4795 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004796 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004797 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4798 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4799 N1 = DAG.getConstant(2, MVT::i32);
4800 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4801 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4802 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4803 // Convert back to integer and return.
4804 // return vmovn_u32(vcvt_s32_f32(result));
4805 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4806 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4807 return N0;
4808}
4809
Dan Gohmand858e902010-04-17 15:26:15 +00004810SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004811 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004812 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004813 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004814 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004815 case ISD::GlobalAddress:
4816 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4817 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004818 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004819 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004820 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4821 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004822 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004823 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004824 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004825 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004826 case ISD::SINT_TO_FP:
4827 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4828 case ISD::FP_TO_SINT:
4829 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004830 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004831 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004832 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004833 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004834 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004835 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004836 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004837 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4838 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00004839 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004840 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004841 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004842 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004843 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004844 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004845 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004846 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004847 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004848 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004849 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004850 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004851 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004852 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004853 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00004854 case ISD::SDIV: return LowerSDIV(Op, DAG);
4855 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004856 }
Dan Gohman475871a2008-07-27 21:46:04 +00004857 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004858}
4859
Duncan Sands1607f052008-12-01 11:39:25 +00004860/// ReplaceNodeResults - Replace the results of node with an illegal result
4861/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004862void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4863 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004864 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004865 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004866 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004867 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004868 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004869 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004870 case ISD::BITCAST:
4871 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004872 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004873 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004874 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004875 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004876 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004877 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004878 if (Res.getNode())
4879 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004880}
Chris Lattner27a6c732007-11-24 07:07:01 +00004881
Evan Chenga8e29892007-01-19 07:51:42 +00004882//===----------------------------------------------------------------------===//
4883// ARM Scheduler Hooks
4884//===----------------------------------------------------------------------===//
4885
4886MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004887ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4888 MachineBasicBlock *BB,
4889 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004890 unsigned dest = MI->getOperand(0).getReg();
4891 unsigned ptr = MI->getOperand(1).getReg();
4892 unsigned oldval = MI->getOperand(2).getReg();
4893 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004894 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4895 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004896 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004897
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004898 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
4899 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00004900 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004901 : ARM::GPRRegisterClass);
4902
4903 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00004904 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
4905 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
4906 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004907 }
4908
Jim Grosbach5278eb82009-12-11 01:42:04 +00004909 unsigned ldrOpc, strOpc;
4910 switch (Size) {
4911 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004912 case 1:
4913 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00004914 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004915 break;
4916 case 2:
4917 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4918 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4919 break;
4920 case 4:
4921 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4922 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4923 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004924 }
4925
4926 MachineFunction *MF = BB->getParent();
4927 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4928 MachineFunction::iterator It = BB;
4929 ++It; // insert the new blocks after the current block
4930
4931 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4932 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4933 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4934 MF->insert(It, loop1MBB);
4935 MF->insert(It, loop2MBB);
4936 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004937
4938 // Transfer the remainder of BB and its successor edges to exitMBB.
4939 exitMBB->splice(exitMBB->begin(), BB,
4940 llvm::next(MachineBasicBlock::iterator(MI)),
4941 BB->end());
4942 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004943
4944 // thisMBB:
4945 // ...
4946 // fallthrough --> loop1MBB
4947 BB->addSuccessor(loop1MBB);
4948
4949 // loop1MBB:
4950 // ldrex dest, [ptr]
4951 // cmp dest, oldval
4952 // bne exitMBB
4953 BB = loop1MBB;
4954 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004955 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004956 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004957 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4958 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004959 BB->addSuccessor(loop2MBB);
4960 BB->addSuccessor(exitMBB);
4961
4962 // loop2MBB:
4963 // strex scratch, newval, [ptr]
4964 // cmp scratch, #0
4965 // bne loop1MBB
4966 BB = loop2MBB;
4967 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
4968 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004969 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004970 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004971 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4972 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004973 BB->addSuccessor(loop1MBB);
4974 BB->addSuccessor(exitMBB);
4975
4976 // exitMBB:
4977 // ...
4978 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00004979
Dan Gohman14152b42010-07-06 20:24:04 +00004980 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00004981
Jim Grosbach5278eb82009-12-11 01:42:04 +00004982 return BB;
4983}
4984
4985MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004986ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
4987 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00004988 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
4989 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4990
4991 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004992 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004993 MachineFunction::iterator It = BB;
4994 ++It;
4995
4996 unsigned dest = MI->getOperand(0).getReg();
4997 unsigned ptr = MI->getOperand(1).getReg();
4998 unsigned incr = MI->getOperand(2).getReg();
4999 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005000 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005001
5002 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5003 if (isThumb2) {
5004 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5005 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5006 }
5007
Jim Grosbachc3c23542009-12-14 04:22:04 +00005008 unsigned ldrOpc, strOpc;
5009 switch (Size) {
5010 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005011 case 1:
5012 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005013 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005014 break;
5015 case 2:
5016 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5017 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5018 break;
5019 case 4:
5020 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5021 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5022 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005023 }
5024
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005025 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5026 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5027 MF->insert(It, loopMBB);
5028 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005029
5030 // Transfer the remainder of BB and its successor edges to exitMBB.
5031 exitMBB->splice(exitMBB->begin(), BB,
5032 llvm::next(MachineBasicBlock::iterator(MI)),
5033 BB->end());
5034 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005035
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005036 TargetRegisterClass *TRC =
5037 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5038 unsigned scratch = MRI.createVirtualRegister(TRC);
5039 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005040
5041 // thisMBB:
5042 // ...
5043 // fallthrough --> loopMBB
5044 BB->addSuccessor(loopMBB);
5045
5046 // loopMBB:
5047 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005048 // <binop> scratch2, dest, incr
5049 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005050 // cmp scratch, #0
5051 // bne- loopMBB
5052 // fallthrough --> exitMBB
5053 BB = loopMBB;
5054 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00005055 if (BinOpcode) {
5056 // operand order needs to go the other way for NAND
5057 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5058 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5059 addReg(incr).addReg(dest)).addReg(0);
5060 else
5061 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5062 addReg(dest).addReg(incr)).addReg(0);
5063 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005064
5065 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5066 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005067 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005068 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005069 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5070 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005071
5072 BB->addSuccessor(loopMBB);
5073 BB->addSuccessor(exitMBB);
5074
5075 // exitMBB:
5076 // ...
5077 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005078
Dan Gohman14152b42010-07-06 20:24:04 +00005079 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005080
Jim Grosbachc3c23542009-12-14 04:22:04 +00005081 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005082}
5083
Jim Grosbachf7da8822011-04-26 19:44:18 +00005084MachineBasicBlock *
5085ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5086 MachineBasicBlock *BB,
5087 unsigned Size,
5088 bool signExtend,
5089 ARMCC::CondCodes Cond) const {
5090 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5091
5092 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5093 MachineFunction *MF = BB->getParent();
5094 MachineFunction::iterator It = BB;
5095 ++It;
5096
5097 unsigned dest = MI->getOperand(0).getReg();
5098 unsigned ptr = MI->getOperand(1).getReg();
5099 unsigned incr = MI->getOperand(2).getReg();
5100 unsigned oldval = dest;
5101 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005102 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005103
5104 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5105 if (isThumb2) {
5106 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5107 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5108 }
5109
Jim Grosbachf7da8822011-04-26 19:44:18 +00005110 unsigned ldrOpc, strOpc, extendOpc;
5111 switch (Size) {
5112 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5113 case 1:
5114 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5115 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
5116 extendOpc = isThumb2 ? ARM::t2SXTBr : ARM::SXTBr;
5117 break;
5118 case 2:
5119 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5120 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5121 extendOpc = isThumb2 ? ARM::t2SXTHr : ARM::SXTHr;
5122 break;
5123 case 4:
5124 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5125 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5126 extendOpc = 0;
5127 break;
5128 }
5129
5130 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5131 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5132 MF->insert(It, loopMBB);
5133 MF->insert(It, exitMBB);
5134
5135 // Transfer the remainder of BB and its successor edges to exitMBB.
5136 exitMBB->splice(exitMBB->begin(), BB,
5137 llvm::next(MachineBasicBlock::iterator(MI)),
5138 BB->end());
5139 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5140
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005141 TargetRegisterClass *TRC =
5142 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5143 unsigned scratch = MRI.createVirtualRegister(TRC);
5144 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005145
5146 // thisMBB:
5147 // ...
5148 // fallthrough --> loopMBB
5149 BB->addSuccessor(loopMBB);
5150
5151 // loopMBB:
5152 // ldrex dest, ptr
5153 // (sign extend dest, if required)
5154 // cmp dest, incr
5155 // cmov.cond scratch2, dest, incr
5156 // strex scratch, scratch2, ptr
5157 // cmp scratch, #0
5158 // bne- loopMBB
5159 // fallthrough --> exitMBB
5160 BB = loopMBB;
5161 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
5162
5163 // Sign extend the value, if necessary.
5164 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005165 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005166 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval).addReg(dest));
5167 }
5168
5169 // Build compare and cmov instructions.
5170 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5171 .addReg(oldval).addReg(incr));
5172 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5173 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5174
5175 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5176 .addReg(ptr));
5177 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5178 .addReg(scratch).addImm(0));
5179 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5180 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5181
5182 BB->addSuccessor(loopMBB);
5183 BB->addSuccessor(exitMBB);
5184
5185 // exitMBB:
5186 // ...
5187 BB = exitMBB;
5188
5189 MI->eraseFromParent(); // The instruction is gone now.
5190
5191 return BB;
5192}
5193
Evan Cheng218977b2010-07-13 19:27:42 +00005194static
5195MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
5196 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
5197 E = MBB->succ_end(); I != E; ++I)
5198 if (*I != Succ)
5199 return *I;
5200 llvm_unreachable("Expecting a BB with two successors!");
5201}
5202
Andrew Trick1c3af772011-04-23 03:55:32 +00005203// FIXME: This opcode table should obviously be expressed in the target
5204// description. We probably just need a "machine opcode" value in the pseudo
5205// instruction. But the ideal solution maybe to simply remove the "S" version
5206// of the opcode altogether.
5207struct AddSubFlagsOpcodePair {
5208 unsigned PseudoOpc;
5209 unsigned MachineOpc;
5210};
5211
5212static AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
5213 {ARM::ADCSri, ARM::ADCri},
5214 {ARM::ADCSrr, ARM::ADCrr},
5215 {ARM::ADCSrs, ARM::ADCrs},
5216 {ARM::SBCSri, ARM::SBCri},
5217 {ARM::SBCSrr, ARM::SBCrr},
5218 {ARM::SBCSrs, ARM::SBCrs},
5219 {ARM::RSBSri, ARM::RSBri},
5220 {ARM::RSBSrr, ARM::RSBrr},
5221 {ARM::RSBSrs, ARM::RSBrs},
5222 {ARM::RSCSri, ARM::RSCri},
5223 {ARM::RSCSrs, ARM::RSCrs},
5224 {ARM::t2ADCSri, ARM::t2ADCri},
5225 {ARM::t2ADCSrr, ARM::t2ADCrr},
5226 {ARM::t2ADCSrs, ARM::t2ADCrs},
5227 {ARM::t2SBCSri, ARM::t2SBCri},
5228 {ARM::t2SBCSrr, ARM::t2SBCrr},
5229 {ARM::t2SBCSrs, ARM::t2SBCrs},
5230 {ARM::t2RSBSri, ARM::t2RSBri},
5231 {ARM::t2RSBSrs, ARM::t2RSBrs},
5232};
5233
5234// Convert and Add or Subtract with Carry and Flags to a generic opcode with
5235// CPSR<def> operand. e.g. ADCS (...) -> ADC (... CPSR<def>).
5236//
5237// FIXME: Somewhere we should assert that CPSR<def> is in the correct
5238// position to be recognized by the target descrition as the 'S' bit.
5239bool ARMTargetLowering::RemapAddSubWithFlags(MachineInstr *MI,
5240 MachineBasicBlock *BB) const {
5241 unsigned OldOpc = MI->getOpcode();
5242 unsigned NewOpc = 0;
5243
5244 // This is only called for instructions that need remapping, so iterating over
5245 // the tiny opcode table is not costly.
5246 static const int NPairs =
5247 sizeof(AddSubFlagsOpcodeMap) / sizeof(AddSubFlagsOpcodePair);
5248 for (AddSubFlagsOpcodePair *Pair = &AddSubFlagsOpcodeMap[0],
5249 *End = &AddSubFlagsOpcodeMap[NPairs]; Pair != End; ++Pair) {
5250 if (OldOpc == Pair->PseudoOpc) {
5251 NewOpc = Pair->MachineOpc;
5252 break;
5253 }
5254 }
5255 if (!NewOpc)
5256 return false;
5257
5258 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5259 DebugLoc dl = MI->getDebugLoc();
5260 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
5261 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
5262 MIB.addOperand(MI->getOperand(i));
5263 AddDefaultPred(MIB);
5264 MIB.addReg(ARM::CPSR, RegState::Define); // S bit
5265 MI->eraseFromParent();
5266 return true;
5267}
5268
Jim Grosbache801dc42009-12-12 01:40:06 +00005269MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00005270ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00005271 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005272 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00005273 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005274 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00005275 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00005276 default: {
5277 if (RemapAddSubWithFlags(MI, BB))
5278 return BB;
5279
Jim Grosbach5278eb82009-12-11 01:42:04 +00005280 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00005281 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00005282 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005283 case ARM::ATOMIC_LOAD_ADD_I8:
5284 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5285 case ARM::ATOMIC_LOAD_ADD_I16:
5286 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5287 case ARM::ATOMIC_LOAD_ADD_I32:
5288 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005289
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005290 case ARM::ATOMIC_LOAD_AND_I8:
5291 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5292 case ARM::ATOMIC_LOAD_AND_I16:
5293 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5294 case ARM::ATOMIC_LOAD_AND_I32:
5295 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005296
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005297 case ARM::ATOMIC_LOAD_OR_I8:
5298 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5299 case ARM::ATOMIC_LOAD_OR_I16:
5300 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5301 case ARM::ATOMIC_LOAD_OR_I32:
5302 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005303
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005304 case ARM::ATOMIC_LOAD_XOR_I8:
5305 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5306 case ARM::ATOMIC_LOAD_XOR_I16:
5307 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5308 case ARM::ATOMIC_LOAD_XOR_I32:
5309 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005310
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005311 case ARM::ATOMIC_LOAD_NAND_I8:
5312 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5313 case ARM::ATOMIC_LOAD_NAND_I16:
5314 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5315 case ARM::ATOMIC_LOAD_NAND_I32:
5316 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005317
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005318 case ARM::ATOMIC_LOAD_SUB_I8:
5319 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5320 case ARM::ATOMIC_LOAD_SUB_I16:
5321 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5322 case ARM::ATOMIC_LOAD_SUB_I32:
5323 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005324
Jim Grosbachf7da8822011-04-26 19:44:18 +00005325 case ARM::ATOMIC_LOAD_MIN_I8:
5326 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
5327 case ARM::ATOMIC_LOAD_MIN_I16:
5328 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
5329 case ARM::ATOMIC_LOAD_MIN_I32:
5330 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
5331
5332 case ARM::ATOMIC_LOAD_MAX_I8:
5333 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
5334 case ARM::ATOMIC_LOAD_MAX_I16:
5335 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
5336 case ARM::ATOMIC_LOAD_MAX_I32:
5337 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
5338
5339 case ARM::ATOMIC_LOAD_UMIN_I8:
5340 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
5341 case ARM::ATOMIC_LOAD_UMIN_I16:
5342 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
5343 case ARM::ATOMIC_LOAD_UMIN_I32:
5344 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
5345
5346 case ARM::ATOMIC_LOAD_UMAX_I8:
5347 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
5348 case ARM::ATOMIC_LOAD_UMAX_I16:
5349 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
5350 case ARM::ATOMIC_LOAD_UMAX_I32:
5351 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
5352
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005353 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
5354 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
5355 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00005356
5357 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
5358 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
5359 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005360
Evan Cheng007ea272009-08-12 05:17:19 +00005361 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00005362 // To "insert" a SELECT_CC instruction, we actually have to insert the
5363 // diamond control-flow pattern. The incoming instruction knows the
5364 // destination vreg to set, the condition code register to branch on, the
5365 // true/false values to select between, and a branch opcode to use.
5366 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005367 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00005368 ++It;
5369
5370 // thisMBB:
5371 // ...
5372 // TrueVal = ...
5373 // cmpTY ccX, r1, r2
5374 // bCC copy1MBB
5375 // fallthrough --> copy0MBB
5376 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005377 MachineFunction *F = BB->getParent();
5378 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
5379 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00005380 F->insert(It, copy0MBB);
5381 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005382
5383 // Transfer the remainder of BB and its successor edges to sinkMBB.
5384 sinkMBB->splice(sinkMBB->begin(), BB,
5385 llvm::next(MachineBasicBlock::iterator(MI)),
5386 BB->end());
5387 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
5388
Dan Gohman258c58c2010-07-06 15:49:48 +00005389 BB->addSuccessor(copy0MBB);
5390 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00005391
Dan Gohman14152b42010-07-06 20:24:04 +00005392 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
5393 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
5394
Evan Chenga8e29892007-01-19 07:51:42 +00005395 // copy0MBB:
5396 // %FalseValue = ...
5397 // # fallthrough to sinkMBB
5398 BB = copy0MBB;
5399
5400 // Update machine-CFG edges
5401 BB->addSuccessor(sinkMBB);
5402
5403 // sinkMBB:
5404 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5405 // ...
5406 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00005407 BuildMI(*BB, BB->begin(), dl,
5408 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00005409 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5410 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5411
Dan Gohman14152b42010-07-06 20:24:04 +00005412 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00005413 return BB;
5414 }
Evan Cheng86198642009-08-07 00:34:42 +00005415
Evan Cheng218977b2010-07-13 19:27:42 +00005416 case ARM::BCCi64:
5417 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00005418 // If there is an unconditional branch to the other successor, remove it.
5419 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00005420
Evan Cheng218977b2010-07-13 19:27:42 +00005421 // Compare both parts that make up the double comparison separately for
5422 // equality.
5423 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
5424
5425 unsigned LHS1 = MI->getOperand(1).getReg();
5426 unsigned LHS2 = MI->getOperand(2).getReg();
5427 if (RHSisZero) {
5428 AddDefaultPred(BuildMI(BB, dl,
5429 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5430 .addReg(LHS1).addImm(0));
5431 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5432 .addReg(LHS2).addImm(0)
5433 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5434 } else {
5435 unsigned RHS1 = MI->getOperand(3).getReg();
5436 unsigned RHS2 = MI->getOperand(4).getReg();
5437 AddDefaultPred(BuildMI(BB, dl,
5438 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5439 .addReg(LHS1).addReg(RHS1));
5440 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5441 .addReg(LHS2).addReg(RHS2)
5442 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5443 }
5444
5445 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
5446 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
5447 if (MI->getOperand(0).getImm() == ARMCC::NE)
5448 std::swap(destMBB, exitMBB);
5449
5450 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5451 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
5452 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
5453 .addMBB(exitMBB);
5454
5455 MI->eraseFromParent(); // The pseudo instruction is gone now.
5456 return BB;
5457 }
Evan Chenga8e29892007-01-19 07:51:42 +00005458 }
5459}
5460
5461//===----------------------------------------------------------------------===//
5462// ARM Optimization Hooks
5463//===----------------------------------------------------------------------===//
5464
Chris Lattnerd1980a52009-03-12 06:52:53 +00005465static
5466SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
5467 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005468 SelectionDAG &DAG = DCI.DAG;
5469 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00005470 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00005471 unsigned Opc = N->getOpcode();
5472 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
5473 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
5474 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
5475 ISD::CondCode CC = ISD::SETCC_INVALID;
5476
5477 if (isSlctCC) {
5478 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
5479 } else {
5480 SDValue CCOp = Slct.getOperand(0);
5481 if (CCOp.getOpcode() == ISD::SETCC)
5482 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
5483 }
5484
5485 bool DoXform = false;
5486 bool InvCC = false;
5487 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
5488 "Bad input!");
5489
5490 if (LHS.getOpcode() == ISD::Constant &&
5491 cast<ConstantSDNode>(LHS)->isNullValue()) {
5492 DoXform = true;
5493 } else if (CC != ISD::SETCC_INVALID &&
5494 RHS.getOpcode() == ISD::Constant &&
5495 cast<ConstantSDNode>(RHS)->isNullValue()) {
5496 std::swap(LHS, RHS);
5497 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005498 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00005499 Op0.getOperand(0).getValueType();
5500 bool isInt = OpVT.isInteger();
5501 CC = ISD::getSetCCInverse(CC, isInt);
5502
5503 if (!TLI.isCondCodeLegal(CC, OpVT))
5504 return SDValue(); // Inverse operator isn't legal.
5505
5506 DoXform = true;
5507 InvCC = true;
5508 }
5509
5510 if (DoXform) {
5511 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
5512 if (isSlctCC)
5513 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
5514 Slct.getOperand(0), Slct.getOperand(1), CC);
5515 SDValue CCOp = Slct.getOperand(0);
5516 if (InvCC)
5517 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
5518 CCOp.getOperand(0), CCOp.getOperand(1), CC);
5519 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
5520 CCOp, OtherOp, Result);
5521 }
5522 return SDValue();
5523}
5524
Bob Wilson3d5792a2010-07-29 20:34:14 +00005525/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
5526/// operands N0 and N1. This is a helper for PerformADDCombine that is
5527/// called with the default operands, and if that fails, with commuted
5528/// operands.
5529static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
5530 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005531 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
5532 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
5533 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
5534 if (Result.getNode()) return Result;
5535 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00005536 return SDValue();
5537}
5538
Bob Wilson3d5792a2010-07-29 20:34:14 +00005539/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
5540///
5541static SDValue PerformADDCombine(SDNode *N,
5542 TargetLowering::DAGCombinerInfo &DCI) {
5543 SDValue N0 = N->getOperand(0);
5544 SDValue N1 = N->getOperand(1);
5545
5546 // First try with the default operand order.
5547 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI);
5548 if (Result.getNode())
5549 return Result;
5550
5551 // If that didn't work, try again with the operands commuted.
5552 return PerformADDCombineWithOperands(N, N1, N0, DCI);
5553}
5554
Chris Lattnerd1980a52009-03-12 06:52:53 +00005555/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00005556///
Chris Lattnerd1980a52009-03-12 06:52:53 +00005557static SDValue PerformSUBCombine(SDNode *N,
5558 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005559 SDValue N0 = N->getOperand(0);
5560 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00005561
Chris Lattnerd1980a52009-03-12 06:52:53 +00005562 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
5563 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
5564 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
5565 if (Result.getNode()) return Result;
5566 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00005567
Chris Lattnerd1980a52009-03-12 06:52:53 +00005568 return SDValue();
5569}
5570
Evan Cheng463d3582011-03-31 19:38:48 +00005571/// PerformVMULCombine
5572/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
5573/// special multiplier accumulator forwarding.
5574/// vmul d3, d0, d2
5575/// vmla d3, d1, d2
5576/// is faster than
5577/// vadd d3, d0, d1
5578/// vmul d3, d3, d2
5579static SDValue PerformVMULCombine(SDNode *N,
5580 TargetLowering::DAGCombinerInfo &DCI,
5581 const ARMSubtarget *Subtarget) {
5582 if (!Subtarget->hasVMLxForwarding())
5583 return SDValue();
5584
5585 SelectionDAG &DAG = DCI.DAG;
5586 SDValue N0 = N->getOperand(0);
5587 SDValue N1 = N->getOperand(1);
5588 unsigned Opcode = N0.getOpcode();
5589 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5590 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
5591 Opcode = N0.getOpcode();
5592 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5593 Opcode != ISD::FADD && Opcode != ISD::FSUB)
5594 return SDValue();
5595 std::swap(N0, N1);
5596 }
5597
5598 EVT VT = N->getValueType(0);
5599 DebugLoc DL = N->getDebugLoc();
5600 SDValue N00 = N0->getOperand(0);
5601 SDValue N01 = N0->getOperand(1);
5602 return DAG.getNode(Opcode, DL, VT,
5603 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
5604 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
5605}
5606
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005607static SDValue PerformMULCombine(SDNode *N,
5608 TargetLowering::DAGCombinerInfo &DCI,
5609 const ARMSubtarget *Subtarget) {
5610 SelectionDAG &DAG = DCI.DAG;
5611
5612 if (Subtarget->isThumb1Only())
5613 return SDValue();
5614
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005615 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5616 return SDValue();
5617
5618 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00005619 if (VT.is64BitVector() || VT.is128BitVector())
5620 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005621 if (VT != MVT::i32)
5622 return SDValue();
5623
5624 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
5625 if (!C)
5626 return SDValue();
5627
5628 uint64_t MulAmt = C->getZExtValue();
5629 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
5630 ShiftAmt = ShiftAmt & (32 - 1);
5631 SDValue V = N->getOperand(0);
5632 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005633
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005634 SDValue Res;
5635 MulAmt >>= ShiftAmt;
5636 if (isPowerOf2_32(MulAmt - 1)) {
5637 // (mul x, 2^N + 1) => (add (shl x, N), x)
5638 Res = DAG.getNode(ISD::ADD, DL, VT,
5639 V, DAG.getNode(ISD::SHL, DL, VT,
5640 V, DAG.getConstant(Log2_32(MulAmt-1),
5641 MVT::i32)));
5642 } else if (isPowerOf2_32(MulAmt + 1)) {
5643 // (mul x, 2^N - 1) => (sub (shl x, N), x)
5644 Res = DAG.getNode(ISD::SUB, DL, VT,
5645 DAG.getNode(ISD::SHL, DL, VT,
5646 V, DAG.getConstant(Log2_32(MulAmt+1),
5647 MVT::i32)),
5648 V);
5649 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005650 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005651
5652 if (ShiftAmt != 0)
5653 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
5654 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005655
5656 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005657 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005658 return SDValue();
5659}
5660
Owen Anderson080c0922010-11-05 19:27:46 +00005661static SDValue PerformANDCombine(SDNode *N,
5662 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00005663
Owen Anderson080c0922010-11-05 19:27:46 +00005664 // Attempt to use immediate-form VBIC
5665 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5666 DebugLoc dl = N->getDebugLoc();
5667 EVT VT = N->getValueType(0);
5668 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005669
Tanya Lattner0433b212011-04-07 15:24:20 +00005670 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5671 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005672
Owen Anderson080c0922010-11-05 19:27:46 +00005673 APInt SplatBits, SplatUndef;
5674 unsigned SplatBitSize;
5675 bool HasAnyUndefs;
5676 if (BVN &&
5677 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5678 if (SplatBitSize <= 64) {
5679 EVT VbicVT;
5680 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
5681 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005682 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005683 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00005684 if (Val.getNode()) {
5685 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005686 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00005687 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005688 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00005689 }
5690 }
5691 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005692
Owen Anderson080c0922010-11-05 19:27:46 +00005693 return SDValue();
5694}
5695
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005696/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
5697static SDValue PerformORCombine(SDNode *N,
5698 TargetLowering::DAGCombinerInfo &DCI,
5699 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00005700 // Attempt to use immediate-form VORR
5701 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5702 DebugLoc dl = N->getDebugLoc();
5703 EVT VT = N->getValueType(0);
5704 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005705
Tanya Lattner0433b212011-04-07 15:24:20 +00005706 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5707 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005708
Owen Anderson60f48702010-11-03 23:15:26 +00005709 APInt SplatBits, SplatUndef;
5710 unsigned SplatBitSize;
5711 bool HasAnyUndefs;
5712 if (BVN && Subtarget->hasNEON() &&
5713 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5714 if (SplatBitSize <= 64) {
5715 EVT VorrVT;
5716 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
5717 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005718 DAG, VorrVT, VT.is128BitVector(),
5719 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00005720 if (Val.getNode()) {
5721 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005722 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00005723 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005724 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00005725 }
5726 }
5727 }
5728
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005729 SDValue N0 = N->getOperand(0);
5730 if (N0.getOpcode() != ISD::AND)
5731 return SDValue();
5732 SDValue N1 = N->getOperand(1);
5733
5734 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
5735 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
5736 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
5737 APInt SplatUndef;
5738 unsigned SplatBitSize;
5739 bool HasAnyUndefs;
5740
5741 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
5742 APInt SplatBits0;
5743 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
5744 HasAnyUndefs) && !HasAnyUndefs) {
5745 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
5746 APInt SplatBits1;
5747 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
5748 HasAnyUndefs) && !HasAnyUndefs &&
5749 SplatBits0 == ~SplatBits1) {
5750 // Canonicalize the vector type to make instruction selection simpler.
5751 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
5752 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
5753 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00005754 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005755 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
5756 }
5757 }
5758 }
5759
Jim Grosbach54238562010-07-17 03:30:54 +00005760 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
5761 // reasonable.
5762
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005763 // BFI is only available on V6T2+
5764 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
5765 return SDValue();
5766
Jim Grosbach54238562010-07-17 03:30:54 +00005767 DebugLoc DL = N->getDebugLoc();
5768 // 1) or (and A, mask), val => ARMbfi A, val, mask
5769 // iff (val & mask) == val
5770 //
5771 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
5772 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005773 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005774 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005775 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005776 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005777
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005778 if (VT != MVT::i32)
5779 return SDValue();
5780
Evan Cheng30fb13f2010-12-13 20:32:54 +00005781 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00005782
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005783 // The value and the mask need to be constants so we can verify this is
5784 // actually a bitfield set. If the mask is 0xffff, we can do better
5785 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00005786 SDValue MaskOp = N0.getOperand(1);
5787 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
5788 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005789 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005790 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005791 if (Mask == 0xffff)
5792 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005793 SDValue Res;
5794 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005795 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
5796 if (N1C) {
5797 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005798 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00005799 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005800
Evan Chenga9688c42010-12-11 04:11:38 +00005801 if (ARM::isBitFieldInvertedMask(Mask)) {
5802 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005803
Evan Cheng30fb13f2010-12-13 20:32:54 +00005804 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00005805 DAG.getConstant(Val, MVT::i32),
5806 DAG.getConstant(Mask, MVT::i32));
5807
5808 // Do not add new nodes to DAG combiner worklist.
5809 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005810 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005811 }
Jim Grosbach54238562010-07-17 03:30:54 +00005812 } else if (N1.getOpcode() == ISD::AND) {
5813 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005814 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5815 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00005816 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005817 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005818
Eric Christopher29aeed12011-03-26 01:21:03 +00005819 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
5820 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00005821 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005822 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005823 // The pack halfword instruction works better for masks that fit it,
5824 // so use that when it's available.
5825 if (Subtarget->hasT2ExtractPack() &&
5826 (Mask == 0xffff || Mask == 0xffff0000))
5827 return SDValue();
5828 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00005829 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00005830 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00005831 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00005832 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00005833 DAG.getConstant(Mask, MVT::i32));
5834 // Do not add new nodes to DAG combiner worklist.
5835 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005836 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005837 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005838 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005839 // The pack halfword instruction works better for masks that fit it,
5840 // so use that when it's available.
5841 if (Subtarget->hasT2ExtractPack() &&
5842 (Mask2 == 0xffff || Mask2 == 0xffff0000))
5843 return SDValue();
5844 // 2b
5845 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005846 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00005847 DAG.getConstant(lsb, MVT::i32));
5848 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00005849 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00005850 // Do not add new nodes to DAG combiner worklist.
5851 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005852 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005853 }
5854 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005855
Evan Cheng30fb13f2010-12-13 20:32:54 +00005856 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
5857 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
5858 ARM::isBitFieldInvertedMask(~Mask)) {
5859 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
5860 // where lsb(mask) == #shamt and masked bits of B are known zero.
5861 SDValue ShAmt = N00.getOperand(1);
5862 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
5863 unsigned LSB = CountTrailingZeros_32(Mask);
5864 if (ShAmtC != LSB)
5865 return SDValue();
5866
5867 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
5868 DAG.getConstant(~Mask, MVT::i32));
5869
5870 // Do not add new nodes to DAG combiner worklist.
5871 DCI.CombineTo(N, Res, false);
5872 }
5873
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005874 return SDValue();
5875}
5876
Evan Cheng0c1aec12010-12-14 03:22:07 +00005877/// PerformBFICombine - (bfi A, (and B, C1), C2) -> (bfi A, B, C2) iff
5878/// C1 & C2 == C1.
5879static SDValue PerformBFICombine(SDNode *N,
5880 TargetLowering::DAGCombinerInfo &DCI) {
5881 SDValue N1 = N->getOperand(1);
5882 if (N1.getOpcode() == ISD::AND) {
5883 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5884 if (!N11C)
5885 return SDValue();
5886 unsigned Mask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
5887 unsigned Mask2 = N11C->getZExtValue();
5888 if ((Mask & Mask2) == Mask2)
5889 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
5890 N->getOperand(0), N1.getOperand(0),
5891 N->getOperand(2));
5892 }
5893 return SDValue();
5894}
5895
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005896/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
5897/// ARMISD::VMOVRRD.
5898static SDValue PerformVMOVRRDCombine(SDNode *N,
5899 TargetLowering::DAGCombinerInfo &DCI) {
5900 // vmovrrd(vmovdrr x, y) -> x,y
5901 SDValue InDouble = N->getOperand(0);
5902 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
5903 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00005904
5905 // vmovrrd(load f64) -> (load i32), (load i32)
5906 SDNode *InNode = InDouble.getNode();
5907 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
5908 InNode->getValueType(0) == MVT::f64 &&
5909 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
5910 !cast<LoadSDNode>(InNode)->isVolatile()) {
5911 // TODO: Should this be done for non-FrameIndex operands?
5912 LoadSDNode *LD = cast<LoadSDNode>(InNode);
5913
5914 SelectionDAG &DAG = DCI.DAG;
5915 DebugLoc DL = LD->getDebugLoc();
5916 SDValue BasePtr = LD->getBasePtr();
5917 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
5918 LD->getPointerInfo(), LD->isVolatile(),
5919 LD->isNonTemporal(), LD->getAlignment());
5920
5921 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
5922 DAG.getConstant(4, MVT::i32));
5923 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
5924 LD->getPointerInfo(), LD->isVolatile(),
5925 LD->isNonTemporal(),
5926 std::min(4U, LD->getAlignment() / 2));
5927
5928 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
5929 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
5930 DCI.RemoveFromWorklist(LD);
5931 DAG.DeleteNode(LD);
5932 return Result;
5933 }
5934
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005935 return SDValue();
5936}
5937
5938/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
5939/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
5940static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
5941 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
5942 SDValue Op0 = N->getOperand(0);
5943 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005944 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005945 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005946 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005947 Op1 = Op1.getOperand(0);
5948 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
5949 Op0.getNode() == Op1.getNode() &&
5950 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005951 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005952 N->getValueType(0), Op0.getOperand(0));
5953 return SDValue();
5954}
5955
Bob Wilson31600902010-12-21 06:43:19 +00005956/// PerformSTORECombine - Target-specific dag combine xforms for
5957/// ISD::STORE.
5958static SDValue PerformSTORECombine(SDNode *N,
5959 TargetLowering::DAGCombinerInfo &DCI) {
5960 // Bitcast an i64 store extracted from a vector to f64.
5961 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5962 StoreSDNode *St = cast<StoreSDNode>(N);
5963 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00005964 if (!ISD::isNormalStore(St) || St->isVolatile())
5965 return SDValue();
5966
5967 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
5968 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
5969 SelectionDAG &DAG = DCI.DAG;
5970 DebugLoc DL = St->getDebugLoc();
5971 SDValue BasePtr = St->getBasePtr();
5972 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
5973 StVal.getNode()->getOperand(0), BasePtr,
5974 St->getPointerInfo(), St->isVolatile(),
5975 St->isNonTemporal(), St->getAlignment());
5976
5977 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
5978 DAG.getConstant(4, MVT::i32));
5979 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
5980 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
5981 St->isNonTemporal(),
5982 std::min(4U, St->getAlignment() / 2));
5983 }
5984
5985 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00005986 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5987 return SDValue();
5988
5989 SelectionDAG &DAG = DCI.DAG;
5990 DebugLoc dl = StVal.getDebugLoc();
5991 SDValue IntVec = StVal.getOperand(0);
5992 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5993 IntVec.getValueType().getVectorNumElements());
5994 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
5995 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5996 Vec, StVal.getOperand(1));
5997 dl = N->getDebugLoc();
5998 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
5999 // Make the DAGCombiner fold the bitcasts.
6000 DCI.AddToWorklist(Vec.getNode());
6001 DCI.AddToWorklist(ExtElt.getNode());
6002 DCI.AddToWorklist(V.getNode());
6003 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
6004 St->getPointerInfo(), St->isVolatile(),
6005 St->isNonTemporal(), St->getAlignment(),
6006 St->getTBAAInfo());
6007}
6008
6009/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
6010/// are normal, non-volatile loads. If so, it is profitable to bitcast an
6011/// i64 vector to have f64 elements, since the value can then be loaded
6012/// directly into a VFP register.
6013static bool hasNormalLoadOperand(SDNode *N) {
6014 unsigned NumElts = N->getValueType(0).getVectorNumElements();
6015 for (unsigned i = 0; i < NumElts; ++i) {
6016 SDNode *Elt = N->getOperand(i).getNode();
6017 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
6018 return true;
6019 }
6020 return false;
6021}
6022
Bob Wilson75f02882010-09-17 22:59:05 +00006023/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
6024/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00006025static SDValue PerformBUILD_VECTORCombine(SDNode *N,
6026 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00006027 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
6028 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
6029 // into a pair of GPRs, which is fine when the value is used as a scalar,
6030 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00006031 SelectionDAG &DAG = DCI.DAG;
6032 if (N->getNumOperands() == 2) {
6033 SDValue RV = PerformVMOVDRRCombine(N, DAG);
6034 if (RV.getNode())
6035 return RV;
6036 }
Bob Wilson75f02882010-09-17 22:59:05 +00006037
Bob Wilson31600902010-12-21 06:43:19 +00006038 // Load i64 elements as f64 values so that type legalization does not split
6039 // them up into i32 values.
6040 EVT VT = N->getValueType(0);
6041 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
6042 return SDValue();
6043 DebugLoc dl = N->getDebugLoc();
6044 SmallVector<SDValue, 8> Ops;
6045 unsigned NumElts = VT.getVectorNumElements();
6046 for (unsigned i = 0; i < NumElts; ++i) {
6047 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
6048 Ops.push_back(V);
6049 // Make the DAGCombiner fold the bitcast.
6050 DCI.AddToWorklist(V.getNode());
6051 }
6052 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
6053 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
6054 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
6055}
6056
6057/// PerformInsertEltCombine - Target-specific dag combine xforms for
6058/// ISD::INSERT_VECTOR_ELT.
6059static SDValue PerformInsertEltCombine(SDNode *N,
6060 TargetLowering::DAGCombinerInfo &DCI) {
6061 // Bitcast an i64 load inserted into a vector to f64.
6062 // Otherwise, the i64 value will be legalized to a pair of i32 values.
6063 EVT VT = N->getValueType(0);
6064 SDNode *Elt = N->getOperand(1).getNode();
6065 if (VT.getVectorElementType() != MVT::i64 ||
6066 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
6067 return SDValue();
6068
6069 SelectionDAG &DAG = DCI.DAG;
6070 DebugLoc dl = N->getDebugLoc();
6071 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
6072 VT.getVectorNumElements());
6073 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
6074 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
6075 // Make the DAGCombiner fold the bitcasts.
6076 DCI.AddToWorklist(Vec.getNode());
6077 DCI.AddToWorklist(V.getNode());
6078 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
6079 Vec, V, N->getOperand(2));
6080 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00006081}
6082
Bob Wilsonf20700c2010-10-27 20:38:28 +00006083/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
6084/// ISD::VECTOR_SHUFFLE.
6085static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
6086 // The LLVM shufflevector instruction does not require the shuffle mask
6087 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
6088 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
6089 // operands do not match the mask length, they are extended by concatenating
6090 // them with undef vectors. That is probably the right thing for other
6091 // targets, but for NEON it is better to concatenate two double-register
6092 // size vector operands into a single quad-register size vector. Do that
6093 // transformation here:
6094 // shuffle(concat(v1, undef), concat(v2, undef)) ->
6095 // shuffle(concat(v1, v2), undef)
6096 SDValue Op0 = N->getOperand(0);
6097 SDValue Op1 = N->getOperand(1);
6098 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
6099 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
6100 Op0.getNumOperands() != 2 ||
6101 Op1.getNumOperands() != 2)
6102 return SDValue();
6103 SDValue Concat0Op1 = Op0.getOperand(1);
6104 SDValue Concat1Op1 = Op1.getOperand(1);
6105 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
6106 Concat1Op1.getOpcode() != ISD::UNDEF)
6107 return SDValue();
6108 // Skip the transformation if any of the types are illegal.
6109 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6110 EVT VT = N->getValueType(0);
6111 if (!TLI.isTypeLegal(VT) ||
6112 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
6113 !TLI.isTypeLegal(Concat1Op1.getValueType()))
6114 return SDValue();
6115
6116 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
6117 Op0.getOperand(0), Op1.getOperand(0));
6118 // Translate the shuffle mask.
6119 SmallVector<int, 16> NewMask;
6120 unsigned NumElts = VT.getVectorNumElements();
6121 unsigned HalfElts = NumElts/2;
6122 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
6123 for (unsigned n = 0; n < NumElts; ++n) {
6124 int MaskElt = SVN->getMaskElt(n);
6125 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006126 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00006127 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006128 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00006129 NewElt = HalfElts + MaskElt - NumElts;
6130 NewMask.push_back(NewElt);
6131 }
6132 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
6133 DAG.getUNDEF(VT), NewMask.data());
6134}
6135
Bob Wilson1c3ef902011-02-07 17:43:21 +00006136/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
6137/// NEON load/store intrinsics to merge base address updates.
6138static SDValue CombineBaseUpdate(SDNode *N,
6139 TargetLowering::DAGCombinerInfo &DCI) {
6140 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6141 return SDValue();
6142
6143 SelectionDAG &DAG = DCI.DAG;
6144 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
6145 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
6146 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
6147 SDValue Addr = N->getOperand(AddrOpIdx);
6148
6149 // Search for a use of the address operand that is an increment.
6150 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
6151 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
6152 SDNode *User = *UI;
6153 if (User->getOpcode() != ISD::ADD ||
6154 UI.getUse().getResNo() != Addr.getResNo())
6155 continue;
6156
6157 // Check that the add is independent of the load/store. Otherwise, folding
6158 // it would create a cycle.
6159 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
6160 continue;
6161
6162 // Find the new opcode for the updating load/store.
6163 bool isLoad = true;
6164 bool isLaneOp = false;
6165 unsigned NewOpc = 0;
6166 unsigned NumVecs = 0;
6167 if (isIntrinsic) {
6168 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
6169 switch (IntNo) {
6170 default: assert(0 && "unexpected intrinsic for Neon base update");
6171 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
6172 NumVecs = 1; break;
6173 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
6174 NumVecs = 2; break;
6175 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
6176 NumVecs = 3; break;
6177 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
6178 NumVecs = 4; break;
6179 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
6180 NumVecs = 2; isLaneOp = true; break;
6181 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
6182 NumVecs = 3; isLaneOp = true; break;
6183 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
6184 NumVecs = 4; isLaneOp = true; break;
6185 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
6186 NumVecs = 1; isLoad = false; break;
6187 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
6188 NumVecs = 2; isLoad = false; break;
6189 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
6190 NumVecs = 3; isLoad = false; break;
6191 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
6192 NumVecs = 4; isLoad = false; break;
6193 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
6194 NumVecs = 2; isLoad = false; isLaneOp = true; break;
6195 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
6196 NumVecs = 3; isLoad = false; isLaneOp = true; break;
6197 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
6198 NumVecs = 4; isLoad = false; isLaneOp = true; break;
6199 }
6200 } else {
6201 isLaneOp = true;
6202 switch (N->getOpcode()) {
6203 default: assert(0 && "unexpected opcode for Neon base update");
6204 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
6205 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
6206 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
6207 }
6208 }
6209
6210 // Find the size of memory referenced by the load/store.
6211 EVT VecTy;
6212 if (isLoad)
6213 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00006214 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00006215 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
6216 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
6217 if (isLaneOp)
6218 NumBytes /= VecTy.getVectorNumElements();
6219
6220 // If the increment is a constant, it must match the memory ref size.
6221 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
6222 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
6223 uint64_t IncVal = CInc->getZExtValue();
6224 if (IncVal != NumBytes)
6225 continue;
6226 } else if (NumBytes >= 3 * 16) {
6227 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
6228 // separate instructions that make it harder to use a non-constant update.
6229 continue;
6230 }
6231
6232 // Create the new updating load/store node.
6233 EVT Tys[6];
6234 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
6235 unsigned n;
6236 for (n = 0; n < NumResultVecs; ++n)
6237 Tys[n] = VecTy;
6238 Tys[n++] = MVT::i32;
6239 Tys[n] = MVT::Other;
6240 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
6241 SmallVector<SDValue, 8> Ops;
6242 Ops.push_back(N->getOperand(0)); // incoming chain
6243 Ops.push_back(N->getOperand(AddrOpIdx));
6244 Ops.push_back(Inc);
6245 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
6246 Ops.push_back(N->getOperand(i));
6247 }
6248 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
6249 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
6250 Ops.data(), Ops.size(),
6251 MemInt->getMemoryVT(),
6252 MemInt->getMemOperand());
6253
6254 // Update the uses.
6255 std::vector<SDValue> NewResults;
6256 for (unsigned i = 0; i < NumResultVecs; ++i) {
6257 NewResults.push_back(SDValue(UpdN.getNode(), i));
6258 }
6259 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
6260 DCI.CombineTo(N, NewResults);
6261 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
6262
6263 break;
Owen Anderson76706012011-04-05 21:48:57 +00006264 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00006265 return SDValue();
6266}
6267
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006268/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
6269/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
6270/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
6271/// return true.
6272static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
6273 SelectionDAG &DAG = DCI.DAG;
6274 EVT VT = N->getValueType(0);
6275 // vldN-dup instructions only support 64-bit vectors for N > 1.
6276 if (!VT.is64BitVector())
6277 return false;
6278
6279 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
6280 SDNode *VLD = N->getOperand(0).getNode();
6281 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
6282 return false;
6283 unsigned NumVecs = 0;
6284 unsigned NewOpc = 0;
6285 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
6286 if (IntNo == Intrinsic::arm_neon_vld2lane) {
6287 NumVecs = 2;
6288 NewOpc = ARMISD::VLD2DUP;
6289 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
6290 NumVecs = 3;
6291 NewOpc = ARMISD::VLD3DUP;
6292 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
6293 NumVecs = 4;
6294 NewOpc = ARMISD::VLD4DUP;
6295 } else {
6296 return false;
6297 }
6298
6299 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
6300 // numbers match the load.
6301 unsigned VLDLaneNo =
6302 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
6303 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6304 UI != UE; ++UI) {
6305 // Ignore uses of the chain result.
6306 if (UI.getUse().getResNo() == NumVecs)
6307 continue;
6308 SDNode *User = *UI;
6309 if (User->getOpcode() != ARMISD::VDUPLANE ||
6310 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
6311 return false;
6312 }
6313
6314 // Create the vldN-dup node.
6315 EVT Tys[5];
6316 unsigned n;
6317 for (n = 0; n < NumVecs; ++n)
6318 Tys[n] = VT;
6319 Tys[n] = MVT::Other;
6320 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
6321 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
6322 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
6323 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
6324 Ops, 2, VLDMemInt->getMemoryVT(),
6325 VLDMemInt->getMemOperand());
6326
6327 // Update the uses.
6328 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6329 UI != UE; ++UI) {
6330 unsigned ResNo = UI.getUse().getResNo();
6331 // Ignore uses of the chain result.
6332 if (ResNo == NumVecs)
6333 continue;
6334 SDNode *User = *UI;
6335 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
6336 }
6337
6338 // Now the vldN-lane intrinsic is dead except for its chain result.
6339 // Update uses of the chain.
6340 std::vector<SDValue> VLDDupResults;
6341 for (unsigned n = 0; n < NumVecs; ++n)
6342 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
6343 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
6344 DCI.CombineTo(VLD, VLDDupResults);
6345
6346 return true;
6347}
6348
Bob Wilson9e82bf12010-07-14 01:22:12 +00006349/// PerformVDUPLANECombine - Target-specific dag combine xforms for
6350/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006351static SDValue PerformVDUPLANECombine(SDNode *N,
6352 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00006353 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006354
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006355 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
6356 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
6357 if (CombineVLDDUP(N, DCI))
6358 return SDValue(N, 0);
6359
6360 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
6361 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006362 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006363 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00006364 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006365 return SDValue();
6366
6367 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
6368 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
6369 // The canonical VMOV for a zero vector uses a 32-bit element size.
6370 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6371 unsigned EltBits;
6372 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
6373 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006374 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006375 if (EltSize > VT.getVectorElementType().getSizeInBits())
6376 return SDValue();
6377
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006378 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006379}
6380
Bob Wilson5bafff32009-06-22 23:27:02 +00006381/// getVShiftImm - Check if this is a valid build_vector for the immediate
6382/// operand of a vector shift operation, where all the elements of the
6383/// build_vector must have the same constant integer value.
6384static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
6385 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006386 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00006387 Op = Op.getOperand(0);
6388 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
6389 APInt SplatBits, SplatUndef;
6390 unsigned SplatBitSize;
6391 bool HasAnyUndefs;
6392 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
6393 HasAnyUndefs, ElementBits) ||
6394 SplatBitSize > ElementBits)
6395 return false;
6396 Cnt = SplatBits.getSExtValue();
6397 return true;
6398}
6399
6400/// isVShiftLImm - Check if this is a valid build_vector for the immediate
6401/// operand of a vector shift left operation. That value must be in the range:
6402/// 0 <= Value < ElementBits for a left shift; or
6403/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006404static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006405 assert(VT.isVector() && "vector shift count is not a vector type");
6406 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6407 if (! getVShiftImm(Op, ElementBits, Cnt))
6408 return false;
6409 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
6410}
6411
6412/// isVShiftRImm - Check if this is a valid build_vector for the immediate
6413/// operand of a vector shift right operation. For a shift opcode, the value
6414/// is positive, but for an intrinsic the value count must be negative. The
6415/// absolute value must be in the range:
6416/// 1 <= |Value| <= ElementBits for a right shift; or
6417/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006418static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00006419 int64_t &Cnt) {
6420 assert(VT.isVector() && "vector shift count is not a vector type");
6421 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6422 if (! getVShiftImm(Op, ElementBits, Cnt))
6423 return false;
6424 if (isIntrinsic)
6425 Cnt = -Cnt;
6426 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
6427}
6428
6429/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
6430static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
6431 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
6432 switch (IntNo) {
6433 default:
6434 // Don't do anything for most intrinsics.
6435 break;
6436
6437 // Vector shifts: check for immediate versions and lower them.
6438 // Note: This is done during DAG combining instead of DAG legalizing because
6439 // the build_vectors for 64-bit vector element shift counts are generally
6440 // not legal, and it is hard to see their values after they get legalized to
6441 // loads from a constant pool.
6442 case Intrinsic::arm_neon_vshifts:
6443 case Intrinsic::arm_neon_vshiftu:
6444 case Intrinsic::arm_neon_vshiftls:
6445 case Intrinsic::arm_neon_vshiftlu:
6446 case Intrinsic::arm_neon_vshiftn:
6447 case Intrinsic::arm_neon_vrshifts:
6448 case Intrinsic::arm_neon_vrshiftu:
6449 case Intrinsic::arm_neon_vrshiftn:
6450 case Intrinsic::arm_neon_vqshifts:
6451 case Intrinsic::arm_neon_vqshiftu:
6452 case Intrinsic::arm_neon_vqshiftsu:
6453 case Intrinsic::arm_neon_vqshiftns:
6454 case Intrinsic::arm_neon_vqshiftnu:
6455 case Intrinsic::arm_neon_vqshiftnsu:
6456 case Intrinsic::arm_neon_vqrshiftns:
6457 case Intrinsic::arm_neon_vqrshiftnu:
6458 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00006459 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006460 int64_t Cnt;
6461 unsigned VShiftOpc = 0;
6462
6463 switch (IntNo) {
6464 case Intrinsic::arm_neon_vshifts:
6465 case Intrinsic::arm_neon_vshiftu:
6466 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
6467 VShiftOpc = ARMISD::VSHL;
6468 break;
6469 }
6470 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
6471 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
6472 ARMISD::VSHRs : ARMISD::VSHRu);
6473 break;
6474 }
6475 return SDValue();
6476
6477 case Intrinsic::arm_neon_vshiftls:
6478 case Intrinsic::arm_neon_vshiftlu:
6479 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
6480 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006481 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006482
6483 case Intrinsic::arm_neon_vrshifts:
6484 case Intrinsic::arm_neon_vrshiftu:
6485 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
6486 break;
6487 return SDValue();
6488
6489 case Intrinsic::arm_neon_vqshifts:
6490 case Intrinsic::arm_neon_vqshiftu:
6491 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6492 break;
6493 return SDValue();
6494
6495 case Intrinsic::arm_neon_vqshiftsu:
6496 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6497 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006498 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006499
6500 case Intrinsic::arm_neon_vshiftn:
6501 case Intrinsic::arm_neon_vrshiftn:
6502 case Intrinsic::arm_neon_vqshiftns:
6503 case Intrinsic::arm_neon_vqshiftnu:
6504 case Intrinsic::arm_neon_vqshiftnsu:
6505 case Intrinsic::arm_neon_vqrshiftns:
6506 case Intrinsic::arm_neon_vqrshiftnu:
6507 case Intrinsic::arm_neon_vqrshiftnsu:
6508 // Narrowing shifts require an immediate right shift.
6509 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
6510 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00006511 llvm_unreachable("invalid shift count for narrowing vector shift "
6512 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006513
6514 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006515 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00006516 }
6517
6518 switch (IntNo) {
6519 case Intrinsic::arm_neon_vshifts:
6520 case Intrinsic::arm_neon_vshiftu:
6521 // Opcode already set above.
6522 break;
6523 case Intrinsic::arm_neon_vshiftls:
6524 case Intrinsic::arm_neon_vshiftlu:
6525 if (Cnt == VT.getVectorElementType().getSizeInBits())
6526 VShiftOpc = ARMISD::VSHLLi;
6527 else
6528 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
6529 ARMISD::VSHLLs : ARMISD::VSHLLu);
6530 break;
6531 case Intrinsic::arm_neon_vshiftn:
6532 VShiftOpc = ARMISD::VSHRN; break;
6533 case Intrinsic::arm_neon_vrshifts:
6534 VShiftOpc = ARMISD::VRSHRs; break;
6535 case Intrinsic::arm_neon_vrshiftu:
6536 VShiftOpc = ARMISD::VRSHRu; break;
6537 case Intrinsic::arm_neon_vrshiftn:
6538 VShiftOpc = ARMISD::VRSHRN; break;
6539 case Intrinsic::arm_neon_vqshifts:
6540 VShiftOpc = ARMISD::VQSHLs; break;
6541 case Intrinsic::arm_neon_vqshiftu:
6542 VShiftOpc = ARMISD::VQSHLu; break;
6543 case Intrinsic::arm_neon_vqshiftsu:
6544 VShiftOpc = ARMISD::VQSHLsu; break;
6545 case Intrinsic::arm_neon_vqshiftns:
6546 VShiftOpc = ARMISD::VQSHRNs; break;
6547 case Intrinsic::arm_neon_vqshiftnu:
6548 VShiftOpc = ARMISD::VQSHRNu; break;
6549 case Intrinsic::arm_neon_vqshiftnsu:
6550 VShiftOpc = ARMISD::VQSHRNsu; break;
6551 case Intrinsic::arm_neon_vqrshiftns:
6552 VShiftOpc = ARMISD::VQRSHRNs; break;
6553 case Intrinsic::arm_neon_vqrshiftnu:
6554 VShiftOpc = ARMISD::VQRSHRNu; break;
6555 case Intrinsic::arm_neon_vqrshiftnsu:
6556 VShiftOpc = ARMISD::VQRSHRNsu; break;
6557 }
6558
6559 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006560 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006561 }
6562
6563 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00006564 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006565 int64_t Cnt;
6566 unsigned VShiftOpc = 0;
6567
6568 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
6569 VShiftOpc = ARMISD::VSLI;
6570 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
6571 VShiftOpc = ARMISD::VSRI;
6572 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006573 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006574 }
6575
6576 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
6577 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00006578 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006579 }
6580
6581 case Intrinsic::arm_neon_vqrshifts:
6582 case Intrinsic::arm_neon_vqrshiftu:
6583 // No immediate versions of these to check for.
6584 break;
6585 }
6586
6587 return SDValue();
6588}
6589
6590/// PerformShiftCombine - Checks for immediate versions of vector shifts and
6591/// lowers them. As with the vector shift intrinsics, this is done during DAG
6592/// combining instead of DAG legalizing because the build_vectors for 64-bit
6593/// vector element shift counts are generally not legal, and it is hard to see
6594/// their values after they get legalized to loads from a constant pool.
6595static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
6596 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00006597 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00006598
6599 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00006600 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6601 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00006602 return SDValue();
6603
6604 assert(ST->hasNEON() && "unexpected vector shift");
6605 int64_t Cnt;
6606
6607 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006608 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006609
6610 case ISD::SHL:
6611 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
6612 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006613 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006614 break;
6615
6616 case ISD::SRA:
6617 case ISD::SRL:
6618 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
6619 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
6620 ARMISD::VSHRs : ARMISD::VSHRu);
6621 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006622 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006623 }
6624 }
6625 return SDValue();
6626}
6627
6628/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
6629/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
6630static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
6631 const ARMSubtarget *ST) {
6632 SDValue N0 = N->getOperand(0);
6633
6634 // Check for sign- and zero-extensions of vector extract operations of 8-
6635 // and 16-bit vector elements. NEON supports these directly. They are
6636 // handled during DAG combining because type legalization will promote them
6637 // to 32-bit types and it is messy to recognize the operations after that.
6638 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
6639 SDValue Vec = N0.getOperand(0);
6640 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006641 EVT VT = N->getValueType(0);
6642 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006643 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6644
Owen Anderson825b72b2009-08-11 20:47:22 +00006645 if (VT == MVT::i32 &&
6646 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00006647 TLI.isTypeLegal(Vec.getValueType()) &&
6648 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006649
6650 unsigned Opc = 0;
6651 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006652 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006653 case ISD::SIGN_EXTEND:
6654 Opc = ARMISD::VGETLANEs;
6655 break;
6656 case ISD::ZERO_EXTEND:
6657 case ISD::ANY_EXTEND:
6658 Opc = ARMISD::VGETLANEu;
6659 break;
6660 }
6661 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
6662 }
6663 }
6664
6665 return SDValue();
6666}
6667
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006668/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
6669/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
6670static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
6671 const ARMSubtarget *ST) {
6672 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00006673 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006674 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
6675 // a NaN; only do the transformation when it matches that behavior.
6676
6677 // For now only do this when using NEON for FP operations; if using VFP, it
6678 // is not obvious that the benefit outweighs the cost of switching to the
6679 // NEON pipeline.
6680 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
6681 N->getValueType(0) != MVT::f32)
6682 return SDValue();
6683
6684 SDValue CondLHS = N->getOperand(0);
6685 SDValue CondRHS = N->getOperand(1);
6686 SDValue LHS = N->getOperand(2);
6687 SDValue RHS = N->getOperand(3);
6688 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
6689
6690 unsigned Opcode = 0;
6691 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00006692 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006693 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00006694 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006695 IsReversed = true ; // x CC y ? y : x
6696 } else {
6697 return SDValue();
6698 }
6699
Bob Wilsone742bb52010-02-24 22:15:53 +00006700 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006701 switch (CC) {
6702 default: break;
6703 case ISD::SETOLT:
6704 case ISD::SETOLE:
6705 case ISD::SETLT:
6706 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006707 case ISD::SETULT:
6708 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006709 // If LHS is NaN, an ordered comparison will be false and the result will
6710 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
6711 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6712 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
6713 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6714 break;
6715 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
6716 // will return -0, so vmin can only be used for unsafe math or if one of
6717 // the operands is known to be nonzero.
6718 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
6719 !UnsafeFPMath &&
6720 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6721 break;
6722 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006723 break;
6724
6725 case ISD::SETOGT:
6726 case ISD::SETOGE:
6727 case ISD::SETGT:
6728 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006729 case ISD::SETUGT:
6730 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006731 // If LHS is NaN, an ordered comparison will be false and the result will
6732 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
6733 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6734 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
6735 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6736 break;
6737 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
6738 // will return +0, so vmax can only be used for unsafe math or if one of
6739 // the operands is known to be nonzero.
6740 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
6741 !UnsafeFPMath &&
6742 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6743 break;
6744 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006745 break;
6746 }
6747
6748 if (!Opcode)
6749 return SDValue();
6750 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
6751}
6752
Dan Gohman475871a2008-07-27 21:46:04 +00006753SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006754 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006755 switch (N->getOpcode()) {
6756 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006757 case ISD::ADD: return PerformADDCombine(N, DCI);
6758 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006759 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006760 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00006761 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00006762 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00006763 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006764 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00006765 case ISD::STORE: return PerformSTORECombine(N, DCI);
6766 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
6767 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00006768 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006769 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006770 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00006771 case ISD::SHL:
6772 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006773 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00006774 case ISD::SIGN_EXTEND:
6775 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006776 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
6777 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Bob Wilson1c3ef902011-02-07 17:43:21 +00006778 case ARMISD::VLD2DUP:
6779 case ARMISD::VLD3DUP:
6780 case ARMISD::VLD4DUP:
6781 return CombineBaseUpdate(N, DCI);
6782 case ISD::INTRINSIC_VOID:
6783 case ISD::INTRINSIC_W_CHAIN:
6784 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
6785 case Intrinsic::arm_neon_vld1:
6786 case Intrinsic::arm_neon_vld2:
6787 case Intrinsic::arm_neon_vld3:
6788 case Intrinsic::arm_neon_vld4:
6789 case Intrinsic::arm_neon_vld2lane:
6790 case Intrinsic::arm_neon_vld3lane:
6791 case Intrinsic::arm_neon_vld4lane:
6792 case Intrinsic::arm_neon_vst1:
6793 case Intrinsic::arm_neon_vst2:
6794 case Intrinsic::arm_neon_vst3:
6795 case Intrinsic::arm_neon_vst4:
6796 case Intrinsic::arm_neon_vst2lane:
6797 case Intrinsic::arm_neon_vst3lane:
6798 case Intrinsic::arm_neon_vst4lane:
6799 return CombineBaseUpdate(N, DCI);
6800 default: break;
6801 }
6802 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006803 }
Dan Gohman475871a2008-07-27 21:46:04 +00006804 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006805}
6806
Evan Cheng31959b12011-02-02 01:06:55 +00006807bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
6808 EVT VT) const {
6809 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
6810}
6811
Bill Wendlingaf566342009-08-15 21:21:19 +00006812bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00006813 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00006814 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00006815
6816 switch (VT.getSimpleVT().SimpleTy) {
6817 default:
6818 return false;
6819 case MVT::i8:
6820 case MVT::i16:
6821 case MVT::i32:
6822 return true;
6823 // FIXME: VLD1 etc with standard alignment is legal.
6824 }
6825}
6826
Evan Chenge6c835f2009-08-14 20:09:37 +00006827static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
6828 if (V < 0)
6829 return false;
6830
6831 unsigned Scale = 1;
6832 switch (VT.getSimpleVT().SimpleTy) {
6833 default: return false;
6834 case MVT::i1:
6835 case MVT::i8:
6836 // Scale == 1;
6837 break;
6838 case MVT::i16:
6839 // Scale == 2;
6840 Scale = 2;
6841 break;
6842 case MVT::i32:
6843 // Scale == 4;
6844 Scale = 4;
6845 break;
6846 }
6847
6848 if ((V & (Scale - 1)) != 0)
6849 return false;
6850 V /= Scale;
6851 return V == (V & ((1LL << 5) - 1));
6852}
6853
6854static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
6855 const ARMSubtarget *Subtarget) {
6856 bool isNeg = false;
6857 if (V < 0) {
6858 isNeg = true;
6859 V = - V;
6860 }
6861
6862 switch (VT.getSimpleVT().SimpleTy) {
6863 default: return false;
6864 case MVT::i1:
6865 case MVT::i8:
6866 case MVT::i16:
6867 case MVT::i32:
6868 // + imm12 or - imm8
6869 if (isNeg)
6870 return V == (V & ((1LL << 8) - 1));
6871 return V == (V & ((1LL << 12) - 1));
6872 case MVT::f32:
6873 case MVT::f64:
6874 // Same as ARM mode. FIXME: NEON?
6875 if (!Subtarget->hasVFP2())
6876 return false;
6877 if ((V & 3) != 0)
6878 return false;
6879 V >>= 2;
6880 return V == (V & ((1LL << 8) - 1));
6881 }
6882}
6883
Evan Chengb01fad62007-03-12 23:30:29 +00006884/// isLegalAddressImmediate - Return true if the integer value can be used
6885/// as the offset of the target addressing mode for load / store of the
6886/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00006887static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006888 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00006889 if (V == 0)
6890 return true;
6891
Evan Cheng65011532009-03-09 19:15:00 +00006892 if (!VT.isSimple())
6893 return false;
6894
Evan Chenge6c835f2009-08-14 20:09:37 +00006895 if (Subtarget->isThumb1Only())
6896 return isLegalT1AddressImmediate(V, VT);
6897 else if (Subtarget->isThumb2())
6898 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00006899
Evan Chenge6c835f2009-08-14 20:09:37 +00006900 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00006901 if (V < 0)
6902 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00006903 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00006904 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006905 case MVT::i1:
6906 case MVT::i8:
6907 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00006908 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006909 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006910 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00006911 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006912 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006913 case MVT::f32:
6914 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00006915 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00006916 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00006917 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00006918 return false;
6919 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006920 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00006921 }
Evan Chenga8e29892007-01-19 07:51:42 +00006922}
6923
Evan Chenge6c835f2009-08-14 20:09:37 +00006924bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
6925 EVT VT) const {
6926 int Scale = AM.Scale;
6927 if (Scale < 0)
6928 return false;
6929
6930 switch (VT.getSimpleVT().SimpleTy) {
6931 default: return false;
6932 case MVT::i1:
6933 case MVT::i8:
6934 case MVT::i16:
6935 case MVT::i32:
6936 if (Scale == 1)
6937 return true;
6938 // r + r << imm
6939 Scale = Scale & ~1;
6940 return Scale == 2 || Scale == 4 || Scale == 8;
6941 case MVT::i64:
6942 // r + r
6943 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
6944 return true;
6945 return false;
6946 case MVT::isVoid:
6947 // Note, we allow "void" uses (basically, uses that aren't loads or
6948 // stores), because arm allows folding a scale into many arithmetic
6949 // operations. This should be made more precise and revisited later.
6950
6951 // Allow r << imm, but the imm has to be a multiple of two.
6952 if (Scale & 1) return false;
6953 return isPowerOf2_32(Scale);
6954 }
6955}
6956
Chris Lattner37caf8c2007-04-09 23:33:39 +00006957/// isLegalAddressingMode - Return true if the addressing mode represented
6958/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006959bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006960 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006961 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00006962 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00006963 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006964
Chris Lattner37caf8c2007-04-09 23:33:39 +00006965 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006966 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006967 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006968
Chris Lattner37caf8c2007-04-09 23:33:39 +00006969 switch (AM.Scale) {
6970 case 0: // no scale reg, must be "r+i" or "r", or "i".
6971 break;
6972 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00006973 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00006974 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006975 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00006976 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006977 // ARM doesn't support any R+R*scale+imm addr modes.
6978 if (AM.BaseOffs)
6979 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006980
Bob Wilson2c7dab12009-04-08 17:55:28 +00006981 if (!VT.isSimple())
6982 return false;
6983
Evan Chenge6c835f2009-08-14 20:09:37 +00006984 if (Subtarget->isThumb2())
6985 return isLegalT2ScaledAddressingMode(AM, VT);
6986
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006987 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00006988 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00006989 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006990 case MVT::i1:
6991 case MVT::i8:
6992 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006993 if (Scale < 0) Scale = -Scale;
6994 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006995 return true;
6996 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00006997 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006998 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00006999 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00007000 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007001 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007002 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00007003 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007004
Owen Anderson825b72b2009-08-11 20:47:22 +00007005 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00007006 // Note, we allow "void" uses (basically, uses that aren't loads or
7007 // stores), because arm allows folding a scale into many arithmetic
7008 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007009
Chris Lattner37caf8c2007-04-09 23:33:39 +00007010 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00007011 if (Scale & 1) return false;
7012 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00007013 }
7014 break;
Evan Chengb01fad62007-03-12 23:30:29 +00007015 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00007016 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00007017}
7018
Evan Cheng77e47512009-11-11 19:05:52 +00007019/// isLegalICmpImmediate - Return true if the specified immediate is legal
7020/// icmp immediate, that is the target has icmp instructions which can compare
7021/// a register against the immediate without having to materialize the
7022/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00007023bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00007024 if (!Subtarget->isThumb())
7025 return ARM_AM::getSOImmVal(Imm) != -1;
7026 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00007027 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00007028 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00007029}
7030
Dan Gohmancca82142011-05-03 00:46:49 +00007031/// isLegalAddImmediate - Return true if the specified immediate is legal
7032/// add immediate, that is the target has add instructions which can add
7033/// a register with the immediate without having to materialize the
7034/// immediate into a register.
7035bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
7036 return ARM_AM::getSOImmVal(Imm) != -1;
7037}
7038
Owen Andersone50ed302009-08-10 22:56:29 +00007039static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007040 bool isSEXTLoad, SDValue &Base,
7041 SDValue &Offset, bool &isInc,
7042 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00007043 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7044 return false;
7045
Owen Anderson825b72b2009-08-11 20:47:22 +00007046 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00007047 // AddressingMode 3
7048 Base = Ptr->getOperand(0);
7049 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007050 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007051 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007052 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007053 isInc = false;
7054 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7055 return true;
7056 }
7057 }
7058 isInc = (Ptr->getOpcode() == ISD::ADD);
7059 Offset = Ptr->getOperand(1);
7060 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00007061 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00007062 // AddressingMode 2
7063 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007064 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007065 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007066 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007067 isInc = false;
7068 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7069 Base = Ptr->getOperand(0);
7070 return true;
7071 }
7072 }
7073
7074 if (Ptr->getOpcode() == ISD::ADD) {
7075 isInc = true;
7076 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
7077 if (ShOpcVal != ARM_AM::no_shift) {
7078 Base = Ptr->getOperand(1);
7079 Offset = Ptr->getOperand(0);
7080 } else {
7081 Base = Ptr->getOperand(0);
7082 Offset = Ptr->getOperand(1);
7083 }
7084 return true;
7085 }
7086
7087 isInc = (Ptr->getOpcode() == ISD::ADD);
7088 Base = Ptr->getOperand(0);
7089 Offset = Ptr->getOperand(1);
7090 return true;
7091 }
7092
Jim Grosbache5165492009-11-09 00:11:35 +00007093 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00007094 return false;
7095}
7096
Owen Andersone50ed302009-08-10 22:56:29 +00007097static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007098 bool isSEXTLoad, SDValue &Base,
7099 SDValue &Offset, bool &isInc,
7100 SelectionDAG &DAG) {
7101 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7102 return false;
7103
7104 Base = Ptr->getOperand(0);
7105 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
7106 int RHSC = (int)RHS->getZExtValue();
7107 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
7108 assert(Ptr->getOpcode() == ISD::ADD);
7109 isInc = false;
7110 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7111 return true;
7112 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
7113 isInc = Ptr->getOpcode() == ISD::ADD;
7114 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
7115 return true;
7116 }
7117 }
7118
7119 return false;
7120}
7121
Evan Chenga8e29892007-01-19 07:51:42 +00007122/// getPreIndexedAddressParts - returns true by value, base pointer and
7123/// offset pointer and addressing mode by reference if the node's address
7124/// can be legally represented as pre-indexed load / store address.
7125bool
Dan Gohman475871a2008-07-27 21:46:04 +00007126ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
7127 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007128 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007129 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007130 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007131 return false;
7132
Owen Andersone50ed302009-08-10 22:56:29 +00007133 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007134 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007135 bool isSEXTLoad = false;
7136 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
7137 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007138 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007139 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7140 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
7141 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007142 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007143 } else
7144 return false;
7145
7146 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007147 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007148 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007149 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
7150 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007151 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007152 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00007153 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00007154 if (!isLegal)
7155 return false;
7156
7157 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
7158 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007159}
7160
7161/// getPostIndexedAddressParts - returns true by value, base pointer and
7162/// offset pointer and addressing mode by reference if this node can be
7163/// combined with a load / store to form a post-indexed load / store.
7164bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00007165 SDValue &Base,
7166 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007167 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007168 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007169 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007170 return false;
7171
Owen Andersone50ed302009-08-10 22:56:29 +00007172 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007173 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007174 bool isSEXTLoad = false;
7175 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007176 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007177 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007178 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7179 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007180 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007181 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007182 } else
7183 return false;
7184
7185 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007186 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007187 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007188 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00007189 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007190 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007191 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
7192 isInc, DAG);
7193 if (!isLegal)
7194 return false;
7195
Evan Cheng28dad2a2010-05-18 21:31:17 +00007196 if (Ptr != Base) {
7197 // Swap base ptr and offset to catch more post-index load / store when
7198 // it's legal. In Thumb2 mode, offset must be an immediate.
7199 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
7200 !Subtarget->isThumb2())
7201 std::swap(Base, Offset);
7202
7203 // Post-indexed load / store update the base pointer.
7204 if (Ptr != Base)
7205 return false;
7206 }
7207
Evan Chenge88d5ce2009-07-02 07:28:31 +00007208 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
7209 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007210}
7211
Dan Gohman475871a2008-07-27 21:46:04 +00007212void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007213 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00007214 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007215 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007216 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00007217 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007218 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007219 switch (Op.getOpcode()) {
7220 default: break;
7221 case ARMISD::CMOV: {
7222 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00007223 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007224 if (KnownZero == 0 && KnownOne == 0) return;
7225
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007226 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00007227 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
7228 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007229 KnownZero &= KnownZeroRHS;
7230 KnownOne &= KnownOneRHS;
7231 return;
7232 }
7233 }
7234}
7235
7236//===----------------------------------------------------------------------===//
7237// ARM Inline Assembly Support
7238//===----------------------------------------------------------------------===//
7239
Evan Cheng55d42002011-01-08 01:24:27 +00007240bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
7241 // Looking for "rev" which is V6+.
7242 if (!Subtarget->hasV6Ops())
7243 return false;
7244
7245 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
7246 std::string AsmStr = IA->getAsmString();
7247 SmallVector<StringRef, 4> AsmPieces;
7248 SplitString(AsmStr, AsmPieces, ";\n");
7249
7250 switch (AsmPieces.size()) {
7251 default: return false;
7252 case 1:
7253 AsmStr = AsmPieces[0];
7254 AsmPieces.clear();
7255 SplitString(AsmStr, AsmPieces, " \t,");
7256
7257 // rev $0, $1
7258 if (AsmPieces.size() == 3 &&
7259 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
7260 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
7261 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
7262 if (Ty && Ty->getBitWidth() == 32)
7263 return IntrinsicLowering::LowerToByteSwap(CI);
7264 }
7265 break;
7266 }
7267
7268 return false;
7269}
7270
Evan Chenga8e29892007-01-19 07:51:42 +00007271/// getConstraintType - Given a constraint letter, return the type of
7272/// constraint it is for this target.
7273ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00007274ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
7275 if (Constraint.size() == 1) {
7276 switch (Constraint[0]) {
7277 default: break;
7278 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007279 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00007280 }
Eric Christophercf714d42011-06-03 17:24:37 +00007281 } else {
7282 if (Constraint == "Uv")
7283 return C_Memory;
Evan Chenga8e29892007-01-19 07:51:42 +00007284 }
Chris Lattner4234f572007-03-25 02:14:49 +00007285 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00007286}
7287
John Thompson44ab89e2010-10-29 17:29:13 +00007288/// Examine constraint type and operand type and determine a weight value.
7289/// This object must already have been set up with the operand type
7290/// and the current alternative constraint selected.
7291TargetLowering::ConstraintWeight
7292ARMTargetLowering::getSingleConstraintMatchWeight(
7293 AsmOperandInfo &info, const char *constraint) const {
7294 ConstraintWeight weight = CW_Invalid;
7295 Value *CallOperandVal = info.CallOperandVal;
7296 // If we don't have a value, we can't do a match,
7297 // but allow it at the lowest weight.
7298 if (CallOperandVal == NULL)
7299 return CW_Default;
7300 const Type *type = CallOperandVal->getType();
7301 // Look at the constraint type.
7302 switch (*constraint) {
7303 default:
7304 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
7305 break;
7306 case 'l':
7307 if (type->isIntegerTy()) {
7308 if (Subtarget->isThumb())
7309 weight = CW_SpecificReg;
7310 else
7311 weight = CW_Register;
7312 }
7313 break;
7314 case 'w':
7315 if (type->isFloatingPointTy())
7316 weight = CW_Register;
7317 break;
7318 }
7319 return weight;
7320}
7321
Bob Wilson2dc4f542009-03-20 22:42:55 +00007322std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00007323ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007324 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007325 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007326 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00007327 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007328 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007329 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007330 return std::make_pair(0U, ARM::tGPRRegisterClass);
7331 else
7332 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007333 case 'r':
7334 return std::make_pair(0U, ARM::GPRRegisterClass);
7335 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007336 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007337 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00007338 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007339 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00007340 if (VT.getSizeInBits() == 128)
7341 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007342 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007343 }
7344 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007345 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00007346 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007347
Evan Chenga8e29892007-01-19 07:51:42 +00007348 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7349}
7350
7351std::vector<unsigned> ARMTargetLowering::
7352getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007353 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007354 if (Constraint.size() != 1)
7355 return std::vector<unsigned>();
7356
7357 switch (Constraint[0]) { // GCC ARM Constraint Letters
7358 default: break;
7359 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007360 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
7361 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
7362 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007363 case 'r':
7364 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
7365 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
7366 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
7367 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007368 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007369 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007370 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
7371 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
7372 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
7373 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
7374 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
7375 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
7376 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
7377 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00007378 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007379 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
7380 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
7381 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
7382 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00007383 if (VT.getSizeInBits() == 128)
7384 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
7385 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007386 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007387 }
7388
7389 return std::vector<unsigned>();
7390}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007391
7392/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7393/// vector. If it is invalid, don't add anything to Ops.
7394void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00007395 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007396 std::vector<SDValue>&Ops,
7397 SelectionDAG &DAG) const {
7398 SDValue Result(0, 0);
7399
Eric Christopher100c8332011-06-02 23:16:42 +00007400 // Currently only support length 1 constraints.
7401 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00007402
Eric Christopher100c8332011-06-02 23:16:42 +00007403 char ConstraintLetter = Constraint[0];
7404 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007405 default: break;
7406 case 'I': case 'J': case 'K': case 'L':
7407 case 'M': case 'N': case 'O':
7408 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
7409 if (!C)
7410 return;
7411
7412 int64_t CVal64 = C->getSExtValue();
7413 int CVal = (int) CVal64;
7414 // None of these constraints allow values larger than 32 bits. Check
7415 // that the value fits in an int.
7416 if (CVal != CVal64)
7417 return;
7418
Eric Christopher100c8332011-06-02 23:16:42 +00007419 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007420 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007421 if (Subtarget->isThumb1Only()) {
7422 // This must be a constant between 0 and 255, for ADD
7423 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007424 if (CVal >= 0 && CVal <= 255)
7425 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007426 } else if (Subtarget->isThumb2()) {
7427 // A constant that can be used as an immediate value in a
7428 // data-processing instruction.
7429 if (ARM_AM::getT2SOImmVal(CVal) != -1)
7430 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007431 } else {
7432 // A constant that can be used as an immediate value in a
7433 // data-processing instruction.
7434 if (ARM_AM::getSOImmVal(CVal) != -1)
7435 break;
7436 }
7437 return;
7438
7439 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007440 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007441 // This must be a constant between -255 and -1, for negated ADD
7442 // immediates. This can be used in GCC with an "n" modifier that
7443 // prints the negated value, for use with SUB instructions. It is
7444 // not useful otherwise but is implemented for compatibility.
7445 if (CVal >= -255 && CVal <= -1)
7446 break;
7447 } else {
7448 // This must be a constant between -4095 and 4095. It is not clear
7449 // what this constraint is intended for. Implemented for
7450 // compatibility with GCC.
7451 if (CVal >= -4095 && CVal <= 4095)
7452 break;
7453 }
7454 return;
7455
7456 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007457 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007458 // A 32-bit value where only one byte has a nonzero value. Exclude
7459 // zero to match GCC. This constraint is used by GCC internally for
7460 // constants that can be loaded with a move/shift combination.
7461 // It is not useful otherwise but is implemented for compatibility.
7462 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
7463 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007464 } else if (Subtarget->isThumb2()) {
7465 // A constant whose bitwise inverse can be used as an immediate
7466 // value in a data-processing instruction. This can be used in GCC
7467 // with a "B" modifier that prints the inverted value, for use with
7468 // BIC and MVN instructions. It is not useful otherwise but is
7469 // implemented for compatibility.
7470 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
7471 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007472 } else {
7473 // A constant whose bitwise inverse can be used as an immediate
7474 // value in a data-processing instruction. This can be used in GCC
7475 // with a "B" modifier that prints the inverted value, for use with
7476 // BIC and MVN instructions. It is not useful otherwise but is
7477 // implemented for compatibility.
7478 if (ARM_AM::getSOImmVal(~CVal) != -1)
7479 break;
7480 }
7481 return;
7482
7483 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007484 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007485 // This must be a constant between -7 and 7,
7486 // for 3-operand ADD/SUB immediate instructions.
7487 if (CVal >= -7 && CVal < 7)
7488 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007489 } else if (Subtarget->isThumb2()) {
7490 // A constant whose negation can be used as an immediate value in a
7491 // data-processing instruction. This can be used in GCC with an "n"
7492 // modifier that prints the negated value, for use with SUB
7493 // instructions. It is not useful otherwise but is implemented for
7494 // compatibility.
7495 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
7496 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007497 } else {
7498 // A constant whose negation can be used as an immediate value in a
7499 // data-processing instruction. This can be used in GCC with an "n"
7500 // modifier that prints the negated value, for use with SUB
7501 // instructions. It is not useful otherwise but is implemented for
7502 // compatibility.
7503 if (ARM_AM::getSOImmVal(-CVal) != -1)
7504 break;
7505 }
7506 return;
7507
7508 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007509 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007510 // This must be a multiple of 4 between 0 and 1020, for
7511 // ADD sp + immediate.
7512 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
7513 break;
7514 } else {
7515 // A power of two or a constant between 0 and 32. This is used in
7516 // GCC for the shift amount on shifted register operands, but it is
7517 // useful in general for any shift amounts.
7518 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
7519 break;
7520 }
7521 return;
7522
7523 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007524 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007525 // This must be a constant between 0 and 31, for shift amounts.
7526 if (CVal >= 0 && CVal <= 31)
7527 break;
7528 }
7529 return;
7530
7531 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007532 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007533 // This must be a multiple of 4 between -508 and 508, for
7534 // ADD/SUB sp = sp + immediate.
7535 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
7536 break;
7537 }
7538 return;
7539 }
7540 Result = DAG.getTargetConstant(CVal, Op.getValueType());
7541 break;
7542 }
7543
7544 if (Result.getNode()) {
7545 Ops.push_back(Result);
7546 return;
7547 }
Dale Johannesen1784d162010-06-25 21:55:36 +00007548 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007549}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00007550
7551bool
7552ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7553 // The ARM target isn't yet aware of offsets.
7554 return false;
7555}
Evan Cheng39382422009-10-28 01:44:26 +00007556
7557int ARM::getVFPf32Imm(const APFloat &FPImm) {
7558 APInt Imm = FPImm.bitcastToAPInt();
7559 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
7560 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
7561 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
7562
7563 // We can handle 4 bits of mantissa.
7564 // mantissa = (16+UInt(e:f:g:h))/16.
7565 if (Mantissa & 0x7ffff)
7566 return -1;
7567 Mantissa >>= 19;
7568 if ((Mantissa & 0xf) != Mantissa)
7569 return -1;
7570
7571 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7572 if (Exp < -3 || Exp > 4)
7573 return -1;
7574 Exp = ((Exp+3) & 0x7) ^ 4;
7575
7576 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7577}
7578
7579int ARM::getVFPf64Imm(const APFloat &FPImm) {
7580 APInt Imm = FPImm.bitcastToAPInt();
7581 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
7582 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
7583 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
7584
7585 // We can handle 4 bits of mantissa.
7586 // mantissa = (16+UInt(e:f:g:h))/16.
7587 if (Mantissa & 0xffffffffffffLL)
7588 return -1;
7589 Mantissa >>= 48;
7590 if ((Mantissa & 0xf) != Mantissa)
7591 return -1;
7592
7593 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7594 if (Exp < -3 || Exp > 4)
7595 return -1;
7596 Exp = ((Exp+3) & 0x7) ^ 4;
7597
7598 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7599}
7600
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007601bool ARM::isBitFieldInvertedMask(unsigned v) {
7602 if (v == 0xffffffff)
7603 return 0;
7604 // there can be 1's on either or both "outsides", all the "inside"
7605 // bits must be 0's
7606 unsigned int lsb = 0, msb = 31;
7607 while (v & (1 << msb)) --msb;
7608 while (v & (1 << lsb)) ++lsb;
7609 for (unsigned int i = lsb; i <= msb; ++i) {
7610 if (v & (1 << i))
7611 return 0;
7612 }
7613 return 1;
7614}
7615
Evan Cheng39382422009-10-28 01:44:26 +00007616/// isFPImmLegal - Returns true if the target can instruction select the
7617/// specified FP immediate natively. If false, the legalizer will
7618/// materialize the FP immediate as a load from a constant pool.
7619bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
7620 if (!Subtarget->hasVFP3())
7621 return false;
7622 if (VT == MVT::f32)
7623 return ARM::getVFPf32Imm(Imm) != -1;
7624 if (VT == MVT::f64)
7625 return ARM::getVFPf64Imm(Imm) != -1;
7626 return false;
7627}
Bob Wilson65ffec42010-09-21 17:56:22 +00007628
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007629/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00007630/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
7631/// specified in the intrinsic calls.
7632bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
7633 const CallInst &I,
7634 unsigned Intrinsic) const {
7635 switch (Intrinsic) {
7636 case Intrinsic::arm_neon_vld1:
7637 case Intrinsic::arm_neon_vld2:
7638 case Intrinsic::arm_neon_vld3:
7639 case Intrinsic::arm_neon_vld4:
7640 case Intrinsic::arm_neon_vld2lane:
7641 case Intrinsic::arm_neon_vld3lane:
7642 case Intrinsic::arm_neon_vld4lane: {
7643 Info.opc = ISD::INTRINSIC_W_CHAIN;
7644 // Conservatively set memVT to the entire set of vectors loaded.
7645 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
7646 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7647 Info.ptrVal = I.getArgOperand(0);
7648 Info.offset = 0;
7649 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7650 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7651 Info.vol = false; // volatile loads with NEON intrinsics not supported
7652 Info.readMem = true;
7653 Info.writeMem = false;
7654 return true;
7655 }
7656 case Intrinsic::arm_neon_vst1:
7657 case Intrinsic::arm_neon_vst2:
7658 case Intrinsic::arm_neon_vst3:
7659 case Intrinsic::arm_neon_vst4:
7660 case Intrinsic::arm_neon_vst2lane:
7661 case Intrinsic::arm_neon_vst3lane:
7662 case Intrinsic::arm_neon_vst4lane: {
7663 Info.opc = ISD::INTRINSIC_VOID;
7664 // Conservatively set memVT to the entire set of vectors stored.
7665 unsigned NumElts = 0;
7666 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
7667 const Type *ArgTy = I.getArgOperand(ArgI)->getType();
7668 if (!ArgTy->isVectorTy())
7669 break;
7670 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
7671 }
7672 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7673 Info.ptrVal = I.getArgOperand(0);
7674 Info.offset = 0;
7675 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7676 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7677 Info.vol = false; // volatile stores with NEON intrinsics not supported
7678 Info.readMem = false;
7679 Info.writeMem = true;
7680 return true;
7681 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00007682 case Intrinsic::arm_strexd: {
7683 Info.opc = ISD::INTRINSIC_W_CHAIN;
7684 Info.memVT = MVT::i64;
7685 Info.ptrVal = I.getArgOperand(2);
7686 Info.offset = 0;
7687 Info.align = 8;
7688 Info.vol = false;
7689 Info.readMem = false;
7690 Info.writeMem = true;
7691 return true;
7692 }
7693 case Intrinsic::arm_ldrexd: {
7694 Info.opc = ISD::INTRINSIC_W_CHAIN;
7695 Info.memVT = MVT::i64;
7696 Info.ptrVal = I.getArgOperand(0);
7697 Info.offset = 0;
7698 Info.align = 8;
7699 Info.vol = false;
7700 Info.readMem = true;
7701 Info.writeMem = false;
7702 return true;
7703 }
Bob Wilson65ffec42010-09-21 17:56:22 +00007704 default:
7705 break;
7706 }
7707
7708 return false;
7709}