blob: d23c7ee96e02ed111b14d7ece0666f8fdd78ada5 [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
Stuart Hastingsc7315872011-04-20 16:47:52 +000075// The APCS parameter registers.
76static const unsigned GPRArgRegs[] = {
77 ARM::R0, ARM::R1, ARM::R2, ARM::R3
78};
79
Owen Andersone50ed302009-08-10 22:56:29 +000080void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
81 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000082 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000083 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000084 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
85 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000086
Owen Anderson70671842009-08-10 20:18:46 +000087 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000088 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000089 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000090 }
91
Owen Andersone50ed302009-08-10 22:56:29 +000092 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +000093 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +000094 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +000095 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +000096 if (ElemTy != MVT::i32) {
97 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
98 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
99 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
100 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
101 }
Owen Anderson70671842009-08-10 20:18:46 +0000102 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
103 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000104 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +0000105 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000106 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
107 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000108 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000109 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
110 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
111 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000112 setLoadExtAction(ISD::SEXTLOAD, VT.getSimpleVT(), Expand);
113 setLoadExtAction(ISD::ZEXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson24645a12010-11-01 18:31:39 +0000114 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
115 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
116 setTruncStoreAction(VT.getSimpleVT(),
117 (MVT::SimpleValueType)InnerVT, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000118 }
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000119 setLoadExtAction(ISD::EXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000120
121 // Promote all bit-wise operations.
122 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000123 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000124 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
125 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000126 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000127 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000128 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000129 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000130 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000131 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000132 }
Bob Wilson16330762009-09-16 00:17:28 +0000133
134 // Neon does not support vector divide/remainder operations.
135 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
136 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
137 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
138 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
139 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
140 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000141}
142
Owen Andersone50ed302009-08-10 22:56:29 +0000143void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000144 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000146}
147
Owen Andersone50ed302009-08-10 22:56:29 +0000148void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000149 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000150 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000151}
152
Chris Lattnerf0144122009-07-28 03:13:23 +0000153static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
154 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000155 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000156
Chris Lattner80ec2792009-08-02 00:34:36 +0000157 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000158}
159
Evan Chenga8e29892007-01-19 07:51:42 +0000160ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000161 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000162 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000163 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000164 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000165
Evan Chengb1df8f22007-04-27 08:15:43 +0000166 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000167 // Uses VFP for Thumb libfuncs if available.
168 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
169 // Single-precision floating-point arithmetic.
170 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
171 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
172 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
173 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000174
Evan Chengb1df8f22007-04-27 08:15:43 +0000175 // Double-precision floating-point arithmetic.
176 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
177 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
178 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
179 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000180
Evan Chengb1df8f22007-04-27 08:15:43 +0000181 // Single-precision comparisons.
182 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
183 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
184 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
185 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
186 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
187 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
188 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
189 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000190
Evan Chengb1df8f22007-04-27 08:15:43 +0000191 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
192 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
193 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
194 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
195 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
196 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
197 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
198 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000199
Evan Chengb1df8f22007-04-27 08:15:43 +0000200 // Double-precision comparisons.
201 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
202 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
203 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
204 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
205 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
206 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
207 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
208 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000209
Evan Chengb1df8f22007-04-27 08:15:43 +0000210 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
214 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
215 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
216 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
217 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000218
Evan Chengb1df8f22007-04-27 08:15:43 +0000219 // Floating-point to integer conversions.
220 // i64 conversions are done via library routines even when generating VFP
221 // instructions, so use the same ones.
222 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
223 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
224 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
225 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000226
Evan Chengb1df8f22007-04-27 08:15:43 +0000227 // Conversions between floating types.
228 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
229 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
230
231 // Integer to floating-point conversions.
232 // i64 conversions are done via library routines even when generating VFP
233 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000234 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
235 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000236 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
237 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
238 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
239 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
240 }
Evan Chenga8e29892007-01-19 07:51:42 +0000241 }
242
Bob Wilson2f954612009-05-22 17:38:41 +0000243 // These libcalls are not available in 32-bit.
244 setLibcallName(RTLIB::SHL_I128, 0);
245 setLibcallName(RTLIB::SRL_I128, 0);
246 setLibcallName(RTLIB::SRA_I128, 0);
247
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000248 if (Subtarget->isAAPCS_ABI()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000249 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000250 // RTABI chapter 4.1.2, Table 2
251 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
252 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
253 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
254 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
255 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
256 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
257 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
258 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
259
260 // Double-precision floating-point comparison helper functions
261 // RTABI chapter 4.1.2, Table 3
262 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
263 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
264 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
265 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
266 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
267 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
268 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
269 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
270 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
271 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
272 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
273 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
274 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
275 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
276 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
277 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
278 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
279 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
280 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
281 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
282 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
283 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
284 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
285 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
286
287 // Single-precision floating-point arithmetic helper functions
288 // RTABI chapter 4.1.2, Table 4
289 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
290 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
291 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
292 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
293 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
294 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
295 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
296 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
297
298 // Single-precision floating-point comparison helper functions
299 // RTABI chapter 4.1.2, Table 5
300 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
301 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
302 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
303 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
304 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
305 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
306 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
307 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
308 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
309 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
310 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
311 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
312 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
313 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
314 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
315 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
316 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
317 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
318 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
319 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
320 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
321 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
322 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
323 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
324
325 // Floating-point to integer conversions.
326 // RTABI chapter 4.1.2, Table 6
327 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
328 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
329 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
330 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
331 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
332 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
333 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
334 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
335 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
337 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
338 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
339 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
340 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
341 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
342 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
343
344 // Conversions between floating types.
345 // RTABI chapter 4.1.2, Table 7
346 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
347 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
348 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000349 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000350
351 // Integer to floating-point conversions.
352 // RTABI chapter 4.1.2, Table 8
353 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
354 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
355 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
356 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
357 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
358 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
359 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
360 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
361 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
362 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
363 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
364 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
365 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
366 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
367 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
368 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
369
370 // Long long helper functions
371 // RTABI chapter 4.2, Table 9
372 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
373 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
374 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
375 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
376 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
377 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
378 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
379 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
380 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
381 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
382 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
383 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
384
385 // Integer division functions
386 // RTABI chapter 4.3.1
387 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
388 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
389 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
390 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
391 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
392 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
393 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
394 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
395 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
396 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
397 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000398 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000399
400 // Memory operations
401 // RTABI chapter 4.3.4
402 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
403 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
404 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000405 }
406
David Goodwinf1daf7d2009-07-08 23:10:31 +0000407 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000409 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000410 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000411 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000412 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000413 if (!Subtarget->isFPOnlySP())
414 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000415
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000417 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000418
419 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000420 addDRTypeForNEON(MVT::v2f32);
421 addDRTypeForNEON(MVT::v8i8);
422 addDRTypeForNEON(MVT::v4i16);
423 addDRTypeForNEON(MVT::v2i32);
424 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000425
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 addQRTypeForNEON(MVT::v4f32);
427 addQRTypeForNEON(MVT::v2f64);
428 addQRTypeForNEON(MVT::v16i8);
429 addQRTypeForNEON(MVT::v8i16);
430 addQRTypeForNEON(MVT::v4i32);
431 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000432
Bob Wilson74dc72e2009-09-15 23:55:57 +0000433 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
434 // neither Neon nor VFP support any arithmetic operations on it.
435 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
436 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
437 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
438 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
439 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
440 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
441 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
442 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
443 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
444 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
445 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
446 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
447 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
448 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
449 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
450 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
451 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
452 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
453 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
454 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
455 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
456 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
457 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
458 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
459
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000460 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
461
Bob Wilson642b3292009-09-16 00:32:15 +0000462 // Neon does not support some operations on v1i64 and v2i64 types.
463 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000464 // Custom handling for some quad-vector types to detect VMULL.
465 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
466 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
467 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000468 // Custom handling for some vector types to avoid expensive expansions
469 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
470 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
471 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
472 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000473 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
474 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000475 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
476 // a destination type that is wider than the source.
477 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
478 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000479
Bob Wilson1c3ef902011-02-07 17:43:21 +0000480 setTargetDAGCombine(ISD::INTRINSIC_VOID);
481 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000482 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
483 setTargetDAGCombine(ISD::SHL);
484 setTargetDAGCombine(ISD::SRL);
485 setTargetDAGCombine(ISD::SRA);
486 setTargetDAGCombine(ISD::SIGN_EXTEND);
487 setTargetDAGCombine(ISD::ZERO_EXTEND);
488 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000489 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000490 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000491 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000492 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
493 setTargetDAGCombine(ISD::STORE);
Bob Wilson5bafff32009-06-22 23:27:02 +0000494 }
495
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000496 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000497
498 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000499 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000500
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000501 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000502 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000503
Evan Chenga8e29892007-01-19 07:51:42 +0000504 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000505 if (!Subtarget->isThumb1Only()) {
506 for (unsigned im = (unsigned)ISD::PRE_INC;
507 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000508 setIndexedLoadAction(im, MVT::i1, Legal);
509 setIndexedLoadAction(im, MVT::i8, Legal);
510 setIndexedLoadAction(im, MVT::i16, Legal);
511 setIndexedLoadAction(im, MVT::i32, Legal);
512 setIndexedStoreAction(im, MVT::i1, Legal);
513 setIndexedStoreAction(im, MVT::i8, Legal);
514 setIndexedStoreAction(im, MVT::i16, Legal);
515 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000516 }
Evan Chenga8e29892007-01-19 07:51:42 +0000517 }
518
519 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000520 setOperationAction(ISD::MUL, MVT::i64, Expand);
521 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000522 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000523 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
524 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000525 }
Eric Christopher2cc40132011-04-19 18:49:19 +0000526 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops())
527 setOperationAction(ISD::MULHS, MVT::i32, Expand);
528
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000529 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000530 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000531 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000532 setOperationAction(ISD::SRL, MVT::i64, Custom);
533 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000534
535 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000536 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000537 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000539 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000540 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000541
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000542 // Only ARMv6 has BSWAP.
543 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000544 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000545
Evan Chenga8e29892007-01-19 07:51:42 +0000546 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000547 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000548 // v7M has a hardware divider
549 setOperationAction(ISD::SDIV, MVT::i32, Expand);
550 setOperationAction(ISD::UDIV, MVT::i32, Expand);
551 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000552 setOperationAction(ISD::SREM, MVT::i32, Expand);
553 setOperationAction(ISD::UREM, MVT::i32, Expand);
554 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
555 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000556
Owen Anderson825b72b2009-08-11 20:47:22 +0000557 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
558 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
559 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
560 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000561 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000562
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000563 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000564
Evan Chenga8e29892007-01-19 07:51:42 +0000565 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000566 setOperationAction(ISD::VASTART, MVT::Other, Custom);
567 setOperationAction(ISD::VAARG, MVT::Other, Expand);
568 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
569 setOperationAction(ISD::VAEND, MVT::Other, Expand);
570 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
571 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000572 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000573 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
574 setExceptionPointerRegister(ARM::R0);
575 setExceptionSelectorRegister(ARM::R1);
576
Evan Cheng3a1588a2010-04-15 22:20:34 +0000577 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000578 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
579 // the default expansion.
580 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000581 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000582 // membarrier needs custom lowering; the rest are legal and handled
583 // normally.
584 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
585 } else {
586 // Set them all for expansion, which will force libcalls.
587 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
588 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
589 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
590 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000591 setOperationAction(ISD::ATOMIC_SWAP, MVT::i8, Expand);
592 setOperationAction(ISD::ATOMIC_SWAP, MVT::i16, Expand);
593 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000594 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
595 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
596 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
597 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
598 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
599 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
600 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
601 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
602 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
603 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
604 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
605 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
606 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
607 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
608 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
609 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
610 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
611 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000612 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i8, Expand);
613 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i16, Expand);
614 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
615 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i8, Expand);
616 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i16, Expand);
617 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
618 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i8, Expand);
619 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i16, Expand);
620 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
621 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i8, Expand);
622 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i16, Expand);
623 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000624 // Since the libcalls include locking, fold in the fences
625 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000626 }
627 // 64-bit versions are always libcalls (for now)
628 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000629 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000630 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
631 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
632 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
633 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
634 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
635 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000636
Evan Cheng416941d2010-11-04 05:19:35 +0000637 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000638
Eli Friedmana2c6f452010-06-26 04:36:50 +0000639 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
640 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000641 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
642 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000643 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000644 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000645
Nate Begemand1fb5832010-08-03 21:31:55 +0000646 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000647 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
648 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000649 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000650 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
651 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000652
653 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000654 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000655 if (Subtarget->isTargetDarwin()) {
656 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
657 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000658 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000659 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000660 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000661
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::SETCC, MVT::i32, Expand);
663 setOperationAction(ISD::SETCC, MVT::f32, Expand);
664 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000665 setOperationAction(ISD::SELECT, MVT::i32, Custom);
666 setOperationAction(ISD::SELECT, MVT::f32, Custom);
667 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000668 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
669 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
670 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000671
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
673 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
674 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
675 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
676 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000677
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000678 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000679 setOperationAction(ISD::FSIN, MVT::f64, Expand);
680 setOperationAction(ISD::FSIN, MVT::f32, Expand);
681 setOperationAction(ISD::FCOS, MVT::f32, Expand);
682 setOperationAction(ISD::FCOS, MVT::f64, Expand);
683 setOperationAction(ISD::FREM, MVT::f64, Expand);
684 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000685 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000686 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
687 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000688 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000689 setOperationAction(ISD::FPOW, MVT::f64, Expand);
690 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000691
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000692 // Various VFP goodness
693 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000694 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
695 if (Subtarget->hasVFP2()) {
696 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
697 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
698 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
699 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
700 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000701 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000702 if (!Subtarget->hasFP16()) {
703 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
704 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000705 }
Evan Cheng110cf482008-04-01 01:50:16 +0000706 }
Evan Chenga8e29892007-01-19 07:51:42 +0000707
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000708 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000709 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000710 setTargetDAGCombine(ISD::ADD);
711 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000712 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000713
Owen Anderson080c0922010-11-05 19:27:46 +0000714 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000715 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000716 if (Subtarget->hasNEON())
717 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000718
Evan Chenga8e29892007-01-19 07:51:42 +0000719 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000720
Evan Chengf7d87ee2010-05-21 00:43:17 +0000721 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
722 setSchedulingPreference(Sched::RegPressure);
723 else
724 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000725
Evan Cheng05219282011-01-06 06:52:41 +0000726 //// temporary - rewrite interface to use type
727 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000728
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000729 // On ARM arguments smaller than 4 bytes are extended, so all arguments
730 // are at least 4 bytes aligned.
731 setMinStackArgumentAlignment(4);
732
Evan Chengfff606d2010-09-24 19:07:23 +0000733 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000734
735 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000736}
737
Andrew Trick32cec0a2011-01-19 02:35:27 +0000738// FIXME: It might make sense to define the representative register class as the
739// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
740// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
741// SPR's representative would be DPR_VFP2. This should work well if register
742// pressure tracking were modified such that a register use would increment the
743// pressure of the register class's representative and all of it's super
744// classes' representatives transitively. We have not implemented this because
745// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000746// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000747// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000748std::pair<const TargetRegisterClass*, uint8_t>
749ARMTargetLowering::findRepresentativeClass(EVT VT) const{
750 const TargetRegisterClass *RRC = 0;
751 uint8_t Cost = 1;
752 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000753 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000754 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000755 // Use DPR as representative register class for all floating point
756 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
757 // the cost is 1 for both f32 and f64.
758 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000759 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000760 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000761 // When NEON is used for SP, only half of the register file is available
762 // because operations that define both SP and DP results will be constrained
763 // to the VFP2 class (D0-D15). We currently model this constraint prior to
764 // coalescing by double-counting the SP regs. See the FIXME above.
765 if (Subtarget->useNEONForSinglePrecisionFP())
766 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000767 break;
768 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
769 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000770 RRC = ARM::DPRRegisterClass;
771 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000772 break;
773 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000774 RRC = ARM::DPRRegisterClass;
775 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000776 break;
777 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000778 RRC = ARM::DPRRegisterClass;
779 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000780 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000781 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000782 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000783}
784
Evan Chenga8e29892007-01-19 07:51:42 +0000785const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
786 switch (Opcode) {
787 default: return 0;
788 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000789 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000790 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000791 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
792 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000793 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000794 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
795 case ARMISD::tCALL: return "ARMISD::tCALL";
796 case ARMISD::BRCOND: return "ARMISD::BRCOND";
797 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000798 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000799 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
800 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
801 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000802 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000803 case ARMISD::CMPFP: return "ARMISD::CMPFP";
804 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000805 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000806 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
807 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000808
Jim Grosbach3482c802010-01-18 19:58:49 +0000809 case ARMISD::RBIT: return "ARMISD::RBIT";
810
Bob Wilson76a312b2010-03-19 22:51:32 +0000811 case ARMISD::FTOSI: return "ARMISD::FTOSI";
812 case ARMISD::FTOUI: return "ARMISD::FTOUI";
813 case ARMISD::SITOF: return "ARMISD::SITOF";
814 case ARMISD::UITOF: return "ARMISD::UITOF";
815
Evan Chenga8e29892007-01-19 07:51:42 +0000816 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
817 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
818 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000819
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000820 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
821 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000822
Evan Chengc5942082009-10-28 06:55:03 +0000823 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
824 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000825 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000826
Dale Johannesen51e28e62010-06-03 21:09:53 +0000827 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000828
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000829 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000830
Evan Cheng86198642009-08-07 00:34:42 +0000831 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
832
Jim Grosbach3728e962009-12-10 00:11:09 +0000833 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000834 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000835
Evan Chengdfed19f2010-11-03 06:34:55 +0000836 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
837
Bob Wilson5bafff32009-06-22 23:27:02 +0000838 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000839 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000840 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000841 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
842 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000843 case ARMISD::VCGEU: return "ARMISD::VCGEU";
844 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000845 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
846 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000847 case ARMISD::VCGTU: return "ARMISD::VCGTU";
848 case ARMISD::VTST: return "ARMISD::VTST";
849
850 case ARMISD::VSHL: return "ARMISD::VSHL";
851 case ARMISD::VSHRs: return "ARMISD::VSHRs";
852 case ARMISD::VSHRu: return "ARMISD::VSHRu";
853 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
854 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
855 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
856 case ARMISD::VSHRN: return "ARMISD::VSHRN";
857 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
858 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
859 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
860 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
861 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
862 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
863 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
864 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
865 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
866 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
867 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
868 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
869 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
870 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000871 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000872 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000873 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000874 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000875 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000876 case ARMISD::VREV64: return "ARMISD::VREV64";
877 case ARMISD::VREV32: return "ARMISD::VREV32";
878 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000879 case ARMISD::VZIP: return "ARMISD::VZIP";
880 case ARMISD::VUZP: return "ARMISD::VUZP";
881 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000882 case ARMISD::VTBL1: return "ARMISD::VTBL1";
883 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000884 case ARMISD::VMULLs: return "ARMISD::VMULLs";
885 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000886 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000887 case ARMISD::FMAX: return "ARMISD::FMAX";
888 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000889 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000890 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
891 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000892 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000893 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
894 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
895 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000896 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
897 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
898 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
899 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
900 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
901 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
902 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
903 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
904 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
905 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
906 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
907 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
908 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
909 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
910 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
911 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
912 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000913 }
914}
915
Evan Cheng06b666c2010-05-15 02:18:07 +0000916/// getRegClassFor - Return the register class that should be used for the
917/// specified value type.
918TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
919 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
920 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
921 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000922 if (Subtarget->hasNEON()) {
923 if (VT == MVT::v4i64)
924 return ARM::QQPRRegisterClass;
925 else if (VT == MVT::v8i64)
926 return ARM::QQQQPRRegisterClass;
927 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000928 return TargetLowering::getRegClassFor(VT);
929}
930
Eric Christopherab695882010-07-21 22:26:11 +0000931// Create a fast isel object.
932FastISel *
933ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
934 return ARM::createFastISel(funcInfo);
935}
936
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000937/// getMaximalGlobalOffset - Returns the maximal possible offset which can
938/// be used for loads / stores from the global.
939unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
940 return (Subtarget->isThumb1Only() ? 127 : 4095);
941}
942
Evan Cheng1cc39842010-05-20 23:26:43 +0000943Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000944 unsigned NumVals = N->getNumValues();
945 if (!NumVals)
946 return Sched::RegPressure;
947
948 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000949 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000950 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000951 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000952 if (VT.isFloatingPoint() || VT.isVector())
953 return Sched::Latency;
954 }
Evan Chengc10f5432010-05-28 23:25:23 +0000955
956 if (!N->isMachineOpcode())
957 return Sched::RegPressure;
958
959 // Load are scheduled for latency even if there instruction itinerary
960 // is not available.
961 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
962 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000963
964 if (TID.getNumDefs() == 0)
965 return Sched::RegPressure;
966 if (!Itins->isEmpty() &&
967 Itins->getOperandCycle(TID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000968 return Sched::Latency;
969
Evan Cheng1cc39842010-05-20 23:26:43 +0000970 return Sched::RegPressure;
971}
972
Evan Chenga8e29892007-01-19 07:51:42 +0000973//===----------------------------------------------------------------------===//
974// Lowering Code
975//===----------------------------------------------------------------------===//
976
Evan Chenga8e29892007-01-19 07:51:42 +0000977/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
978static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
979 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000980 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000981 case ISD::SETNE: return ARMCC::NE;
982 case ISD::SETEQ: return ARMCC::EQ;
983 case ISD::SETGT: return ARMCC::GT;
984 case ISD::SETGE: return ARMCC::GE;
985 case ISD::SETLT: return ARMCC::LT;
986 case ISD::SETLE: return ARMCC::LE;
987 case ISD::SETUGT: return ARMCC::HI;
988 case ISD::SETUGE: return ARMCC::HS;
989 case ISD::SETULT: return ARMCC::LO;
990 case ISD::SETULE: return ARMCC::LS;
991 }
992}
993
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000994/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
995static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000996 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000997 CondCode2 = ARMCC::AL;
998 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000999 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001000 case ISD::SETEQ:
1001 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1002 case ISD::SETGT:
1003 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1004 case ISD::SETGE:
1005 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1006 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001007 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001008 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1009 case ISD::SETO: CondCode = ARMCC::VC; break;
1010 case ISD::SETUO: CondCode = ARMCC::VS; break;
1011 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1012 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1013 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1014 case ISD::SETLT:
1015 case ISD::SETULT: CondCode = ARMCC::LT; break;
1016 case ISD::SETLE:
1017 case ISD::SETULE: CondCode = ARMCC::LE; break;
1018 case ISD::SETNE:
1019 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1020 }
Evan Chenga8e29892007-01-19 07:51:42 +00001021}
1022
Bob Wilson1f595bb2009-04-17 19:07:39 +00001023//===----------------------------------------------------------------------===//
1024// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001025//===----------------------------------------------------------------------===//
1026
1027#include "ARMGenCallingConv.inc"
1028
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001029/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1030/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001031CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001032 bool Return,
1033 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001034 switch (CC) {
1035 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001036 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001037 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001038 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001039 if (!Subtarget->isAAPCS_ABI())
1040 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1041 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1042 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1043 }
1044 // Fallthrough
1045 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001046 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001047 if (!Subtarget->isAAPCS_ABI())
1048 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1049 else if (Subtarget->hasVFP2() &&
1050 FloatABIType == FloatABI::Hard && !isVarArg)
1051 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1052 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1053 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001054 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001055 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001056 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001057 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001058 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001059 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001060 }
1061}
1062
Dan Gohman98ca4f22009-08-05 01:29:28 +00001063/// LowerCallResult - Lower the result values of a call into the
1064/// appropriate copies out of appropriate physical registers.
1065SDValue
1066ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001067 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001068 const SmallVectorImpl<ISD::InputArg> &Ins,
1069 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001070 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001071
Bob Wilson1f595bb2009-04-17 19:07:39 +00001072 // Assign locations to each value returned by this call.
1073 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001074 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1075 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001076 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001077 CCAssignFnForNode(CallConv, /* Return*/ true,
1078 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001079
1080 // Copy all of the result registers out of their specified physreg.
1081 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1082 CCValAssign VA = RVLocs[i];
1083
Bob Wilson80915242009-04-25 00:33:20 +00001084 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001085 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001086 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001087 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001088 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001089 Chain = Lo.getValue(1);
1090 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001091 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001092 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001093 InFlag);
1094 Chain = Hi.getValue(1);
1095 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001096 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001097
Owen Anderson825b72b2009-08-11 20:47:22 +00001098 if (VA.getLocVT() == MVT::v2f64) {
1099 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1100 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1101 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001102
1103 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001104 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001105 Chain = Lo.getValue(1);
1106 InFlag = Lo.getValue(2);
1107 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001108 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001109 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);
Owen Anderson825b72b2009-08-11 20:47:22 +00001112 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1113 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001114 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001115 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001116 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1117 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001118 Chain = Val.getValue(1);
1119 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001120 }
Bob Wilson80915242009-04-25 00:33:20 +00001121
1122 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001123 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001124 case CCValAssign::Full: break;
1125 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001126 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001127 break;
1128 }
1129
Dan Gohman98ca4f22009-08-05 01:29:28 +00001130 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001131 }
1132
Dan Gohman98ca4f22009-08-05 01:29:28 +00001133 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001134}
1135
Bob Wilsondee46d72009-04-17 20:35:10 +00001136/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001137SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001138ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1139 SDValue StackPtr, SDValue Arg,
1140 DebugLoc dl, SelectionDAG &DAG,
1141 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001142 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001143 unsigned LocMemOffset = VA.getLocMemOffset();
1144 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1145 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001146 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001147 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001148 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001149}
1150
Dan Gohman98ca4f22009-08-05 01:29:28 +00001151void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001152 SDValue Chain, SDValue &Arg,
1153 RegsToPassVector &RegsToPass,
1154 CCValAssign &VA, CCValAssign &NextVA,
1155 SDValue &StackPtr,
1156 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001157 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001158
Jim Grosbache5165492009-11-09 00:11:35 +00001159 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001160 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001161 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1162
1163 if (NextVA.isRegLoc())
1164 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1165 else {
1166 assert(NextVA.isMemLoc());
1167 if (StackPtr.getNode() == 0)
1168 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1169
Dan Gohman98ca4f22009-08-05 01:29:28 +00001170 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1171 dl, DAG, NextVA,
1172 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001173 }
1174}
1175
Dan Gohman98ca4f22009-08-05 01:29:28 +00001176/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001177/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1178/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001179SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001180ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001181 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001182 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001183 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001184 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001185 const SmallVectorImpl<ISD::InputArg> &Ins,
1186 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001187 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001188 MachineFunction &MF = DAG.getMachineFunction();
1189 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1190 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001191 // Temporarily disable tail calls so things don't break.
Evan Cheng0b655992011-05-20 17:38:48 +00001192 if (!EnableARMTailCalls)
Bob Wilson703af3a2010-08-13 22:43:33 +00001193 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001194 if (isTailCall) {
1195 // Check if it's really possible to do a tail call.
1196 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1197 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001198 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001199 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1200 // detected sibcalls.
1201 if (isTailCall) {
1202 ++NumTailCalls;
1203 IsSibCall = true;
1204 }
1205 }
Evan Chenga8e29892007-01-19 07:51:42 +00001206
Bob Wilson1f595bb2009-04-17 19:07:39 +00001207 // Analyze operands of the call, assigning locations to each operand.
1208 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001209 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1210 getTargetMachine(), ArgLocs, *DAG.getContext());
Stuart Hastingsc7315872011-04-20 16:47:52 +00001211 CCInfo.setCallOrPrologue(Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001212 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001213 CCAssignFnForNode(CallConv, /* Return*/ false,
1214 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001215
Bob Wilson1f595bb2009-04-17 19:07:39 +00001216 // Get a count of how many bytes are to be pushed on the stack.
1217 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001218
Dale Johannesen51e28e62010-06-03 21:09:53 +00001219 // For tail calls, memory operands are available in our caller's stack.
1220 if (IsSibCall)
1221 NumBytes = 0;
1222
Evan Chenga8e29892007-01-19 07:51:42 +00001223 // Adjust the stack pointer for the new arguments...
1224 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001225 if (!IsSibCall)
1226 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001227
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001228 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001229
Bob Wilson5bafff32009-06-22 23:27:02 +00001230 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001231 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001232
Bob Wilson1f595bb2009-04-17 19:07:39 +00001233 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001234 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001235 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1236 i != e;
1237 ++i, ++realArgIdx) {
1238 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001239 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001240 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001241 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001242
Bob Wilson1f595bb2009-04-17 19:07:39 +00001243 // Promote the value if needed.
1244 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001245 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001246 case CCValAssign::Full: break;
1247 case CCValAssign::SExt:
1248 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1249 break;
1250 case CCValAssign::ZExt:
1251 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1252 break;
1253 case CCValAssign::AExt:
1254 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1255 break;
1256 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001257 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001258 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001259 }
1260
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001261 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001262 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001263 if (VA.getLocVT() == MVT::v2f64) {
1264 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1265 DAG.getConstant(0, MVT::i32));
1266 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1267 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001268
Dan Gohman98ca4f22009-08-05 01:29:28 +00001269 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001270 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1271
1272 VA = ArgLocs[++i]; // skip ahead to next loc
1273 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001274 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001275 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1276 } else {
1277 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001278
Dan Gohman98ca4f22009-08-05 01:29:28 +00001279 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1280 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001281 }
1282 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001283 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001284 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001285 }
1286 } else if (VA.isRegLoc()) {
1287 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001288 } else if (isByVal) {
1289 assert(VA.isMemLoc());
1290 unsigned offset = 0;
1291
1292 // True if this byval aggregate will be split between registers
1293 // and memory.
1294 if (CCInfo.isFirstByValRegValid()) {
1295 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1296 unsigned int i, j;
1297 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1298 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1299 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1300 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1301 MachinePointerInfo(),
1302 false, false, 0);
1303 MemOpChains.push_back(Load.getValue(1));
1304 RegsToPass.push_back(std::make_pair(j, Load));
1305 }
1306 offset = ARM::R4 - CCInfo.getFirstByValReg();
1307 CCInfo.clearFirstByValReg();
1308 }
1309
1310 unsigned LocMemOffset = VA.getLocMemOffset();
1311 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1312 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1313 StkPtrOff);
1314 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1315 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1316 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1317 MVT::i32);
1318 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1319 Flags.getByValAlign(),
1320 /*isVolatile=*/false,
1321 /*AlwaysInline=*/false,
1322 MachinePointerInfo(0),
1323 MachinePointerInfo(0)));
1324
1325 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001326 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001327
Dan Gohman98ca4f22009-08-05 01:29:28 +00001328 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1329 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001330 }
Evan Chenga8e29892007-01-19 07:51:42 +00001331 }
1332
1333 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001334 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001335 &MemOpChains[0], MemOpChains.size());
1336
1337 // Build a sequence of copy-to-reg nodes chained together with token chain
1338 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001339 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001340 // Tail call byval lowering might overwrite argument registers so in case of
1341 // tail call optimization the copies to registers are lowered later.
1342 if (!isTailCall)
1343 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1344 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1345 RegsToPass[i].second, InFlag);
1346 InFlag = Chain.getValue(1);
1347 }
Evan Chenga8e29892007-01-19 07:51:42 +00001348
Dale Johannesen51e28e62010-06-03 21:09:53 +00001349 // For tail calls lower the arguments to the 'real' stack slot.
1350 if (isTailCall) {
1351 // Force all the incoming stack arguments to be loaded from the stack
1352 // before any new outgoing arguments are stored to the stack, because the
1353 // outgoing stack slots may alias the incoming argument stack slots, and
1354 // the alias isn't otherwise explicit. This is slightly more conservative
1355 // than necessary, because it means that each store effectively depends
1356 // on every argument instead of just those arguments it would clobber.
1357
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001358 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001359 InFlag = SDValue();
1360 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1361 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1362 RegsToPass[i].second, InFlag);
1363 InFlag = Chain.getValue(1);
1364 }
1365 InFlag =SDValue();
1366 }
1367
Bill Wendling056292f2008-09-16 21:48:12 +00001368 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1369 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1370 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001371 bool isDirect = false;
1372 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001373 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001374 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001375
1376 if (EnableARMLongCalls) {
1377 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1378 && "long-calls with non-static relocation model!");
1379 // Handle a global address or an external symbol. If it's not one of
1380 // those, the target's already in a register, so we don't need to do
1381 // anything extra.
1382 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001383 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001384 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001385 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001386 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1387 ARMPCLabelIndex,
1388 ARMCP::CPValue, 0);
1389 // Get the address of the callee into a register
1390 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1391 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1392 Callee = DAG.getLoad(getPointerTy(), dl,
1393 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001394 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001395 false, false, 0);
1396 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1397 const char *Sym = S->getSymbol();
1398
1399 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001400 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001401 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1402 Sym, ARMPCLabelIndex, 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 }
1411 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001412 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001413 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001414 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001415 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001416 getTargetMachine().getRelocationModel() != Reloc::Static;
1417 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001418 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001419 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001420 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001421 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001422 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001423 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001424 ARMPCLabelIndex,
1425 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001426 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001427 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001428 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001429 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001430 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001431 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001432 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001433 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001434 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001435 } else {
1436 // On ELF targets for PIC code, direct calls should go through the PLT
1437 unsigned OpFlags = 0;
1438 if (Subtarget->isTargetELF() &&
1439 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1440 OpFlags = ARMII::MO_PLT;
1441 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1442 }
Bill Wendling056292f2008-09-16 21:48:12 +00001443 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001444 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001445 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001446 getTargetMachine().getRelocationModel() != Reloc::Static;
1447 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001448 // tBX takes a register source operand.
1449 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001450 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001451 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001452 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001453 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001454 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001455 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001456 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001457 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001458 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001459 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001460 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001461 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001462 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001463 } else {
1464 unsigned OpFlags = 0;
1465 // On ELF targets for PIC code, direct calls should go through the PLT
1466 if (Subtarget->isTargetELF() &&
1467 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1468 OpFlags = ARMII::MO_PLT;
1469 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1470 }
Evan Chenga8e29892007-01-19 07:51:42 +00001471 }
1472
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001473 // FIXME: handle tail calls differently.
1474 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001475 if (Subtarget->isThumb()) {
1476 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001477 CallOpc = ARMISD::CALL_NOLINK;
1478 else
1479 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1480 } else {
1481 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001482 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1483 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001484 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001485
Dan Gohman475871a2008-07-27 21:46:04 +00001486 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001487 Ops.push_back(Chain);
1488 Ops.push_back(Callee);
1489
1490 // Add argument registers to the end of the list so that they are known live
1491 // into the call.
1492 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1493 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1494 RegsToPass[i].second.getValueType()));
1495
Gabor Greifba36cb52008-08-28 21:40:38 +00001496 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001497 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001498
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001499 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001500 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001501 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001502
Duncan Sands4bdcb612008-07-02 17:40:58 +00001503 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001504 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001505 InFlag = Chain.getValue(1);
1506
Chris Lattnere563bbc2008-10-11 22:08:30 +00001507 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1508 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001509 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001510 InFlag = Chain.getValue(1);
1511
Bob Wilson1f595bb2009-04-17 19:07:39 +00001512 // Handle result values, copying them out of physregs into vregs that we
1513 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001514 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1515 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001516}
1517
Stuart Hastingsf222e592011-02-28 17:17:53 +00001518/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001519/// on the stack. Remember the next parameter register to allocate,
1520/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001521/// this.
1522void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001523llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1524 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1525 assert((State->getCallOrPrologue() == Prologue ||
1526 State->getCallOrPrologue() == Call) &&
1527 "unhandled ParmContext");
1528 if ((!State->isFirstByValRegValid()) &&
1529 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1530 State->setFirstByValReg(reg);
1531 // At a call site, a byval parameter that is split between
1532 // registers and memory needs its size truncated here. In a
1533 // function prologue, such byval parameters are reassembled in
1534 // memory, and are not truncated.
1535 if (State->getCallOrPrologue() == Call) {
1536 unsigned excess = 4 * (ARM::R4 - reg);
1537 assert(size >= excess && "expected larger existing stack allocation");
1538 size -= excess;
1539 }
1540 }
1541 // Confiscate any remaining parameter registers to preclude their
1542 // assignment to subsequent parameters.
1543 while (State->AllocateReg(GPRArgRegs, 4))
1544 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001545}
1546
Dale Johannesen51e28e62010-06-03 21:09:53 +00001547/// MatchingStackOffset - Return true if the given stack call argument is
1548/// already available in the same position (relatively) of the caller's
1549/// incoming argument stack.
1550static
1551bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1552 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1553 const ARMInstrInfo *TII) {
1554 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1555 int FI = INT_MAX;
1556 if (Arg.getOpcode() == ISD::CopyFromReg) {
1557 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001558 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001559 return false;
1560 MachineInstr *Def = MRI->getVRegDef(VR);
1561 if (!Def)
1562 return false;
1563 if (!Flags.isByVal()) {
1564 if (!TII->isLoadFromStackSlot(Def, FI))
1565 return false;
1566 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001567 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001568 }
1569 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1570 if (Flags.isByVal())
1571 // ByVal argument is passed in as a pointer but it's now being
1572 // dereferenced. e.g.
1573 // define @foo(%struct.X* %A) {
1574 // tail call @bar(%struct.X* byval %A)
1575 // }
1576 return false;
1577 SDValue Ptr = Ld->getBasePtr();
1578 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1579 if (!FINode)
1580 return false;
1581 FI = FINode->getIndex();
1582 } else
1583 return false;
1584
1585 assert(FI != INT_MAX);
1586 if (!MFI->isFixedObjectIndex(FI))
1587 return false;
1588 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1589}
1590
1591/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1592/// for tail call optimization. Targets which want to do tail call
1593/// optimization should implement this function.
1594bool
1595ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1596 CallingConv::ID CalleeCC,
1597 bool isVarArg,
1598 bool isCalleeStructRet,
1599 bool isCallerStructRet,
1600 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001601 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001602 const SmallVectorImpl<ISD::InputArg> &Ins,
1603 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001604 const Function *CallerF = DAG.getMachineFunction().getFunction();
1605 CallingConv::ID CallerCC = CallerF->getCallingConv();
1606 bool CCMatch = CallerCC == CalleeCC;
1607
1608 // Look for obvious safe cases to perform tail call optimization that do not
1609 // require ABI changes. This is what gcc calls sibcall.
1610
Jim Grosbach7616b642010-06-16 23:45:49 +00001611 // Do not sibcall optimize vararg calls unless the call site is not passing
1612 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001613 if (isVarArg && !Outs.empty())
1614 return false;
1615
1616 // Also avoid sibcall optimization if either caller or callee uses struct
1617 // return semantics.
1618 if (isCalleeStructRet || isCallerStructRet)
1619 return false;
1620
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001621 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Evan Cheng0110ac62010-06-19 01:01:32 +00001622 // emitEpilogue is not ready for them.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001623 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1624 // LR. This means if we need to reload LR, it takes an extra instructions,
1625 // which outweighs the value of the tail call; but here we don't know yet
1626 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001627 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001628 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001629
1630 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1631 // but we need to make sure there are enough registers; the only valid
1632 // registers are the 4 used for parameters. We don't currently do this
1633 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001634 if (Subtarget->isThumb1Only())
1635 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001636
Dale Johannesen51e28e62010-06-03 21:09:53 +00001637 // If the calling conventions do not match, then we'd better make sure the
1638 // results are returned in the same way as what the caller expects.
1639 if (!CCMatch) {
1640 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00001641 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1642 getTargetMachine(), RVLocs1, *DAG.getContext());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001643 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1644
1645 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00001646 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1647 getTargetMachine(), RVLocs2, *DAG.getContext());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001648 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1649
1650 if (RVLocs1.size() != RVLocs2.size())
1651 return false;
1652 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1653 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1654 return false;
1655 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1656 return false;
1657 if (RVLocs1[i].isRegLoc()) {
1658 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1659 return false;
1660 } else {
1661 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1662 return false;
1663 }
1664 }
1665 }
1666
1667 // If the callee takes no arguments then go on to check the results of the
1668 // call.
1669 if (!Outs.empty()) {
1670 // Check if stack adjustment is needed. For now, do not do this if any
1671 // argument is passed on the stack.
1672 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001673 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1674 getTargetMachine(), ArgLocs, *DAG.getContext());
Cameron Zwaricha2e97952011-06-09 22:30:07 +00001675 CCInfo.setCallOrPrologue(Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001676 CCInfo.AnalyzeCallOperands(Outs,
1677 CCAssignFnForNode(CalleeCC, false, isVarArg));
1678 if (CCInfo.getNextStackOffset()) {
1679 MachineFunction &MF = DAG.getMachineFunction();
1680
1681 // Check if the arguments are already laid out in the right way as
1682 // the caller's fixed stack objects.
1683 MachineFrameInfo *MFI = MF.getFrameInfo();
1684 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1685 const ARMInstrInfo *TII =
1686 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001687 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1688 i != e;
1689 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001690 CCValAssign &VA = ArgLocs[i];
1691 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001692 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001693 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001694 if (VA.getLocInfo() == CCValAssign::Indirect)
1695 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001696 if (VA.needsCustom()) {
1697 // f64 and vector types are split into multiple registers or
1698 // register/stack-slot combinations. The types will not match
1699 // the registers; give up on memory f64 refs until we figure
1700 // out what to do about this.
1701 if (!VA.isRegLoc())
1702 return false;
1703 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001704 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001705 if (RegVT == MVT::v2f64) {
1706 if (!ArgLocs[++i].isRegLoc())
1707 return false;
1708 if (!ArgLocs[++i].isRegLoc())
1709 return false;
1710 }
1711 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001712 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1713 MFI, MRI, TII))
1714 return false;
1715 }
1716 }
1717 }
1718 }
1719
1720 return true;
1721}
1722
Dan Gohman98ca4f22009-08-05 01:29:28 +00001723SDValue
1724ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001725 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001726 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001727 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001728 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001729
Bob Wilsondee46d72009-04-17 20:35:10 +00001730 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001731 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001732
Bob Wilsondee46d72009-04-17 20:35:10 +00001733 // CCState - Info about the registers and stack slots.
Eric Christopher471e4222011-06-08 23:55:35 +00001734 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1735 getTargetMachine(), RVLocs, *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001736
Dan Gohman98ca4f22009-08-05 01:29:28 +00001737 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001738 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1739 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001740
1741 // If this is the first return lowered for this function, add
1742 // the regs to the liveout set for the function.
1743 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1744 for (unsigned i = 0; i != RVLocs.size(); ++i)
1745 if (RVLocs[i].isRegLoc())
1746 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001747 }
1748
Bob Wilson1f595bb2009-04-17 19:07:39 +00001749 SDValue Flag;
1750
1751 // Copy the result values into the output registers.
1752 for (unsigned i = 0, realRVLocIdx = 0;
1753 i != RVLocs.size();
1754 ++i, ++realRVLocIdx) {
1755 CCValAssign &VA = RVLocs[i];
1756 assert(VA.isRegLoc() && "Can only return in registers!");
1757
Dan Gohmanc9403652010-07-07 15:54:55 +00001758 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001759
1760 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001761 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001762 case CCValAssign::Full: break;
1763 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001764 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001765 break;
1766 }
1767
Bob Wilson1f595bb2009-04-17 19:07:39 +00001768 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001769 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001770 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001771 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1772 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001773 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001774 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001775
1776 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1777 Flag = Chain.getValue(1);
1778 VA = RVLocs[++i]; // skip ahead to next loc
1779 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1780 HalfGPRs.getValue(1), Flag);
1781 Flag = Chain.getValue(1);
1782 VA = RVLocs[++i]; // skip ahead to next loc
1783
1784 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001785 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1786 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001787 }
1788 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1789 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001790 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001791 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001792 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001793 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001794 VA = RVLocs[++i]; // skip ahead to next loc
1795 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1796 Flag);
1797 } else
1798 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1799
Bob Wilsondee46d72009-04-17 20:35:10 +00001800 // Guarantee that all emitted copies are
1801 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001802 Flag = Chain.getValue(1);
1803 }
1804
1805 SDValue result;
1806 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001807 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001808 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001809 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001810
1811 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001812}
1813
Evan Cheng3d2125c2010-11-30 23:55:39 +00001814bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1815 if (N->getNumValues() != 1)
1816 return false;
1817 if (!N->hasNUsesOfValue(1, 0))
1818 return false;
1819
1820 unsigned NumCopies = 0;
1821 SDNode* Copies[2];
1822 SDNode *Use = *N->use_begin();
1823 if (Use->getOpcode() == ISD::CopyToReg) {
1824 Copies[NumCopies++] = Use;
1825 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1826 // f64 returned in a pair of GPRs.
1827 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1828 UI != UE; ++UI) {
1829 if (UI->getOpcode() != ISD::CopyToReg)
1830 return false;
1831 Copies[UI.getUse().getResNo()] = *UI;
1832 ++NumCopies;
1833 }
1834 } else if (Use->getOpcode() == ISD::BITCAST) {
1835 // f32 returned in a single GPR.
1836 if (!Use->hasNUsesOfValue(1, 0))
1837 return false;
1838 Use = *Use->use_begin();
1839 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1840 return false;
1841 Copies[NumCopies++] = Use;
1842 } else {
1843 return false;
1844 }
1845
1846 if (NumCopies != 1 && NumCopies != 2)
1847 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001848
1849 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001850 for (unsigned i = 0; i < NumCopies; ++i) {
1851 SDNode *Copy = Copies[i];
1852 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1853 UI != UE; ++UI) {
1854 if (UI->getOpcode() == ISD::CopyToReg) {
1855 SDNode *Use = *UI;
1856 if (Use == Copies[0] || Use == Copies[1])
1857 continue;
1858 return false;
1859 }
1860 if (UI->getOpcode() != ARMISD::RET_FLAG)
1861 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001862 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001863 }
1864 }
1865
Evan Cheng1bf891a2010-12-01 22:59:46 +00001866 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001867}
1868
Evan Cheng485fafc2011-03-21 01:19:09 +00001869bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1870 if (!EnableARMTailCalls)
1871 return false;
1872
1873 if (!CI->isTailCall())
1874 return false;
1875
1876 return !Subtarget->isThumb1Only();
1877}
1878
Bob Wilsonb62d2572009-11-03 00:02:05 +00001879// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1880// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1881// one of the above mentioned nodes. It has to be wrapped because otherwise
1882// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1883// be used to form addressing mode. These wrapped nodes will be selected
1884// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001885static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001886 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001887 // FIXME there is no actual debug info here
1888 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001889 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001890 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001891 if (CP->isMachineConstantPoolEntry())
1892 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1893 CP->getAlignment());
1894 else
1895 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1896 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001897 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001898}
1899
Jim Grosbache1102ca2010-07-19 17:20:38 +00001900unsigned ARMTargetLowering::getJumpTableEncoding() const {
1901 return MachineJumpTableInfo::EK_Inline;
1902}
1903
Dan Gohmand858e902010-04-17 15:26:15 +00001904SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1905 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001906 MachineFunction &MF = DAG.getMachineFunction();
1907 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1908 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001909 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001910 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001911 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001912 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1913 SDValue CPAddr;
1914 if (RelocM == Reloc::Static) {
1915 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1916 } else {
1917 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001918 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001919 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1920 ARMCP::CPBlockAddress,
1921 PCAdj);
1922 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1923 }
1924 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1925 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001926 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001927 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001928 if (RelocM == Reloc::Static)
1929 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001930 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001931 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001932}
1933
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001934// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001935SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001936ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001937 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001938 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001939 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001940 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001941 MachineFunction &MF = DAG.getMachineFunction();
1942 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001943 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001944 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001945 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001946 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001947 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001948 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001949 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001950 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001951 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001952 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001953
Evan Chenge7e0d622009-11-06 22:24:13 +00001954 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001955 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001956
1957 // call __tls_get_addr.
1958 ArgListTy Args;
1959 ArgListEntry Entry;
1960 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001961 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001962 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001963 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001964 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001965 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1966 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001967 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001968 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001969 return CallResult.first;
1970}
1971
1972// Lower ISD::GlobalTLSAddress using the "initial exec" or
1973// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001974SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001975ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001976 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001977 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001978 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001979 SDValue Offset;
1980 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001981 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001982 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001983 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001984
Chris Lattner4fb63d02009-07-15 04:12:33 +00001985 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001986 MachineFunction &MF = DAG.getMachineFunction();
1987 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001988 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00001989 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001990 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1991 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001992 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001993 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001994 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001995 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001996 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001997 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001998 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001999 Chain = Offset.getValue(1);
2000
Evan Chenge7e0d622009-11-06 22:24:13 +00002001 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002002 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002003
Evan Cheng9eda6892009-10-31 03:39:36 +00002004 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002005 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002006 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002007 } else {
2008 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002009 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002010 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002011 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002012 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002013 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002014 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002015 }
2016
2017 // The address of the thread local variable is the add of the thread
2018 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002019 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002020}
2021
Dan Gohman475871a2008-07-27 21:46:04 +00002022SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002023ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002024 // TODO: implement the "local dynamic" model
2025 assert(Subtarget->isTargetELF() &&
2026 "TLS not implemented for non-ELF targets");
2027 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2028 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2029 // otherwise use the "Local Exec" TLS Model
2030 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2031 return LowerToTLSGeneralDynamicModel(GA, DAG);
2032 else
2033 return LowerToTLSExecModels(GA, DAG);
2034}
2035
Dan Gohman475871a2008-07-27 21:46:04 +00002036SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002037 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002038 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002039 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002040 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002041 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2042 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002043 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002044 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002045 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002046 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002047 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002048 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002049 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002050 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002051 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002052 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002053 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002054 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002055 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002056 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002057 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002058 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002059 }
2060
2061 // If we have T2 ops, we can materialize the address directly via movt/movw
2062 // pair. This is always cheaper.
2063 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002064 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002065 // FIXME: Once remat is capable of dealing with instructions with register
2066 // operands, expand this into two nodes.
2067 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2068 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002069 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002070 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2071 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2072 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2073 MachinePointerInfo::getConstantPool(),
2074 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002075 }
2076}
2077
Dan Gohman475871a2008-07-27 21:46:04 +00002078SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002079 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002080 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002081 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002082 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002083 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002084 MachineFunction &MF = DAG.getMachineFunction();
2085 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2086
Evan Cheng4abce0c2011-05-27 20:11:27 +00002087 // FIXME: Enable this for static codegen when tool issues are fixed.
2088 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002089 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002090 // FIXME: Once remat is capable of dealing with instructions with register
2091 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002092 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002093 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2094 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2095
Evan Cheng53519f02011-01-21 18:55:51 +00002096 unsigned Wrapper = (RelocM == Reloc::PIC_)
2097 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2098 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002099 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002100 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2101 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2102 MachinePointerInfo::getGOT(), false, false, 0);
2103 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002104 }
2105
2106 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002107 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002108 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002109 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002110 } else {
2111 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002112 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2113 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002114 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002115 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002116 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002117 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002118
Evan Cheng9eda6892009-10-31 03:39:36 +00002119 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002120 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002121 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002122 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002123
2124 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002125 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002126 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002127 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002128
Evan Cheng63476a82009-09-03 07:04:02 +00002129 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002130 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002131 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002132
2133 return Result;
2134}
2135
Dan Gohman475871a2008-07-27 21:46:04 +00002136SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002137 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002138 assert(Subtarget->isTargetELF() &&
2139 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002140 MachineFunction &MF = DAG.getMachineFunction();
2141 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002142 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002143 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002144 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002145 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002146 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2147 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002148 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002149 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002150 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002151 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002152 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002153 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002154 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002155 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002156}
2157
Jim Grosbach0e0da732009-05-12 23:59:14 +00002158SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002159ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2160 const {
2161 DebugLoc dl = Op.getDebugLoc();
2162 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
Bill Wendling61512ba2011-05-11 01:11:55 +00002163 Op.getOperand(0), Op.getOperand(1));
Jim Grosbache4ad3872010-10-19 23:27:08 +00002164}
2165
2166SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002167ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2168 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002169 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002170 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2171 Op.getOperand(1), Val);
2172}
2173
2174SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002175ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2176 DebugLoc dl = Op.getDebugLoc();
2177 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2178 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2179}
2180
2181SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002182ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002183 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002184 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002185 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002186 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002187 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002188 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002189 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002190 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2191 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002192 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002193 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002194 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002195 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002196 EVT PtrVT = getPointerTy();
2197 DebugLoc dl = Op.getDebugLoc();
2198 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2199 SDValue CPAddr;
2200 unsigned PCAdj = (RelocM != Reloc::PIC_)
2201 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002202 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002203 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2204 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002205 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002206 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002207 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002208 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002209 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002210 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002211
2212 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002213 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002214 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2215 }
2216 return Result;
2217 }
Evan Cheng92e39162011-03-29 23:06:19 +00002218 case Intrinsic::arm_neon_vmulls:
2219 case Intrinsic::arm_neon_vmullu: {
2220 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2221 ? ARMISD::VMULLs : ARMISD::VMULLu;
2222 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2223 Op.getOperand(1), Op.getOperand(2));
2224 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002225 }
2226}
2227
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002228static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002229 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002230 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002231 if (!Subtarget->hasDataBarrier()) {
2232 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2233 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2234 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002235 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002236 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002237 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002238 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002239 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002240
2241 SDValue Op5 = Op.getOperand(5);
2242 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2243 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2244 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2245 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2246
2247 ARM_MB::MemBOpt DMBOpt;
2248 if (isDeviceBarrier)
2249 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2250 else
2251 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2252 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2253 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002254}
2255
Evan Chengdfed19f2010-11-03 06:34:55 +00002256static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2257 const ARMSubtarget *Subtarget) {
2258 // ARM pre v5TE and Thumb1 does not have preload instructions.
2259 if (!(Subtarget->isThumb2() ||
2260 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2261 // Just preserve the chain.
2262 return Op.getOperand(0);
2263
2264 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002265 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2266 if (!isRead &&
2267 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2268 // ARMv7 with MP extension has PLDW.
2269 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002270
2271 if (Subtarget->isThumb())
2272 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002273 isRead = ~isRead & 1;
2274 unsigned isData = Subtarget->isThumb() ? 0 : 1;
Evan Chengdfed19f2010-11-03 06:34:55 +00002275
Evan Cheng416941d2010-11-04 05:19:35 +00002276 // Currently there is no intrinsic that matches pli.
Evan Chengdfed19f2010-11-03 06:34:55 +00002277 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002278 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2279 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002280}
2281
Dan Gohman1e93df62010-04-17 14:41:14 +00002282static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2283 MachineFunction &MF = DAG.getMachineFunction();
2284 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2285
Evan Chenga8e29892007-01-19 07:51:42 +00002286 // vastart just stores the address of the VarArgsFrameIndex slot into the
2287 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002288 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002289 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002290 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002291 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002292 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2293 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002294}
2295
Dan Gohman475871a2008-07-27 21:46:04 +00002296SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002297ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2298 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002299 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002300 MachineFunction &MF = DAG.getMachineFunction();
2301 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2302
2303 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002304 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002305 RC = ARM::tGPRRegisterClass;
2306 else
2307 RC = ARM::GPRRegisterClass;
2308
2309 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002310 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002311 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002312
2313 SDValue ArgValue2;
2314 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002315 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002316 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002317
2318 // Create load node to retrieve arguments from the stack.
2319 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002320 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002321 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002322 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002323 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002324 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002325 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002326 }
2327
Jim Grosbache5165492009-11-09 00:11:35 +00002328 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002329}
2330
Stuart Hastingsc7315872011-04-20 16:47:52 +00002331void
2332ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2333 unsigned &VARegSize, unsigned &VARegSaveSize)
2334 const {
2335 unsigned NumGPRs;
2336 if (CCInfo.isFirstByValRegValid())
2337 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2338 else {
2339 unsigned int firstUnalloced;
2340 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2341 sizeof(GPRArgRegs) /
2342 sizeof(GPRArgRegs[0]));
2343 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2344 }
2345
2346 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2347 VARegSize = NumGPRs * 4;
2348 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2349}
2350
2351// The remaining GPRs hold either the beginning of variable-argument
2352// data, or the beginning of an aggregate passed by value (usuall
2353// byval). Either way, we allocate stack slots adjacent to the data
2354// provided by our caller, and store the unallocated registers there.
2355// If this is a variadic function, the va_list pointer will begin with
2356// these values; otherwise, this reassembles a (byval) structure that
2357// was split between registers and memory.
2358void
2359ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2360 DebugLoc dl, SDValue &Chain,
2361 unsigned ArgOffset) const {
2362 MachineFunction &MF = DAG.getMachineFunction();
2363 MachineFrameInfo *MFI = MF.getFrameInfo();
2364 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2365 unsigned firstRegToSaveIndex;
2366 if (CCInfo.isFirstByValRegValid())
2367 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2368 else {
2369 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2370 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2371 }
2372
2373 unsigned VARegSize, VARegSaveSize;
2374 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2375 if (VARegSaveSize) {
2376 // If this function is vararg, store any remaining integer argument regs
2377 // to their spots on the stack so that they may be loaded by deferencing
2378 // the result of va_next.
2379 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002380 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2381 ArgOffset + VARegSaveSize
2382 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002383 false));
2384 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2385 getPointerTy());
2386
2387 SmallVector<SDValue, 4> MemOps;
2388 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2389 TargetRegisterClass *RC;
2390 if (AFI->isThumb1OnlyFunction())
2391 RC = ARM::tGPRRegisterClass;
2392 else
2393 RC = ARM::GPRRegisterClass;
2394
2395 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2396 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2397 SDValue Store =
2398 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002399 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002400 false, false, 0);
2401 MemOps.push_back(Store);
2402 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2403 DAG.getConstant(4, getPointerTy()));
2404 }
2405 if (!MemOps.empty())
2406 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2407 &MemOps[0], MemOps.size());
2408 } else
2409 // This will point to the next argument passed via stack.
2410 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2411}
2412
Bob Wilson5bafff32009-06-22 23:27:02 +00002413SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002414ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002415 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002416 const SmallVectorImpl<ISD::InputArg>
2417 &Ins,
2418 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002419 SmallVectorImpl<SDValue> &InVals)
2420 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002421 MachineFunction &MF = DAG.getMachineFunction();
2422 MachineFrameInfo *MFI = MF.getFrameInfo();
2423
Bob Wilson1f595bb2009-04-17 19:07:39 +00002424 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2425
2426 // Assign locations to all of the incoming arguments.
2427 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002428 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2429 getTargetMachine(), ArgLocs, *DAG.getContext());
Stuart Hastingsc7315872011-04-20 16:47:52 +00002430 CCInfo.setCallOrPrologue(Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002431 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002432 CCAssignFnForNode(CallConv, /* Return*/ false,
2433 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002434
2435 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002436 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002437
Stuart Hastingsf222e592011-02-28 17:17:53 +00002438 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002439 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2440 CCValAssign &VA = ArgLocs[i];
2441
Bob Wilsondee46d72009-04-17 20:35:10 +00002442 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002443 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002444 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002445
Bob Wilson1f595bb2009-04-17 19:07:39 +00002446 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002447 // f64 and vector types are split up into multiple registers or
2448 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002449 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002450 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002451 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002452 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002453 SDValue ArgValue2;
2454 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002455 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002456 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2457 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002458 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002459 false, false, 0);
2460 } else {
2461 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2462 Chain, DAG, dl);
2463 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002464 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2465 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002466 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002467 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002468 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2469 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002470 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002471
Bob Wilson5bafff32009-06-22 23:27:02 +00002472 } else {
2473 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002474
Owen Anderson825b72b2009-08-11 20:47:22 +00002475 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002476 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002477 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002478 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002479 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002480 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002481 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002482 RC = (AFI->isThumb1OnlyFunction() ?
2483 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002484 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002485 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002486
2487 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002488 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002489 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002490 }
2491
2492 // If this is an 8 or 16-bit value, it is really passed promoted
2493 // to 32 bits. Insert an assert[sz]ext to capture this, then
2494 // truncate to the right size.
2495 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002496 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002497 case CCValAssign::Full: break;
2498 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002499 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002500 break;
2501 case CCValAssign::SExt:
2502 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2503 DAG.getValueType(VA.getValVT()));
2504 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2505 break;
2506 case CCValAssign::ZExt:
2507 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2508 DAG.getValueType(VA.getValVT()));
2509 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2510 break;
2511 }
2512
Dan Gohman98ca4f22009-08-05 01:29:28 +00002513 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002514
2515 } else { // VA.isRegLoc()
2516
2517 // sanity check
2518 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002519 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002520
Stuart Hastingsf222e592011-02-28 17:17:53 +00002521 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002522
Stuart Hastingsf222e592011-02-28 17:17:53 +00002523 // Some Ins[] entries become multiple ArgLoc[] entries.
2524 // Process them only once.
2525 if (index != lastInsIndex)
2526 {
2527 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002528 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002529 // This can be changed with more analysis.
2530 // In case of tail call optimization mark all arguments mutable.
2531 // Since they could be overwritten by lowering of arguments in case of
2532 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002533 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002534 unsigned VARegSize, VARegSaveSize;
2535 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2536 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2537 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002538 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002539 int FI = MFI->CreateFixedObject(Bytes,
2540 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002541 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2542 } else {
2543 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2544 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002545
Stuart Hastingsf222e592011-02-28 17:17:53 +00002546 // Create load nodes to retrieve arguments from the stack.
2547 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2548 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2549 MachinePointerInfo::getFixedStack(FI),
2550 false, false, 0));
2551 }
2552 lastInsIndex = index;
2553 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002554 }
2555 }
2556
2557 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002558 if (isVarArg)
2559 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002560
Dan Gohman98ca4f22009-08-05 01:29:28 +00002561 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002562}
2563
2564/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002565static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002566 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002567 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002568 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002569 // Maybe this has already been legalized into the constant pool?
2570 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002571 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002572 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002573 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002574 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002575 }
2576 }
2577 return false;
2578}
2579
Evan Chenga8e29892007-01-19 07:51:42 +00002580/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2581/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002582SDValue
2583ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002584 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002585 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002586 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002587 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002588 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002589 // Constant does not fit, try adjusting it by one?
2590 switch (CC) {
2591 default: break;
2592 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002593 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002594 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002595 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002596 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002597 }
2598 break;
2599 case ISD::SETULT:
2600 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002601 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002602 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002603 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002604 }
2605 break;
2606 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002607 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002608 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002609 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002610 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002611 }
2612 break;
2613 case ISD::SETULE:
2614 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002615 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002616 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002617 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002618 }
2619 break;
2620 }
2621 }
2622 }
2623
2624 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002625 ARMISD::NodeType CompareType;
2626 switch (CondCode) {
2627 default:
2628 CompareType = ARMISD::CMP;
2629 break;
2630 case ARMCC::EQ:
2631 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002632 // Uses only Z Flag
2633 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002634 break;
2635 }
Evan Cheng218977b2010-07-13 19:27:42 +00002636 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002637 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002638}
2639
2640/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002641SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002642ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002643 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002644 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002645 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002646 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002647 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002648 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2649 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002650}
2651
Bob Wilson79f56c92011-03-08 01:17:20 +00002652/// duplicateCmp - Glue values can have only one use, so this function
2653/// duplicates a comparison node.
2654SDValue
2655ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2656 unsigned Opc = Cmp.getOpcode();
2657 DebugLoc DL = Cmp.getDebugLoc();
2658 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2659 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2660
2661 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2662 Cmp = Cmp.getOperand(0);
2663 Opc = Cmp.getOpcode();
2664 if (Opc == ARMISD::CMPFP)
2665 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2666 else {
2667 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2668 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2669 }
2670 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2671}
2672
Bill Wendlingde2b1512010-08-11 08:43:16 +00002673SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2674 SDValue Cond = Op.getOperand(0);
2675 SDValue SelectTrue = Op.getOperand(1);
2676 SDValue SelectFalse = Op.getOperand(2);
2677 DebugLoc dl = Op.getDebugLoc();
2678
2679 // Convert:
2680 //
2681 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2682 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2683 //
2684 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2685 const ConstantSDNode *CMOVTrue =
2686 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2687 const ConstantSDNode *CMOVFalse =
2688 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2689
2690 if (CMOVTrue && CMOVFalse) {
2691 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2692 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2693
2694 SDValue True;
2695 SDValue False;
2696 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2697 True = SelectTrue;
2698 False = SelectFalse;
2699 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2700 True = SelectFalse;
2701 False = SelectTrue;
2702 }
2703
2704 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002705 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002706 SDValue ARMcc = Cond.getOperand(2);
2707 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002708 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002709 assert(True.getValueType() == VT);
2710 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002711 }
2712 }
2713 }
2714
2715 return DAG.getSelectCC(dl, Cond,
2716 DAG.getConstant(0, Cond.getValueType()),
2717 SelectTrue, SelectFalse, ISD::SETNE);
2718}
2719
Dan Gohmand858e902010-04-17 15:26:15 +00002720SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002721 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002722 SDValue LHS = Op.getOperand(0);
2723 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002724 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002725 SDValue TrueVal = Op.getOperand(2);
2726 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002727 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002728
Owen Anderson825b72b2009-08-11 20:47:22 +00002729 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002730 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002731 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002732 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2733 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002734 }
2735
2736 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002737 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002738
Evan Cheng218977b2010-07-13 19:27:42 +00002739 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2740 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002741 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002742 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002743 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002744 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002745 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002746 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002747 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002748 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002749 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002750 }
2751 return Result;
2752}
2753
Evan Cheng218977b2010-07-13 19:27:42 +00002754/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2755/// to morph to an integer compare sequence.
2756static bool canChangeToInt(SDValue Op, bool &SeenZero,
2757 const ARMSubtarget *Subtarget) {
2758 SDNode *N = Op.getNode();
2759 if (!N->hasOneUse())
2760 // Otherwise it requires moving the value from fp to integer registers.
2761 return false;
2762 if (!N->getNumValues())
2763 return false;
2764 EVT VT = Op.getValueType();
2765 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2766 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2767 // vmrs are very slow, e.g. cortex-a8.
2768 return false;
2769
2770 if (isFloatingPointZero(Op)) {
2771 SeenZero = true;
2772 return true;
2773 }
2774 return ISD::isNormalLoad(N);
2775}
2776
2777static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2778 if (isFloatingPointZero(Op))
2779 return DAG.getConstant(0, MVT::i32);
2780
2781 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2782 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002783 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002784 Ld->isVolatile(), Ld->isNonTemporal(),
2785 Ld->getAlignment());
2786
2787 llvm_unreachable("Unknown VFP cmp argument!");
2788}
2789
2790static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2791 SDValue &RetVal1, SDValue &RetVal2) {
2792 if (isFloatingPointZero(Op)) {
2793 RetVal1 = DAG.getConstant(0, MVT::i32);
2794 RetVal2 = DAG.getConstant(0, MVT::i32);
2795 return;
2796 }
2797
2798 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2799 SDValue Ptr = Ld->getBasePtr();
2800 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2801 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002802 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002803 Ld->isVolatile(), Ld->isNonTemporal(),
2804 Ld->getAlignment());
2805
2806 EVT PtrType = Ptr.getValueType();
2807 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2808 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2809 PtrType, Ptr, DAG.getConstant(4, PtrType));
2810 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2811 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002812 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002813 Ld->isVolatile(), Ld->isNonTemporal(),
2814 NewAlign);
2815 return;
2816 }
2817
2818 llvm_unreachable("Unknown VFP cmp argument!");
2819}
2820
2821/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2822/// f32 and even f64 comparisons to integer ones.
2823SDValue
2824ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2825 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002826 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002827 SDValue LHS = Op.getOperand(2);
2828 SDValue RHS = Op.getOperand(3);
2829 SDValue Dest = Op.getOperand(4);
2830 DebugLoc dl = Op.getDebugLoc();
2831
2832 bool SeenZero = false;
2833 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2834 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002835 // If one of the operand is zero, it's safe to ignore the NaN case since
2836 // we only care about equality comparisons.
2837 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002838 // If unsafe fp math optimization is enabled and there are no other uses of
2839 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002840 // to an integer comparison.
2841 if (CC == ISD::SETOEQ)
2842 CC = ISD::SETEQ;
2843 else if (CC == ISD::SETUNE)
2844 CC = ISD::SETNE;
2845
2846 SDValue ARMcc;
2847 if (LHS.getValueType() == MVT::f32) {
2848 LHS = bitcastf32Toi32(LHS, DAG);
2849 RHS = bitcastf32Toi32(RHS, DAG);
2850 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2851 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2852 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2853 Chain, Dest, ARMcc, CCR, Cmp);
2854 }
2855
2856 SDValue LHS1, LHS2;
2857 SDValue RHS1, RHS2;
2858 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2859 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2860 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2861 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002862 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002863 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2864 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2865 }
2866
2867 return SDValue();
2868}
2869
2870SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2871 SDValue Chain = Op.getOperand(0);
2872 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2873 SDValue LHS = Op.getOperand(2);
2874 SDValue RHS = Op.getOperand(3);
2875 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002876 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002877
Owen Anderson825b72b2009-08-11 20:47:22 +00002878 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002879 SDValue ARMcc;
2880 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002881 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002882 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002883 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002884 }
2885
Owen Anderson825b72b2009-08-11 20:47:22 +00002886 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002887
2888 if (UnsafeFPMath &&
2889 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2890 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2891 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2892 if (Result.getNode())
2893 return Result;
2894 }
2895
Evan Chenga8e29892007-01-19 07:51:42 +00002896 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002897 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002898
Evan Cheng218977b2010-07-13 19:27:42 +00002899 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2900 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002901 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002902 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002903 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002904 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002905 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002906 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2907 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002908 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002909 }
2910 return Res;
2911}
2912
Dan Gohmand858e902010-04-17 15:26:15 +00002913SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002914 SDValue Chain = Op.getOperand(0);
2915 SDValue Table = Op.getOperand(1);
2916 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002917 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002918
Owen Andersone50ed302009-08-10 22:56:29 +00002919 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002920 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2921 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002922 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002923 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002924 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002925 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2926 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002927 if (Subtarget->isThumb2()) {
2928 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2929 // which does another jump to the destination. This also makes it easier
2930 // to translate it to TBB / TBH later.
2931 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002932 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002933 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002934 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002935 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002936 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002937 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002938 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002939 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002940 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002941 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002942 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002943 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002944 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002945 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002946 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002947 }
Evan Chenga8e29892007-01-19 07:51:42 +00002948}
2949
Bob Wilson76a312b2010-03-19 22:51:32 +00002950static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2951 DebugLoc dl = Op.getDebugLoc();
2952 unsigned Opc;
2953
2954 switch (Op.getOpcode()) {
2955 default:
2956 assert(0 && "Invalid opcode!");
2957 case ISD::FP_TO_SINT:
2958 Opc = ARMISD::FTOSI;
2959 break;
2960 case ISD::FP_TO_UINT:
2961 Opc = ARMISD::FTOUI;
2962 break;
2963 }
2964 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002965 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00002966}
2967
Cameron Zwarich3007d332011-03-29 21:41:55 +00002968static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2969 EVT VT = Op.getValueType();
2970 DebugLoc dl = Op.getDebugLoc();
2971
2972 EVT OperandVT = Op.getOperand(0).getValueType();
2973 assert(OperandVT == MVT::v4i16 && "Invalid type for custom lowering!");
2974 if (VT != MVT::v4f32)
2975 return DAG.UnrollVectorOp(Op.getNode());
2976
2977 unsigned CastOpc;
2978 unsigned Opc;
2979 switch (Op.getOpcode()) {
2980 default:
2981 assert(0 && "Invalid opcode!");
2982 case ISD::SINT_TO_FP:
2983 CastOpc = ISD::SIGN_EXTEND;
2984 Opc = ISD::SINT_TO_FP;
2985 break;
2986 case ISD::UINT_TO_FP:
2987 CastOpc = ISD::ZERO_EXTEND;
2988 Opc = ISD::UINT_TO_FP;
2989 break;
2990 }
2991
2992 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
2993 return DAG.getNode(Opc, dl, VT, Op);
2994}
2995
Bob Wilson76a312b2010-03-19 22:51:32 +00002996static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2997 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00002998 if (VT.isVector())
2999 return LowerVectorINT_TO_FP(Op, DAG);
3000
Bob Wilson76a312b2010-03-19 22:51:32 +00003001 DebugLoc dl = Op.getDebugLoc();
3002 unsigned Opc;
3003
3004 switch (Op.getOpcode()) {
3005 default:
3006 assert(0 && "Invalid opcode!");
3007 case ISD::SINT_TO_FP:
3008 Opc = ARMISD::SITOF;
3009 break;
3010 case ISD::UINT_TO_FP:
3011 Opc = ARMISD::UITOF;
3012 break;
3013 }
3014
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003015 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003016 return DAG.getNode(Opc, dl, VT, Op);
3017}
3018
Evan Cheng515fe3a2010-07-08 02:08:50 +00003019SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003020 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003021 SDValue Tmp0 = Op.getOperand(0);
3022 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003023 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003024 EVT VT = Op.getValueType();
3025 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003026 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3027 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3028 bool UseNEON = !InGPR && Subtarget->hasNEON();
3029
3030 if (UseNEON) {
3031 // Use VBSL to copy the sign bit.
3032 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3033 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3034 DAG.getTargetConstant(EncodedVal, MVT::i32));
3035 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3036 if (VT == MVT::f64)
3037 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3038 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3039 DAG.getConstant(32, MVT::i32));
3040 else /*if (VT == MVT::f32)*/
3041 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3042 if (SrcVT == MVT::f32) {
3043 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3044 if (VT == MVT::f64)
3045 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3046 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3047 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003048 } else if (VT == MVT::f32)
3049 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3050 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3051 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003052 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3053 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3054
3055 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3056 MVT::i32);
3057 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3058 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3059 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003060
Evan Chenge573fb32011-02-23 02:24:55 +00003061 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3062 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3063 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003064 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003065 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3066 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3067 DAG.getConstant(0, MVT::i32));
3068 } else {
3069 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3070 }
3071
3072 return Res;
3073 }
Evan Chengc143dd42011-02-11 02:28:55 +00003074
3075 // Bitcast operand 1 to i32.
3076 if (SrcVT == MVT::f64)
3077 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3078 &Tmp1, 1).getValue(1);
3079 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3080
Evan Chenge573fb32011-02-23 02:24:55 +00003081 // Or in the signbit with integer operations.
3082 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3083 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3084 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3085 if (VT == MVT::f32) {
3086 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3087 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3088 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3089 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003090 }
3091
Evan Chenge573fb32011-02-23 02:24:55 +00003092 // f64: Or the high part with signbit and then combine two parts.
3093 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3094 &Tmp0, 1);
3095 SDValue Lo = Tmp0.getValue(0);
3096 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3097 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3098 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003099}
3100
Evan Cheng2457f2c2010-05-22 01:47:14 +00003101SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3102 MachineFunction &MF = DAG.getMachineFunction();
3103 MachineFrameInfo *MFI = MF.getFrameInfo();
3104 MFI->setReturnAddressIsTaken(true);
3105
3106 EVT VT = Op.getValueType();
3107 DebugLoc dl = Op.getDebugLoc();
3108 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3109 if (Depth) {
3110 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3111 SDValue Offset = DAG.getConstant(4, MVT::i32);
3112 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3113 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003114 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003115 }
3116
3117 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003118 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003119 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3120}
3121
Dan Gohmand858e902010-04-17 15:26:15 +00003122SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003123 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3124 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003125
Owen Andersone50ed302009-08-10 22:56:29 +00003126 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003127 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3128 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003129 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003130 ? ARM::R7 : ARM::R11;
3131 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3132 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003133 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3134 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00003135 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003136 return FrameAddr;
3137}
3138
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003139/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003140/// expand a bit convert where either the source or destination type is i64 to
3141/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3142/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3143/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003144static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003145 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3146 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003147 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003148
Bob Wilson9f3f0612010-04-17 05:30:19 +00003149 // This function is only supposed to be called for i64 types, either as the
3150 // source or destination of the bit convert.
3151 EVT SrcVT = Op.getValueType();
3152 EVT DstVT = N->getValueType(0);
3153 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003154 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003155
Bob Wilson9f3f0612010-04-17 05:30:19 +00003156 // Turn i64->f64 into VMOVDRR.
3157 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003158 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3159 DAG.getConstant(0, MVT::i32));
3160 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3161 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003162 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003163 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003164 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003165
Jim Grosbache5165492009-11-09 00:11:35 +00003166 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003167 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3168 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3169 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3170 // Merge the pieces into a single i64 value.
3171 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3172 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003173
Bob Wilson9f3f0612010-04-17 05:30:19 +00003174 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003175}
3176
Bob Wilson5bafff32009-06-22 23:27:02 +00003177/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003178/// Zero vectors are used to represent vector negation and in those cases
3179/// will be implemented with the NEON VNEG instruction. However, VNEG does
3180/// not support i64 elements, so sometimes the zero vectors will need to be
3181/// explicitly constructed. Regardless, use a canonical VMOV to create the
3182/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003183static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003184 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003185 // The canonical modified immediate encoding of a zero vector is....0!
3186 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3187 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3188 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003189 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003190}
3191
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003192/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3193/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003194SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3195 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003196 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3197 EVT VT = Op.getValueType();
3198 unsigned VTBits = VT.getSizeInBits();
3199 DebugLoc dl = Op.getDebugLoc();
3200 SDValue ShOpLo = Op.getOperand(0);
3201 SDValue ShOpHi = Op.getOperand(1);
3202 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003203 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003204 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003205
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003206 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3207
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003208 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3209 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3210 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3211 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3212 DAG.getConstant(VTBits, MVT::i32));
3213 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3214 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003215 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003216
3217 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3218 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003219 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003220 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003221 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003222 CCR, Cmp);
3223
3224 SDValue Ops[2] = { Lo, Hi };
3225 return DAG.getMergeValues(Ops, 2, dl);
3226}
3227
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003228/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3229/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003230SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3231 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003232 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3233 EVT VT = Op.getValueType();
3234 unsigned VTBits = VT.getSizeInBits();
3235 DebugLoc dl = Op.getDebugLoc();
3236 SDValue ShOpLo = Op.getOperand(0);
3237 SDValue ShOpHi = Op.getOperand(1);
3238 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003239 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003240
3241 assert(Op.getOpcode() == ISD::SHL_PARTS);
3242 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3243 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3244 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3245 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3246 DAG.getConstant(VTBits, MVT::i32));
3247 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3248 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3249
3250 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3251 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3252 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003253 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003254 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003255 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003256 CCR, Cmp);
3257
3258 SDValue Ops[2] = { Lo, Hi };
3259 return DAG.getMergeValues(Ops, 2, dl);
3260}
3261
Jim Grosbach4725ca72010-09-08 03:54:02 +00003262SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003263 SelectionDAG &DAG) const {
3264 // The rounding mode is in bits 23:22 of the FPSCR.
3265 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3266 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3267 // so that the shift + and get folded into a bitfield extract.
3268 DebugLoc dl = Op.getDebugLoc();
3269 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3270 DAG.getConstant(Intrinsic::arm_get_fpscr,
3271 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003272 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003273 DAG.getConstant(1U << 22, MVT::i32));
3274 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3275 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003276 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003277 DAG.getConstant(3, MVT::i32));
3278}
3279
Jim Grosbach3482c802010-01-18 19:58:49 +00003280static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3281 const ARMSubtarget *ST) {
3282 EVT VT = N->getValueType(0);
3283 DebugLoc dl = N->getDebugLoc();
3284
3285 if (!ST->hasV6T2Ops())
3286 return SDValue();
3287
3288 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3289 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3290}
3291
Bob Wilson5bafff32009-06-22 23:27:02 +00003292static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3293 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003294 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003295 DebugLoc dl = N->getDebugLoc();
3296
Bob Wilsond5448bb2010-11-18 21:16:28 +00003297 if (!VT.isVector())
3298 return SDValue();
3299
Bob Wilson5bafff32009-06-22 23:27:02 +00003300 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003301 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003302
Bob Wilsond5448bb2010-11-18 21:16:28 +00003303 // Left shifts translate directly to the vshiftu intrinsic.
3304 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003305 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003306 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3307 N->getOperand(0), N->getOperand(1));
3308
3309 assert((N->getOpcode() == ISD::SRA ||
3310 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3311
3312 // NEON uses the same intrinsics for both left and right shifts. For
3313 // right shifts, the shift amounts are negative, so negate the vector of
3314 // shift amounts.
3315 EVT ShiftVT = N->getOperand(1).getValueType();
3316 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3317 getZeroVector(ShiftVT, DAG, dl),
3318 N->getOperand(1));
3319 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3320 Intrinsic::arm_neon_vshifts :
3321 Intrinsic::arm_neon_vshiftu);
3322 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3323 DAG.getConstant(vshiftInt, MVT::i32),
3324 N->getOperand(0), NegatedCount);
3325}
3326
3327static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3328 const ARMSubtarget *ST) {
3329 EVT VT = N->getValueType(0);
3330 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003331
Eli Friedmance392eb2009-08-22 03:13:10 +00003332 // We can get here for a node like i32 = ISD::SHL i32, i64
3333 if (VT != MVT::i64)
3334 return SDValue();
3335
3336 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003337 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003338
Chris Lattner27a6c732007-11-24 07:07:01 +00003339 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3340 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003341 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003342 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003343
Chris Lattner27a6c732007-11-24 07:07:01 +00003344 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003345 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003346
Chris Lattner27a6c732007-11-24 07:07:01 +00003347 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003348 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003349 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003350 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003351 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003352
Chris Lattner27a6c732007-11-24 07:07:01 +00003353 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3354 // captures the result into a carry flag.
3355 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003356 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003357
Chris Lattner27a6c732007-11-24 07:07:01 +00003358 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003359 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003360
Chris Lattner27a6c732007-11-24 07:07:01 +00003361 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003362 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003363}
3364
Bob Wilson5bafff32009-06-22 23:27:02 +00003365static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3366 SDValue TmpOp0, TmpOp1;
3367 bool Invert = false;
3368 bool Swap = false;
3369 unsigned Opc = 0;
3370
3371 SDValue Op0 = Op.getOperand(0);
3372 SDValue Op1 = Op.getOperand(1);
3373 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003374 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003375 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3376 DebugLoc dl = Op.getDebugLoc();
3377
3378 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3379 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003380 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003381 case ISD::SETUNE:
3382 case ISD::SETNE: Invert = true; // Fallthrough
3383 case ISD::SETOEQ:
3384 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3385 case ISD::SETOLT:
3386 case ISD::SETLT: Swap = true; // Fallthrough
3387 case ISD::SETOGT:
3388 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3389 case ISD::SETOLE:
3390 case ISD::SETLE: Swap = true; // Fallthrough
3391 case ISD::SETOGE:
3392 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3393 case ISD::SETUGE: Swap = true; // Fallthrough
3394 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3395 case ISD::SETUGT: Swap = true; // Fallthrough
3396 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3397 case ISD::SETUEQ: Invert = true; // Fallthrough
3398 case ISD::SETONE:
3399 // Expand this to (OLT | OGT).
3400 TmpOp0 = Op0;
3401 TmpOp1 = Op1;
3402 Opc = ISD::OR;
3403 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3404 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3405 break;
3406 case ISD::SETUO: Invert = true; // Fallthrough
3407 case ISD::SETO:
3408 // Expand this to (OLT | OGE).
3409 TmpOp0 = Op0;
3410 TmpOp1 = Op1;
3411 Opc = ISD::OR;
3412 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3413 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3414 break;
3415 }
3416 } else {
3417 // Integer comparisons.
3418 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003419 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003420 case ISD::SETNE: Invert = true;
3421 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3422 case ISD::SETLT: Swap = true;
3423 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3424 case ISD::SETLE: Swap = true;
3425 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3426 case ISD::SETULT: Swap = true;
3427 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3428 case ISD::SETULE: Swap = true;
3429 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3430 }
3431
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003432 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003433 if (Opc == ARMISD::VCEQ) {
3434
3435 SDValue AndOp;
3436 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3437 AndOp = Op0;
3438 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3439 AndOp = Op1;
3440
3441 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003442 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003443 AndOp = AndOp.getOperand(0);
3444
3445 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3446 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003447 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3448 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003449 Invert = !Invert;
3450 }
3451 }
3452 }
3453
3454 if (Swap)
3455 std::swap(Op0, Op1);
3456
Owen Andersonc24cb352010-11-08 23:21:22 +00003457 // If one of the operands is a constant vector zero, attempt to fold the
3458 // comparison to a specialized compare-against-zero form.
3459 SDValue SingleOp;
3460 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3461 SingleOp = Op0;
3462 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3463 if (Opc == ARMISD::VCGE)
3464 Opc = ARMISD::VCLEZ;
3465 else if (Opc == ARMISD::VCGT)
3466 Opc = ARMISD::VCLTZ;
3467 SingleOp = Op1;
3468 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003469
Owen Andersonc24cb352010-11-08 23:21:22 +00003470 SDValue Result;
3471 if (SingleOp.getNode()) {
3472 switch (Opc) {
3473 case ARMISD::VCEQ:
3474 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3475 case ARMISD::VCGE:
3476 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3477 case ARMISD::VCLEZ:
3478 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3479 case ARMISD::VCGT:
3480 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3481 case ARMISD::VCLTZ:
3482 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3483 default:
3484 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3485 }
3486 } else {
3487 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3488 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003489
3490 if (Invert)
3491 Result = DAG.getNOT(dl, Result, VT);
3492
3493 return Result;
3494}
3495
Bob Wilsond3c42842010-06-14 22:19:57 +00003496/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3497/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003498/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003499static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3500 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003501 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003502 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003503
Bob Wilson827b2102010-06-15 19:05:35 +00003504 // SplatBitSize is set to the smallest size that splats the vector, so a
3505 // zero vector will always have SplatBitSize == 8. However, NEON modified
3506 // immediate instructions others than VMOV do not support the 8-bit encoding
3507 // of a zero vector, and the default encoding of zero is supposed to be the
3508 // 32-bit version.
3509 if (SplatBits == 0)
3510 SplatBitSize = 32;
3511
Bob Wilson5bafff32009-06-22 23:27:02 +00003512 switch (SplatBitSize) {
3513 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003514 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003515 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003516 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003517 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003518 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003519 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003520 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003521 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003522
3523 case 16:
3524 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003525 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003526 if ((SplatBits & ~0xff) == 0) {
3527 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003528 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003529 Imm = SplatBits;
3530 break;
3531 }
3532 if ((SplatBits & ~0xff00) == 0) {
3533 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003534 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003535 Imm = SplatBits >> 8;
3536 break;
3537 }
3538 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003539
3540 case 32:
3541 // NEON's 32-bit VMOV supports splat values where:
3542 // * only one byte is nonzero, or
3543 // * the least significant byte is 0xff and the second byte is nonzero, or
3544 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003545 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003546 if ((SplatBits & ~0xff) == 0) {
3547 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003548 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003549 Imm = SplatBits;
3550 break;
3551 }
3552 if ((SplatBits & ~0xff00) == 0) {
3553 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003554 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003555 Imm = SplatBits >> 8;
3556 break;
3557 }
3558 if ((SplatBits & ~0xff0000) == 0) {
3559 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003560 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003561 Imm = SplatBits >> 16;
3562 break;
3563 }
3564 if ((SplatBits & ~0xff000000) == 0) {
3565 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003566 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003567 Imm = SplatBits >> 24;
3568 break;
3569 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003570
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003571 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3572 if (type == OtherModImm) return SDValue();
3573
Bob Wilson5bafff32009-06-22 23:27:02 +00003574 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003575 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3576 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003577 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003578 Imm = SplatBits >> 8;
3579 SplatBits |= 0xff;
3580 break;
3581 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003582
3583 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003584 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3585 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003586 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003587 Imm = SplatBits >> 16;
3588 SplatBits |= 0xffff;
3589 break;
3590 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003591
3592 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3593 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3594 // VMOV.I32. A (very) minor optimization would be to replicate the value
3595 // and fall through here to test for a valid 64-bit splat. But, then the
3596 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003597 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003598
3599 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003600 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003601 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003602 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003603 uint64_t BitMask = 0xff;
3604 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003605 unsigned ImmMask = 1;
3606 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003607 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003608 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003609 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003610 Imm |= ImmMask;
3611 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003612 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003613 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003614 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003615 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003616 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003617 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003618 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003619 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003620 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003621 break;
3622 }
3623
Bob Wilson1a913ed2010-06-11 21:34:50 +00003624 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003625 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003626 return SDValue();
3627 }
3628
Bob Wilsoncba270d2010-07-13 21:16:48 +00003629 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3630 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003631}
3632
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003633static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3634 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003635 unsigned NumElts = VT.getVectorNumElements();
3636 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003637
3638 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3639 if (M[0] < 0)
3640 return false;
3641
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003642 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003643
3644 // If this is a VEXT shuffle, the immediate value is the index of the first
3645 // element. The other shuffle indices must be the successive elements after
3646 // the first one.
3647 unsigned ExpectedElt = Imm;
3648 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003649 // Increment the expected index. If it wraps around, it may still be
3650 // a VEXT but the source vectors must be swapped.
3651 ExpectedElt += 1;
3652 if (ExpectedElt == NumElts * 2) {
3653 ExpectedElt = 0;
3654 ReverseVEXT = true;
3655 }
3656
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003657 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003658 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003659 return false;
3660 }
3661
3662 // Adjust the index value if the source operands will be swapped.
3663 if (ReverseVEXT)
3664 Imm -= NumElts;
3665
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003666 return true;
3667}
3668
Bob Wilson8bb9e482009-07-26 00:39:34 +00003669/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3670/// instruction with the specified blocksize. (The order of the elements
3671/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003672static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3673 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003674 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3675 "Only possible block sizes for VREV are: 16, 32, 64");
3676
Bob Wilson8bb9e482009-07-26 00:39:34 +00003677 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003678 if (EltSz == 64)
3679 return false;
3680
3681 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003682 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003683 // If the first shuffle index is UNDEF, be optimistic.
3684 if (M[0] < 0)
3685 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003686
3687 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3688 return false;
3689
3690 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003691 if (M[i] < 0) continue; // ignore UNDEF indices
3692 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003693 return false;
3694 }
3695
3696 return true;
3697}
3698
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003699static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3700 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3701 // range, then 0 is placed into the resulting vector. So pretty much any mask
3702 // of 8 elements can work here.
3703 return VT == MVT::v8i8 && M.size() == 8;
3704}
3705
Bob Wilsonc692cb72009-08-21 20:54:19 +00003706static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3707 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003708 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3709 if (EltSz == 64)
3710 return false;
3711
Bob Wilsonc692cb72009-08-21 20:54:19 +00003712 unsigned NumElts = VT.getVectorNumElements();
3713 WhichResult = (M[0] == 0 ? 0 : 1);
3714 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003715 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3716 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003717 return false;
3718 }
3719 return true;
3720}
3721
Bob Wilson324f4f12009-12-03 06:40:55 +00003722/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3723/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3724/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3725static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3726 unsigned &WhichResult) {
3727 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3728 if (EltSz == 64)
3729 return false;
3730
3731 unsigned NumElts = VT.getVectorNumElements();
3732 WhichResult = (M[0] == 0 ? 0 : 1);
3733 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003734 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3735 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003736 return false;
3737 }
3738 return true;
3739}
3740
Bob Wilsonc692cb72009-08-21 20:54:19 +00003741static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3742 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003743 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3744 if (EltSz == 64)
3745 return false;
3746
Bob Wilsonc692cb72009-08-21 20:54:19 +00003747 unsigned NumElts = VT.getVectorNumElements();
3748 WhichResult = (M[0] == 0 ? 0 : 1);
3749 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003750 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003751 if ((unsigned) M[i] != 2 * i + WhichResult)
3752 return false;
3753 }
3754
3755 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003756 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003757 return false;
3758
3759 return true;
3760}
3761
Bob Wilson324f4f12009-12-03 06:40:55 +00003762/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3763/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3764/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3765static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3766 unsigned &WhichResult) {
3767 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3768 if (EltSz == 64)
3769 return false;
3770
3771 unsigned Half = VT.getVectorNumElements() / 2;
3772 WhichResult = (M[0] == 0 ? 0 : 1);
3773 for (unsigned j = 0; j != 2; ++j) {
3774 unsigned Idx = WhichResult;
3775 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003776 int MIdx = M[i + j * Half];
3777 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003778 return false;
3779 Idx += 2;
3780 }
3781 }
3782
3783 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3784 if (VT.is64BitVector() && EltSz == 32)
3785 return false;
3786
3787 return true;
3788}
3789
Bob Wilsonc692cb72009-08-21 20:54:19 +00003790static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3791 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003792 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3793 if (EltSz == 64)
3794 return false;
3795
Bob Wilsonc692cb72009-08-21 20:54:19 +00003796 unsigned NumElts = VT.getVectorNumElements();
3797 WhichResult = (M[0] == 0 ? 0 : 1);
3798 unsigned Idx = WhichResult * NumElts / 2;
3799 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003800 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3801 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003802 return false;
3803 Idx += 1;
3804 }
3805
3806 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003807 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003808 return false;
3809
3810 return true;
3811}
3812
Bob Wilson324f4f12009-12-03 06:40:55 +00003813/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3814/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3815/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3816static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3817 unsigned &WhichResult) {
3818 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3819 if (EltSz == 64)
3820 return false;
3821
3822 unsigned NumElts = VT.getVectorNumElements();
3823 WhichResult = (M[0] == 0 ? 0 : 1);
3824 unsigned Idx = WhichResult * NumElts / 2;
3825 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003826 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3827 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003828 return false;
3829 Idx += 1;
3830 }
3831
3832 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3833 if (VT.is64BitVector() && EltSz == 32)
3834 return false;
3835
3836 return true;
3837}
3838
Dale Johannesenf630c712010-07-29 20:10:08 +00003839// If N is an integer constant that can be moved into a register in one
3840// instruction, return an SDValue of such a constant (will become a MOV
3841// instruction). Otherwise return null.
3842static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3843 const ARMSubtarget *ST, DebugLoc dl) {
3844 uint64_t Val;
3845 if (!isa<ConstantSDNode>(N))
3846 return SDValue();
3847 Val = cast<ConstantSDNode>(N)->getZExtValue();
3848
3849 if (ST->isThumb1Only()) {
3850 if (Val <= 255 || ~Val <= 255)
3851 return DAG.getConstant(Val, MVT::i32);
3852 } else {
3853 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3854 return DAG.getConstant(Val, MVT::i32);
3855 }
3856 return SDValue();
3857}
3858
Bob Wilson5bafff32009-06-22 23:27:02 +00003859// If this is a case we can't handle, return null and let the default
3860// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003861SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3862 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003863 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003864 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003865 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003866
3867 APInt SplatBits, SplatUndef;
3868 unsigned SplatBitSize;
3869 bool HasAnyUndefs;
3870 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003871 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003872 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003873 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003874 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003875 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003876 DAG, VmovVT, VT.is128BitVector(),
3877 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003878 if (Val.getNode()) {
3879 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003880 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003881 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003882
3883 // Try an immediate VMVN.
3884 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3885 ((1LL << SplatBitSize) - 1));
3886 Val = isNEONModifiedImm(NegatedImm,
3887 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003888 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003889 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003890 if (Val.getNode()) {
3891 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003892 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003893 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003894 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003895 }
3896
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003897 // Scan through the operands to see if only one value is used.
3898 unsigned NumElts = VT.getVectorNumElements();
3899 bool isOnlyLowElement = true;
3900 bool usesOnlyOneValue = true;
3901 bool isConstant = true;
3902 SDValue Value;
3903 for (unsigned i = 0; i < NumElts; ++i) {
3904 SDValue V = Op.getOperand(i);
3905 if (V.getOpcode() == ISD::UNDEF)
3906 continue;
3907 if (i > 0)
3908 isOnlyLowElement = false;
3909 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3910 isConstant = false;
3911
3912 if (!Value.getNode())
3913 Value = V;
3914 else if (V != Value)
3915 usesOnlyOneValue = false;
3916 }
3917
3918 if (!Value.getNode())
3919 return DAG.getUNDEF(VT);
3920
3921 if (isOnlyLowElement)
3922 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3923
Dale Johannesenf630c712010-07-29 20:10:08 +00003924 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3925
Dale Johannesen575cd142010-10-19 20:00:17 +00003926 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3927 // i32 and try again.
3928 if (usesOnlyOneValue && EltSize <= 32) {
3929 if (!isConstant)
3930 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3931 if (VT.getVectorElementType().isFloatingPoint()) {
3932 SmallVector<SDValue, 8> Ops;
3933 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003934 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003935 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003936 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3937 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003938 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3939 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003940 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003941 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003942 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3943 if (Val.getNode())
3944 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003945 }
3946
3947 // If all elements are constants and the case above didn't get hit, fall back
3948 // to the default expansion, which will generate a load from the constant
3949 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003950 if (isConstant)
3951 return SDValue();
3952
Bob Wilson11a1dff2011-01-07 21:37:30 +00003953 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3954 if (NumElts >= 4) {
3955 SDValue shuffle = ReconstructShuffle(Op, DAG);
3956 if (shuffle != SDValue())
3957 return shuffle;
3958 }
3959
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003960 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003961 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3962 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003963 if (EltSize >= 32) {
3964 // Do the expansion with floating-point types, since that is what the VFP
3965 // registers are defined to use, and since i64 is not legal.
3966 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3967 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003968 SmallVector<SDValue, 8> Ops;
3969 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003970 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003971 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003972 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003973 }
3974
3975 return SDValue();
3976}
3977
Bob Wilson11a1dff2011-01-07 21:37:30 +00003978// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003979// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00003980SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
3981 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00003982 DebugLoc dl = Op.getDebugLoc();
3983 EVT VT = Op.getValueType();
3984 unsigned NumElts = VT.getVectorNumElements();
3985
3986 SmallVector<SDValue, 2> SourceVecs;
3987 SmallVector<unsigned, 2> MinElts;
3988 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003989
Bob Wilson11a1dff2011-01-07 21:37:30 +00003990 for (unsigned i = 0; i < NumElts; ++i) {
3991 SDValue V = Op.getOperand(i);
3992 if (V.getOpcode() == ISD::UNDEF)
3993 continue;
3994 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
3995 // A shuffle can only come from building a vector from various
3996 // elements of other vectors.
3997 return SDValue();
3998 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003999
Bob Wilson11a1dff2011-01-07 21:37:30 +00004000 // Record this extraction against the appropriate vector if possible...
4001 SDValue SourceVec = V.getOperand(0);
4002 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4003 bool FoundSource = false;
4004 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4005 if (SourceVecs[j] == SourceVec) {
4006 if (MinElts[j] > EltNo)
4007 MinElts[j] = EltNo;
4008 if (MaxElts[j] < EltNo)
4009 MaxElts[j] = EltNo;
4010 FoundSource = true;
4011 break;
4012 }
4013 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004014
Bob Wilson11a1dff2011-01-07 21:37:30 +00004015 // Or record a new source if not...
4016 if (!FoundSource) {
4017 SourceVecs.push_back(SourceVec);
4018 MinElts.push_back(EltNo);
4019 MaxElts.push_back(EltNo);
4020 }
4021 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004022
Bob Wilson11a1dff2011-01-07 21:37:30 +00004023 // Currently only do something sane when at most two source vectors
4024 // involved.
4025 if (SourceVecs.size() > 2)
4026 return SDValue();
4027
4028 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4029 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004030
Bob Wilson11a1dff2011-01-07 21:37:30 +00004031 // This loop extracts the usage patterns of the source vectors
4032 // and prepares appropriate SDValues for a shuffle if possible.
4033 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4034 if (SourceVecs[i].getValueType() == VT) {
4035 // No VEXT necessary
4036 ShuffleSrcs[i] = SourceVecs[i];
4037 VEXTOffsets[i] = 0;
4038 continue;
4039 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4040 // It probably isn't worth padding out a smaller vector just to
4041 // break it down again in a shuffle.
4042 return SDValue();
4043 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004044
Bob Wilson11a1dff2011-01-07 21:37:30 +00004045 // Since only 64-bit and 128-bit vectors are legal on ARM and
4046 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004047 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4048 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004049
Bob Wilson11a1dff2011-01-07 21:37:30 +00004050 if (MaxElts[i] - MinElts[i] >= NumElts) {
4051 // Span too large for a VEXT to cope
4052 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004053 }
4054
Bob Wilson11a1dff2011-01-07 21:37:30 +00004055 if (MinElts[i] >= NumElts) {
4056 // The extraction can just take the second half
4057 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004058 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4059 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004060 DAG.getIntPtrConstant(NumElts));
4061 } else if (MaxElts[i] < NumElts) {
4062 // The extraction can just take the first half
4063 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004064 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4065 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004066 DAG.getIntPtrConstant(0));
4067 } else {
4068 // An actual VEXT is needed
4069 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004070 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4071 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004072 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004073 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4074 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004075 DAG.getIntPtrConstant(NumElts));
4076 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4077 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4078 }
4079 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004080
Bob Wilson11a1dff2011-01-07 21:37:30 +00004081 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004082
Bob Wilson11a1dff2011-01-07 21:37:30 +00004083 for (unsigned i = 0; i < NumElts; ++i) {
4084 SDValue Entry = Op.getOperand(i);
4085 if (Entry.getOpcode() == ISD::UNDEF) {
4086 Mask.push_back(-1);
4087 continue;
4088 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004089
Bob Wilson11a1dff2011-01-07 21:37:30 +00004090 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004091 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4092 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004093 if (ExtractVec == SourceVecs[0]) {
4094 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4095 } else {
4096 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4097 }
4098 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004099
Bob Wilson11a1dff2011-01-07 21:37:30 +00004100 // Final check before we try to produce nonsense...
4101 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004102 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4103 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004104
Bob Wilson11a1dff2011-01-07 21:37:30 +00004105 return SDValue();
4106}
4107
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004108/// isShuffleMaskLegal - Targets can use this to indicate that they only
4109/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4110/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4111/// are assumed to be legal.
4112bool
4113ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4114 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004115 if (VT.getVectorNumElements() == 4 &&
4116 (VT.is128BitVector() || VT.is64BitVector())) {
4117 unsigned PFIndexes[4];
4118 for (unsigned i = 0; i != 4; ++i) {
4119 if (M[i] < 0)
4120 PFIndexes[i] = 8;
4121 else
4122 PFIndexes[i] = M[i];
4123 }
4124
4125 // Compute the index in the perfect shuffle table.
4126 unsigned PFTableIndex =
4127 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4128 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4129 unsigned Cost = (PFEntry >> 30);
4130
4131 if (Cost <= 4)
4132 return true;
4133 }
4134
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004135 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004136 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004137
Bob Wilson53dd2452010-06-07 23:53:38 +00004138 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4139 return (EltSize >= 32 ||
4140 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004141 isVREVMask(M, VT, 64) ||
4142 isVREVMask(M, VT, 32) ||
4143 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004144 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004145 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004146 isVTRNMask(M, VT, WhichResult) ||
4147 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004148 isVZIPMask(M, VT, WhichResult) ||
4149 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4150 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4151 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004152}
4153
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004154/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4155/// the specified operations to build the shuffle.
4156static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4157 SDValue RHS, SelectionDAG &DAG,
4158 DebugLoc dl) {
4159 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4160 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4161 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4162
4163 enum {
4164 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4165 OP_VREV,
4166 OP_VDUP0,
4167 OP_VDUP1,
4168 OP_VDUP2,
4169 OP_VDUP3,
4170 OP_VEXT1,
4171 OP_VEXT2,
4172 OP_VEXT3,
4173 OP_VUZPL, // VUZP, left result
4174 OP_VUZPR, // VUZP, right result
4175 OP_VZIPL, // VZIP, left result
4176 OP_VZIPR, // VZIP, right result
4177 OP_VTRNL, // VTRN, left result
4178 OP_VTRNR // VTRN, right result
4179 };
4180
4181 if (OpNum == OP_COPY) {
4182 if (LHSID == (1*9+2)*9+3) return LHS;
4183 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4184 return RHS;
4185 }
4186
4187 SDValue OpLHS, OpRHS;
4188 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4189 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4190 EVT VT = OpLHS.getValueType();
4191
4192 switch (OpNum) {
4193 default: llvm_unreachable("Unknown shuffle opcode!");
4194 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004195 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004196 if (VT.getVectorElementType() == MVT::i32 ||
4197 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004198 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4199 // vrev <4 x i16> -> VREV32
4200 if (VT.getVectorElementType() == MVT::i16)
4201 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4202 // vrev <4 x i8> -> VREV16
4203 assert(VT.getVectorElementType() == MVT::i8);
4204 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004205 case OP_VDUP0:
4206 case OP_VDUP1:
4207 case OP_VDUP2:
4208 case OP_VDUP3:
4209 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004210 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004211 case OP_VEXT1:
4212 case OP_VEXT2:
4213 case OP_VEXT3:
4214 return DAG.getNode(ARMISD::VEXT, dl, VT,
4215 OpLHS, OpRHS,
4216 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4217 case OP_VUZPL:
4218 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004219 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004220 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4221 case OP_VZIPL:
4222 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004223 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004224 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4225 case OP_VTRNL:
4226 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004227 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4228 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004229 }
4230}
4231
Bill Wendling69a05a72011-03-14 23:02:38 +00004232static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4233 SmallVectorImpl<int> &ShuffleMask,
4234 SelectionDAG &DAG) {
4235 // Check to see if we can use the VTBL instruction.
4236 SDValue V1 = Op.getOperand(0);
4237 SDValue V2 = Op.getOperand(1);
4238 DebugLoc DL = Op.getDebugLoc();
4239
4240 SmallVector<SDValue, 8> VTBLMask;
4241 for (SmallVectorImpl<int>::iterator
4242 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4243 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4244
4245 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4246 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4247 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4248 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004249
Owen Anderson76706012011-04-05 21:48:57 +00004250 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004251 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4252 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004253}
4254
Bob Wilson5bafff32009-06-22 23:27:02 +00004255static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004256 SDValue V1 = Op.getOperand(0);
4257 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004258 DebugLoc dl = Op.getDebugLoc();
4259 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004260 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004261 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004262
Bob Wilson28865062009-08-13 02:13:04 +00004263 // Convert shuffles that are directly supported on NEON to target-specific
4264 // DAG nodes, instead of keeping them as shuffles and matching them again
4265 // during code selection. This is more efficient and avoids the possibility
4266 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004267 // FIXME: floating-point vectors should be canonicalized to integer vectors
4268 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004269 SVN->getMask(ShuffleMask);
4270
Bob Wilson53dd2452010-06-07 23:53:38 +00004271 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4272 if (EltSize <= 32) {
4273 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4274 int Lane = SVN->getSplatIndex();
4275 // If this is undef splat, generate it via "just" vdup, if possible.
4276 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004277
Bob Wilson53dd2452010-06-07 23:53:38 +00004278 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4279 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4280 }
4281 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4282 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004283 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004284
4285 bool ReverseVEXT;
4286 unsigned Imm;
4287 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4288 if (ReverseVEXT)
4289 std::swap(V1, V2);
4290 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4291 DAG.getConstant(Imm, MVT::i32));
4292 }
4293
4294 if (isVREVMask(ShuffleMask, VT, 64))
4295 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4296 if (isVREVMask(ShuffleMask, VT, 32))
4297 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4298 if (isVREVMask(ShuffleMask, VT, 16))
4299 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4300
4301 // Check for Neon shuffles that modify both input vectors in place.
4302 // If both results are used, i.e., if there are two shuffles with the same
4303 // source operands and with masks corresponding to both results of one of
4304 // these operations, DAG memoization will ensure that a single node is
4305 // used for both shuffles.
4306 unsigned WhichResult;
4307 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4308 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4309 V1, V2).getValue(WhichResult);
4310 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4311 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4312 V1, V2).getValue(WhichResult);
4313 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4314 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4315 V1, V2).getValue(WhichResult);
4316
4317 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4318 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4319 V1, V1).getValue(WhichResult);
4320 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4321 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4322 V1, V1).getValue(WhichResult);
4323 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4324 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4325 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004326 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004327
Bob Wilsonc692cb72009-08-21 20:54:19 +00004328 // If the shuffle is not directly supported and it has 4 elements, use
4329 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004330 unsigned NumElts = VT.getVectorNumElements();
4331 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004332 unsigned PFIndexes[4];
4333 for (unsigned i = 0; i != 4; ++i) {
4334 if (ShuffleMask[i] < 0)
4335 PFIndexes[i] = 8;
4336 else
4337 PFIndexes[i] = ShuffleMask[i];
4338 }
4339
4340 // Compute the index in the perfect shuffle table.
4341 unsigned PFTableIndex =
4342 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004343 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4344 unsigned Cost = (PFEntry >> 30);
4345
4346 if (Cost <= 4)
4347 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4348 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004349
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004350 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004351 if (EltSize >= 32) {
4352 // Do the expansion with floating-point types, since that is what the VFP
4353 // registers are defined to use, and since i64 is not legal.
4354 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4355 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004356 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4357 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004358 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004359 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004360 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004361 Ops.push_back(DAG.getUNDEF(EltVT));
4362 else
4363 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4364 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4365 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4366 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004367 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004368 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004369 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004370 }
4371
Bill Wendling69a05a72011-03-14 23:02:38 +00004372 if (VT == MVT::v8i8) {
4373 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4374 if (NewOp.getNode())
4375 return NewOp;
4376 }
4377
Bob Wilson22cac0d2009-08-14 05:16:33 +00004378 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004379}
4380
Bob Wilson5bafff32009-06-22 23:27:02 +00004381static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004382 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004383 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004384 if (!isa<ConstantSDNode>(Lane))
4385 return SDValue();
4386
4387 SDValue Vec = Op.getOperand(0);
4388 if (Op.getValueType() == MVT::i32 &&
4389 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4390 DebugLoc dl = Op.getDebugLoc();
4391 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4392 }
4393
4394 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004395}
4396
Bob Wilsona6d65862009-08-03 20:36:38 +00004397static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4398 // The only time a CONCAT_VECTORS operation can have legal types is when
4399 // two 64-bit vectors are concatenated to a 128-bit vector.
4400 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4401 "unexpected CONCAT_VECTORS");
4402 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004403 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004404 SDValue Op0 = Op.getOperand(0);
4405 SDValue Op1 = Op.getOperand(1);
4406 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004407 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004408 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004409 DAG.getIntPtrConstant(0));
4410 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004411 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004412 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004413 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004414 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004415}
4416
Bob Wilson626613d2010-11-23 19:38:38 +00004417/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4418/// element has been zero/sign-extended, depending on the isSigned parameter,
4419/// from an integer type half its size.
4420static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4421 bool isSigned) {
4422 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4423 EVT VT = N->getValueType(0);
4424 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4425 SDNode *BVN = N->getOperand(0).getNode();
4426 if (BVN->getValueType(0) != MVT::v4i32 ||
4427 BVN->getOpcode() != ISD::BUILD_VECTOR)
4428 return false;
4429 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4430 unsigned HiElt = 1 - LoElt;
4431 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4432 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4433 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4434 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4435 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4436 return false;
4437 if (isSigned) {
4438 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4439 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4440 return true;
4441 } else {
4442 if (Hi0->isNullValue() && Hi1->isNullValue())
4443 return true;
4444 }
4445 return false;
4446 }
4447
4448 if (N->getOpcode() != ISD::BUILD_VECTOR)
4449 return false;
4450
4451 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4452 SDNode *Elt = N->getOperand(i).getNode();
4453 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4454 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4455 unsigned HalfSize = EltSize / 2;
4456 if (isSigned) {
4457 int64_t SExtVal = C->getSExtValue();
4458 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4459 return false;
4460 } else {
4461 if ((C->getZExtValue() >> HalfSize) != 0)
4462 return false;
4463 }
4464 continue;
4465 }
4466 return false;
4467 }
4468
4469 return true;
4470}
4471
4472/// isSignExtended - Check if a node is a vector value that is sign-extended
4473/// or a constant BUILD_VECTOR with sign-extended elements.
4474static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4475 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4476 return true;
4477 if (isExtendedBUILD_VECTOR(N, DAG, true))
4478 return true;
4479 return false;
4480}
4481
4482/// isZeroExtended - Check if a node is a vector value that is zero-extended
4483/// or a constant BUILD_VECTOR with zero-extended elements.
4484static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4485 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4486 return true;
4487 if (isExtendedBUILD_VECTOR(N, DAG, false))
4488 return true;
4489 return false;
4490}
4491
4492/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4493/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004494static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4495 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4496 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004497 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4498 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4499 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4500 LD->isNonTemporal(), LD->getAlignment());
4501 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4502 // have been legalized as a BITCAST from v4i32.
4503 if (N->getOpcode() == ISD::BITCAST) {
4504 SDNode *BVN = N->getOperand(0).getNode();
4505 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4506 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4507 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4508 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4509 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4510 }
4511 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4512 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4513 EVT VT = N->getValueType(0);
4514 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4515 unsigned NumElts = VT.getVectorNumElements();
4516 MVT TruncVT = MVT::getIntegerVT(EltSize);
4517 SmallVector<SDValue, 8> Ops;
4518 for (unsigned i = 0; i != NumElts; ++i) {
4519 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4520 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004521 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004522 }
4523 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4524 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004525}
4526
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004527static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4528 unsigned Opcode = N->getOpcode();
4529 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4530 SDNode *N0 = N->getOperand(0).getNode();
4531 SDNode *N1 = N->getOperand(1).getNode();
4532 return N0->hasOneUse() && N1->hasOneUse() &&
4533 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4534 }
4535 return false;
4536}
4537
4538static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4539 unsigned Opcode = N->getOpcode();
4540 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4541 SDNode *N0 = N->getOperand(0).getNode();
4542 SDNode *N1 = N->getOperand(1).getNode();
4543 return N0->hasOneUse() && N1->hasOneUse() &&
4544 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4545 }
4546 return false;
4547}
4548
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004549static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4550 // Multiplications are only custom-lowered for 128-bit vectors so that
4551 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4552 EVT VT = Op.getValueType();
4553 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4554 SDNode *N0 = Op.getOperand(0).getNode();
4555 SDNode *N1 = Op.getOperand(1).getNode();
4556 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004557 bool isMLA = false;
4558 bool isN0SExt = isSignExtended(N0, DAG);
4559 bool isN1SExt = isSignExtended(N1, DAG);
4560 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004561 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004562 else {
4563 bool isN0ZExt = isZeroExtended(N0, DAG);
4564 bool isN1ZExt = isZeroExtended(N1, DAG);
4565 if (isN0ZExt && isN1ZExt)
4566 NewOpc = ARMISD::VMULLu;
4567 else if (isN1SExt || isN1ZExt) {
4568 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4569 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4570 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4571 NewOpc = ARMISD::VMULLs;
4572 isMLA = true;
4573 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4574 NewOpc = ARMISD::VMULLu;
4575 isMLA = true;
4576 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4577 std::swap(N0, N1);
4578 NewOpc = ARMISD::VMULLu;
4579 isMLA = true;
4580 }
4581 }
4582
4583 if (!NewOpc) {
4584 if (VT == MVT::v2i64)
4585 // Fall through to expand this. It is not legal.
4586 return SDValue();
4587 else
4588 // Other vector multiplications are legal.
4589 return Op;
4590 }
4591 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004592
4593 // Legalize to a VMULL instruction.
4594 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004595 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004596 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004597 if (!isMLA) {
4598 Op0 = SkipExtension(N0, DAG);
4599 assert(Op0.getValueType().is64BitVector() &&
4600 Op1.getValueType().is64BitVector() &&
4601 "unexpected types for extended operands to VMULL");
4602 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4603 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004604
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004605 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4606 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4607 // vmull q0, d4, d6
4608 // vmlal q0, d5, d6
4609 // is faster than
4610 // vaddl q0, d4, d5
4611 // vmovl q1, d6
4612 // vmul q0, q0, q1
4613 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4614 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4615 EVT Op1VT = Op1.getValueType();
4616 return DAG.getNode(N0->getOpcode(), DL, VT,
4617 DAG.getNode(NewOpc, DL, VT,
4618 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4619 DAG.getNode(NewOpc, DL, VT,
4620 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004621}
4622
Owen Anderson76706012011-04-05 21:48:57 +00004623static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004624LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4625 // Convert to float
4626 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4627 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4628 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4629 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4630 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4631 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4632 // Get reciprocal estimate.
4633 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004634 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004635 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4636 // Because char has a smaller range than uchar, we can actually get away
4637 // without any newton steps. This requires that we use a weird bias
4638 // of 0xb000, however (again, this has been exhaustively tested).
4639 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4640 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4641 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4642 Y = DAG.getConstant(0xb000, MVT::i32);
4643 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4644 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4645 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4646 // Convert back to short.
4647 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4648 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4649 return X;
4650}
4651
Owen Anderson76706012011-04-05 21:48:57 +00004652static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004653LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4654 SDValue N2;
4655 // Convert to float.
4656 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4657 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4658 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4659 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4660 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4661 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004662
Nate Begeman7973f352011-02-11 20:53:29 +00004663 // Use reciprocal estimate and one refinement step.
4664 // float4 recip = vrecpeq_f32(yf);
4665 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004666 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004667 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004668 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004669 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4670 N1, N2);
4671 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4672 // Because short has a smaller range than ushort, we can actually get away
4673 // with only a single newton step. This requires that we use a weird bias
4674 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004675 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004676 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4677 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004678 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004679 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4680 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4681 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4682 // Convert back to integer and return.
4683 // return vmovn_s32(vcvt_s32_f32(result));
4684 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4685 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4686 return N0;
4687}
4688
4689static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4690 EVT VT = Op.getValueType();
4691 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4692 "unexpected type for custom-lowering ISD::SDIV");
4693
4694 DebugLoc dl = Op.getDebugLoc();
4695 SDValue N0 = Op.getOperand(0);
4696 SDValue N1 = Op.getOperand(1);
4697 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004698
Nate Begeman7973f352011-02-11 20:53:29 +00004699 if (VT == MVT::v8i8) {
4700 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4701 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004702
Nate Begeman7973f352011-02-11 20:53:29 +00004703 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4704 DAG.getIntPtrConstant(4));
4705 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004706 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004707 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4708 DAG.getIntPtrConstant(0));
4709 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4710 DAG.getIntPtrConstant(0));
4711
4712 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4713 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4714
4715 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4716 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004717
Nate Begeman7973f352011-02-11 20:53:29 +00004718 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4719 return N0;
4720 }
4721 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4722}
4723
4724static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4725 EVT VT = Op.getValueType();
4726 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4727 "unexpected type for custom-lowering ISD::UDIV");
4728
4729 DebugLoc dl = Op.getDebugLoc();
4730 SDValue N0 = Op.getOperand(0);
4731 SDValue N1 = Op.getOperand(1);
4732 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004733
Nate Begeman7973f352011-02-11 20:53:29 +00004734 if (VT == MVT::v8i8) {
4735 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4736 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004737
Nate Begeman7973f352011-02-11 20:53:29 +00004738 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4739 DAG.getIntPtrConstant(4));
4740 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004741 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004742 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4743 DAG.getIntPtrConstant(0));
4744 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4745 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004746
Nate Begeman7973f352011-02-11 20:53:29 +00004747 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4748 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004749
Nate Begeman7973f352011-02-11 20:53:29 +00004750 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4751 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004752
4753 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004754 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4755 N0);
4756 return N0;
4757 }
Owen Anderson76706012011-04-05 21:48:57 +00004758
Nate Begeman7973f352011-02-11 20:53:29 +00004759 // v4i16 sdiv ... Convert to float.
4760 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4761 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4762 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4763 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4764 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004765 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004766
4767 // Use reciprocal estimate and two refinement steps.
4768 // float4 recip = vrecpeq_f32(yf);
4769 // recip *= vrecpsq_f32(yf, recip);
4770 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004771 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004772 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00004773 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004774 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004775 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004776 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004777 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004778 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004779 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004780 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4781 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4782 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4783 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004784 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004785 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4786 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4787 N1 = DAG.getConstant(2, MVT::i32);
4788 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4789 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4790 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4791 // Convert back to integer and return.
4792 // return vmovn_u32(vcvt_s32_f32(result));
4793 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4794 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4795 return N0;
4796}
4797
Dan Gohmand858e902010-04-17 15:26:15 +00004798SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004799 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004800 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004801 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004802 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004803 case ISD::GlobalAddress:
4804 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4805 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004806 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004807 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004808 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4809 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004810 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004811 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004812 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004813 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004814 case ISD::SINT_TO_FP:
4815 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4816 case ISD::FP_TO_SINT:
4817 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004818 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004819 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004820 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004821 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004822 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004823 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004824 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004825 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4826 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00004827 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004828 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004829 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004830 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004831 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004832 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004833 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004834 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004835 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004836 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004837 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004838 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004839 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004840 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004841 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00004842 case ISD::SDIV: return LowerSDIV(Op, DAG);
4843 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004844 }
Dan Gohman475871a2008-07-27 21:46:04 +00004845 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004846}
4847
Duncan Sands1607f052008-12-01 11:39:25 +00004848/// ReplaceNodeResults - Replace the results of node with an illegal result
4849/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004850void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4851 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004852 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004853 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004854 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004855 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004856 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004857 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004858 case ISD::BITCAST:
4859 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004860 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004861 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004862 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004863 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004864 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004865 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004866 if (Res.getNode())
4867 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004868}
Chris Lattner27a6c732007-11-24 07:07:01 +00004869
Evan Chenga8e29892007-01-19 07:51:42 +00004870//===----------------------------------------------------------------------===//
4871// ARM Scheduler Hooks
4872//===----------------------------------------------------------------------===//
4873
4874MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004875ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4876 MachineBasicBlock *BB,
4877 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004878 unsigned dest = MI->getOperand(0).getReg();
4879 unsigned ptr = MI->getOperand(1).getReg();
4880 unsigned oldval = MI->getOperand(2).getReg();
4881 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004882 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4883 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004884 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004885
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004886 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
4887 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00004888 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004889 : ARM::GPRRegisterClass);
4890
4891 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00004892 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
4893 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
4894 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004895 }
4896
Jim Grosbach5278eb82009-12-11 01:42:04 +00004897 unsigned ldrOpc, strOpc;
4898 switch (Size) {
4899 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004900 case 1:
4901 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00004902 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004903 break;
4904 case 2:
4905 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4906 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4907 break;
4908 case 4:
4909 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4910 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4911 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004912 }
4913
4914 MachineFunction *MF = BB->getParent();
4915 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4916 MachineFunction::iterator It = BB;
4917 ++It; // insert the new blocks after the current block
4918
4919 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4920 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4921 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4922 MF->insert(It, loop1MBB);
4923 MF->insert(It, loop2MBB);
4924 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004925
4926 // Transfer the remainder of BB and its successor edges to exitMBB.
4927 exitMBB->splice(exitMBB->begin(), BB,
4928 llvm::next(MachineBasicBlock::iterator(MI)),
4929 BB->end());
4930 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004931
4932 // thisMBB:
4933 // ...
4934 // fallthrough --> loop1MBB
4935 BB->addSuccessor(loop1MBB);
4936
4937 // loop1MBB:
4938 // ldrex dest, [ptr]
4939 // cmp dest, oldval
4940 // bne exitMBB
4941 BB = loop1MBB;
4942 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004943 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004944 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004945 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4946 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004947 BB->addSuccessor(loop2MBB);
4948 BB->addSuccessor(exitMBB);
4949
4950 // loop2MBB:
4951 // strex scratch, newval, [ptr]
4952 // cmp scratch, #0
4953 // bne loop1MBB
4954 BB = loop2MBB;
4955 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
4956 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004957 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004958 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004959 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4960 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004961 BB->addSuccessor(loop1MBB);
4962 BB->addSuccessor(exitMBB);
4963
4964 // exitMBB:
4965 // ...
4966 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00004967
Dan Gohman14152b42010-07-06 20:24:04 +00004968 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00004969
Jim Grosbach5278eb82009-12-11 01:42:04 +00004970 return BB;
4971}
4972
4973MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004974ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
4975 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00004976 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
4977 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4978
4979 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004980 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004981 MachineFunction::iterator It = BB;
4982 ++It;
4983
4984 unsigned dest = MI->getOperand(0).getReg();
4985 unsigned ptr = MI->getOperand(1).getReg();
4986 unsigned incr = MI->getOperand(2).getReg();
4987 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004988 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00004989
4990 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
4991 if (isThumb2) {
4992 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
4993 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
4994 }
4995
Jim Grosbachc3c23542009-12-14 04:22:04 +00004996 unsigned ldrOpc, strOpc;
4997 switch (Size) {
4998 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004999 case 1:
5000 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005001 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005002 break;
5003 case 2:
5004 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5005 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5006 break;
5007 case 4:
5008 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5009 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5010 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005011 }
5012
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005013 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5014 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5015 MF->insert(It, loopMBB);
5016 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005017
5018 // Transfer the remainder of BB and its successor edges to exitMBB.
5019 exitMBB->splice(exitMBB->begin(), BB,
5020 llvm::next(MachineBasicBlock::iterator(MI)),
5021 BB->end());
5022 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005023
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005024 TargetRegisterClass *TRC =
5025 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5026 unsigned scratch = MRI.createVirtualRegister(TRC);
5027 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005028
5029 // thisMBB:
5030 // ...
5031 // fallthrough --> loopMBB
5032 BB->addSuccessor(loopMBB);
5033
5034 // loopMBB:
5035 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005036 // <binop> scratch2, dest, incr
5037 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005038 // cmp scratch, #0
5039 // bne- loopMBB
5040 // fallthrough --> exitMBB
5041 BB = loopMBB;
5042 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00005043 if (BinOpcode) {
5044 // operand order needs to go the other way for NAND
5045 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5046 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5047 addReg(incr).addReg(dest)).addReg(0);
5048 else
5049 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5050 addReg(dest).addReg(incr)).addReg(0);
5051 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005052
5053 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5054 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005055 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005056 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005057 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5058 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005059
5060 BB->addSuccessor(loopMBB);
5061 BB->addSuccessor(exitMBB);
5062
5063 // exitMBB:
5064 // ...
5065 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005066
Dan Gohman14152b42010-07-06 20:24:04 +00005067 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005068
Jim Grosbachc3c23542009-12-14 04:22:04 +00005069 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005070}
5071
Jim Grosbachf7da8822011-04-26 19:44:18 +00005072MachineBasicBlock *
5073ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5074 MachineBasicBlock *BB,
5075 unsigned Size,
5076 bool signExtend,
5077 ARMCC::CondCodes Cond) const {
5078 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5079
5080 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5081 MachineFunction *MF = BB->getParent();
5082 MachineFunction::iterator It = BB;
5083 ++It;
5084
5085 unsigned dest = MI->getOperand(0).getReg();
5086 unsigned ptr = MI->getOperand(1).getReg();
5087 unsigned incr = MI->getOperand(2).getReg();
5088 unsigned oldval = dest;
5089 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005090 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005091
5092 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5093 if (isThumb2) {
5094 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5095 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5096 }
5097
Jim Grosbachf7da8822011-04-26 19:44:18 +00005098 unsigned ldrOpc, strOpc, extendOpc;
5099 switch (Size) {
5100 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5101 case 1:
5102 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5103 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
5104 extendOpc = isThumb2 ? ARM::t2SXTBr : ARM::SXTBr;
5105 break;
5106 case 2:
5107 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5108 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5109 extendOpc = isThumb2 ? ARM::t2SXTHr : ARM::SXTHr;
5110 break;
5111 case 4:
5112 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5113 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5114 extendOpc = 0;
5115 break;
5116 }
5117
5118 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5119 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5120 MF->insert(It, loopMBB);
5121 MF->insert(It, exitMBB);
5122
5123 // Transfer the remainder of BB and its successor edges to exitMBB.
5124 exitMBB->splice(exitMBB->begin(), BB,
5125 llvm::next(MachineBasicBlock::iterator(MI)),
5126 BB->end());
5127 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5128
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005129 TargetRegisterClass *TRC =
5130 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5131 unsigned scratch = MRI.createVirtualRegister(TRC);
5132 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005133
5134 // thisMBB:
5135 // ...
5136 // fallthrough --> loopMBB
5137 BB->addSuccessor(loopMBB);
5138
5139 // loopMBB:
5140 // ldrex dest, ptr
5141 // (sign extend dest, if required)
5142 // cmp dest, incr
5143 // cmov.cond scratch2, dest, incr
5144 // strex scratch, scratch2, ptr
5145 // cmp scratch, #0
5146 // bne- loopMBB
5147 // fallthrough --> exitMBB
5148 BB = loopMBB;
5149 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
5150
5151 // Sign extend the value, if necessary.
5152 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005153 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005154 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval).addReg(dest));
5155 }
5156
5157 // Build compare and cmov instructions.
5158 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5159 .addReg(oldval).addReg(incr));
5160 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5161 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5162
5163 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5164 .addReg(ptr));
5165 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5166 .addReg(scratch).addImm(0));
5167 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5168 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5169
5170 BB->addSuccessor(loopMBB);
5171 BB->addSuccessor(exitMBB);
5172
5173 // exitMBB:
5174 // ...
5175 BB = exitMBB;
5176
5177 MI->eraseFromParent(); // The instruction is gone now.
5178
5179 return BB;
5180}
5181
Evan Cheng218977b2010-07-13 19:27:42 +00005182static
5183MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
5184 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
5185 E = MBB->succ_end(); I != E; ++I)
5186 if (*I != Succ)
5187 return *I;
5188 llvm_unreachable("Expecting a BB with two successors!");
5189}
5190
Andrew Trick1c3af772011-04-23 03:55:32 +00005191// FIXME: This opcode table should obviously be expressed in the target
5192// description. We probably just need a "machine opcode" value in the pseudo
5193// instruction. But the ideal solution maybe to simply remove the "S" version
5194// of the opcode altogether.
5195struct AddSubFlagsOpcodePair {
5196 unsigned PseudoOpc;
5197 unsigned MachineOpc;
5198};
5199
5200static AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
5201 {ARM::ADCSri, ARM::ADCri},
5202 {ARM::ADCSrr, ARM::ADCrr},
5203 {ARM::ADCSrs, ARM::ADCrs},
5204 {ARM::SBCSri, ARM::SBCri},
5205 {ARM::SBCSrr, ARM::SBCrr},
5206 {ARM::SBCSrs, ARM::SBCrs},
5207 {ARM::RSBSri, ARM::RSBri},
5208 {ARM::RSBSrr, ARM::RSBrr},
5209 {ARM::RSBSrs, ARM::RSBrs},
5210 {ARM::RSCSri, ARM::RSCri},
5211 {ARM::RSCSrs, ARM::RSCrs},
5212 {ARM::t2ADCSri, ARM::t2ADCri},
5213 {ARM::t2ADCSrr, ARM::t2ADCrr},
5214 {ARM::t2ADCSrs, ARM::t2ADCrs},
5215 {ARM::t2SBCSri, ARM::t2SBCri},
5216 {ARM::t2SBCSrr, ARM::t2SBCrr},
5217 {ARM::t2SBCSrs, ARM::t2SBCrs},
5218 {ARM::t2RSBSri, ARM::t2RSBri},
5219 {ARM::t2RSBSrs, ARM::t2RSBrs},
5220};
5221
5222// Convert and Add or Subtract with Carry and Flags to a generic opcode with
5223// CPSR<def> operand. e.g. ADCS (...) -> ADC (... CPSR<def>).
5224//
5225// FIXME: Somewhere we should assert that CPSR<def> is in the correct
5226// position to be recognized by the target descrition as the 'S' bit.
5227bool ARMTargetLowering::RemapAddSubWithFlags(MachineInstr *MI,
5228 MachineBasicBlock *BB) const {
5229 unsigned OldOpc = MI->getOpcode();
5230 unsigned NewOpc = 0;
5231
5232 // This is only called for instructions that need remapping, so iterating over
5233 // the tiny opcode table is not costly.
5234 static const int NPairs =
5235 sizeof(AddSubFlagsOpcodeMap) / sizeof(AddSubFlagsOpcodePair);
5236 for (AddSubFlagsOpcodePair *Pair = &AddSubFlagsOpcodeMap[0],
5237 *End = &AddSubFlagsOpcodeMap[NPairs]; Pair != End; ++Pair) {
5238 if (OldOpc == Pair->PseudoOpc) {
5239 NewOpc = Pair->MachineOpc;
5240 break;
5241 }
5242 }
5243 if (!NewOpc)
5244 return false;
5245
5246 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5247 DebugLoc dl = MI->getDebugLoc();
5248 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
5249 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
5250 MIB.addOperand(MI->getOperand(i));
5251 AddDefaultPred(MIB);
5252 MIB.addReg(ARM::CPSR, RegState::Define); // S bit
5253 MI->eraseFromParent();
5254 return true;
5255}
5256
Jim Grosbache801dc42009-12-12 01:40:06 +00005257MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00005258ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00005259 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005260 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00005261 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005262 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00005263 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00005264 default: {
5265 if (RemapAddSubWithFlags(MI, BB))
5266 return BB;
5267
Jim Grosbach5278eb82009-12-11 01:42:04 +00005268 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00005269 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00005270 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005271 case ARM::ATOMIC_LOAD_ADD_I8:
5272 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5273 case ARM::ATOMIC_LOAD_ADD_I16:
5274 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5275 case ARM::ATOMIC_LOAD_ADD_I32:
5276 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005277
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005278 case ARM::ATOMIC_LOAD_AND_I8:
5279 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5280 case ARM::ATOMIC_LOAD_AND_I16:
5281 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5282 case ARM::ATOMIC_LOAD_AND_I32:
5283 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005284
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005285 case ARM::ATOMIC_LOAD_OR_I8:
5286 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5287 case ARM::ATOMIC_LOAD_OR_I16:
5288 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5289 case ARM::ATOMIC_LOAD_OR_I32:
5290 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005291
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005292 case ARM::ATOMIC_LOAD_XOR_I8:
5293 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5294 case ARM::ATOMIC_LOAD_XOR_I16:
5295 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5296 case ARM::ATOMIC_LOAD_XOR_I32:
5297 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005298
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005299 case ARM::ATOMIC_LOAD_NAND_I8:
5300 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5301 case ARM::ATOMIC_LOAD_NAND_I16:
5302 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5303 case ARM::ATOMIC_LOAD_NAND_I32:
5304 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005305
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005306 case ARM::ATOMIC_LOAD_SUB_I8:
5307 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5308 case ARM::ATOMIC_LOAD_SUB_I16:
5309 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5310 case ARM::ATOMIC_LOAD_SUB_I32:
5311 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005312
Jim Grosbachf7da8822011-04-26 19:44:18 +00005313 case ARM::ATOMIC_LOAD_MIN_I8:
5314 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
5315 case ARM::ATOMIC_LOAD_MIN_I16:
5316 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
5317 case ARM::ATOMIC_LOAD_MIN_I32:
5318 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
5319
5320 case ARM::ATOMIC_LOAD_MAX_I8:
5321 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
5322 case ARM::ATOMIC_LOAD_MAX_I16:
5323 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
5324 case ARM::ATOMIC_LOAD_MAX_I32:
5325 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
5326
5327 case ARM::ATOMIC_LOAD_UMIN_I8:
5328 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
5329 case ARM::ATOMIC_LOAD_UMIN_I16:
5330 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
5331 case ARM::ATOMIC_LOAD_UMIN_I32:
5332 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
5333
5334 case ARM::ATOMIC_LOAD_UMAX_I8:
5335 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
5336 case ARM::ATOMIC_LOAD_UMAX_I16:
5337 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
5338 case ARM::ATOMIC_LOAD_UMAX_I32:
5339 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
5340
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005341 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
5342 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
5343 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00005344
5345 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
5346 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
5347 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005348
Evan Cheng007ea272009-08-12 05:17:19 +00005349 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00005350 // To "insert" a SELECT_CC instruction, we actually have to insert the
5351 // diamond control-flow pattern. The incoming instruction knows the
5352 // destination vreg to set, the condition code register to branch on, the
5353 // true/false values to select between, and a branch opcode to use.
5354 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005355 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00005356 ++It;
5357
5358 // thisMBB:
5359 // ...
5360 // TrueVal = ...
5361 // cmpTY ccX, r1, r2
5362 // bCC copy1MBB
5363 // fallthrough --> copy0MBB
5364 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005365 MachineFunction *F = BB->getParent();
5366 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
5367 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00005368 F->insert(It, copy0MBB);
5369 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005370
5371 // Transfer the remainder of BB and its successor edges to sinkMBB.
5372 sinkMBB->splice(sinkMBB->begin(), BB,
5373 llvm::next(MachineBasicBlock::iterator(MI)),
5374 BB->end());
5375 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
5376
Dan Gohman258c58c2010-07-06 15:49:48 +00005377 BB->addSuccessor(copy0MBB);
5378 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00005379
Dan Gohman14152b42010-07-06 20:24:04 +00005380 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
5381 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
5382
Evan Chenga8e29892007-01-19 07:51:42 +00005383 // copy0MBB:
5384 // %FalseValue = ...
5385 // # fallthrough to sinkMBB
5386 BB = copy0MBB;
5387
5388 // Update machine-CFG edges
5389 BB->addSuccessor(sinkMBB);
5390
5391 // sinkMBB:
5392 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5393 // ...
5394 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00005395 BuildMI(*BB, BB->begin(), dl,
5396 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00005397 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5398 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5399
Dan Gohman14152b42010-07-06 20:24:04 +00005400 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00005401 return BB;
5402 }
Evan Cheng86198642009-08-07 00:34:42 +00005403
Evan Cheng218977b2010-07-13 19:27:42 +00005404 case ARM::BCCi64:
5405 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00005406 // If there is an unconditional branch to the other successor, remove it.
5407 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00005408
Evan Cheng218977b2010-07-13 19:27:42 +00005409 // Compare both parts that make up the double comparison separately for
5410 // equality.
5411 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
5412
5413 unsigned LHS1 = MI->getOperand(1).getReg();
5414 unsigned LHS2 = MI->getOperand(2).getReg();
5415 if (RHSisZero) {
5416 AddDefaultPred(BuildMI(BB, dl,
5417 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5418 .addReg(LHS1).addImm(0));
5419 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5420 .addReg(LHS2).addImm(0)
5421 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5422 } else {
5423 unsigned RHS1 = MI->getOperand(3).getReg();
5424 unsigned RHS2 = MI->getOperand(4).getReg();
5425 AddDefaultPred(BuildMI(BB, dl,
5426 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5427 .addReg(LHS1).addReg(RHS1));
5428 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5429 .addReg(LHS2).addReg(RHS2)
5430 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5431 }
5432
5433 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
5434 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
5435 if (MI->getOperand(0).getImm() == ARMCC::NE)
5436 std::swap(destMBB, exitMBB);
5437
5438 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5439 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
5440 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
5441 .addMBB(exitMBB);
5442
5443 MI->eraseFromParent(); // The pseudo instruction is gone now.
5444 return BB;
5445 }
Evan Chenga8e29892007-01-19 07:51:42 +00005446 }
5447}
5448
5449//===----------------------------------------------------------------------===//
5450// ARM Optimization Hooks
5451//===----------------------------------------------------------------------===//
5452
Chris Lattnerd1980a52009-03-12 06:52:53 +00005453static
5454SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
5455 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005456 SelectionDAG &DAG = DCI.DAG;
5457 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00005458 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00005459 unsigned Opc = N->getOpcode();
5460 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
5461 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
5462 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
5463 ISD::CondCode CC = ISD::SETCC_INVALID;
5464
5465 if (isSlctCC) {
5466 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
5467 } else {
5468 SDValue CCOp = Slct.getOperand(0);
5469 if (CCOp.getOpcode() == ISD::SETCC)
5470 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
5471 }
5472
5473 bool DoXform = false;
5474 bool InvCC = false;
5475 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
5476 "Bad input!");
5477
5478 if (LHS.getOpcode() == ISD::Constant &&
5479 cast<ConstantSDNode>(LHS)->isNullValue()) {
5480 DoXform = true;
5481 } else if (CC != ISD::SETCC_INVALID &&
5482 RHS.getOpcode() == ISD::Constant &&
5483 cast<ConstantSDNode>(RHS)->isNullValue()) {
5484 std::swap(LHS, RHS);
5485 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005486 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00005487 Op0.getOperand(0).getValueType();
5488 bool isInt = OpVT.isInteger();
5489 CC = ISD::getSetCCInverse(CC, isInt);
5490
5491 if (!TLI.isCondCodeLegal(CC, OpVT))
5492 return SDValue(); // Inverse operator isn't legal.
5493
5494 DoXform = true;
5495 InvCC = true;
5496 }
5497
5498 if (DoXform) {
5499 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
5500 if (isSlctCC)
5501 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
5502 Slct.getOperand(0), Slct.getOperand(1), CC);
5503 SDValue CCOp = Slct.getOperand(0);
5504 if (InvCC)
5505 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
5506 CCOp.getOperand(0), CCOp.getOperand(1), CC);
5507 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
5508 CCOp, OtherOp, Result);
5509 }
5510 return SDValue();
5511}
5512
Bob Wilson3d5792a2010-07-29 20:34:14 +00005513/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
5514/// operands N0 and N1. This is a helper for PerformADDCombine that is
5515/// called with the default operands, and if that fails, with commuted
5516/// operands.
5517static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
5518 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005519 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
5520 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
5521 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
5522 if (Result.getNode()) return Result;
5523 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00005524 return SDValue();
5525}
5526
Bob Wilson3d5792a2010-07-29 20:34:14 +00005527/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
5528///
5529static SDValue PerformADDCombine(SDNode *N,
5530 TargetLowering::DAGCombinerInfo &DCI) {
5531 SDValue N0 = N->getOperand(0);
5532 SDValue N1 = N->getOperand(1);
5533
5534 // First try with the default operand order.
5535 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI);
5536 if (Result.getNode())
5537 return Result;
5538
5539 // If that didn't work, try again with the operands commuted.
5540 return PerformADDCombineWithOperands(N, N1, N0, DCI);
5541}
5542
Chris Lattnerd1980a52009-03-12 06:52:53 +00005543/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00005544///
Chris Lattnerd1980a52009-03-12 06:52:53 +00005545static SDValue PerformSUBCombine(SDNode *N,
5546 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005547 SDValue N0 = N->getOperand(0);
5548 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00005549
Chris Lattnerd1980a52009-03-12 06:52:53 +00005550 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
5551 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
5552 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
5553 if (Result.getNode()) return Result;
5554 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00005555
Chris Lattnerd1980a52009-03-12 06:52:53 +00005556 return SDValue();
5557}
5558
Evan Cheng463d3582011-03-31 19:38:48 +00005559/// PerformVMULCombine
5560/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
5561/// special multiplier accumulator forwarding.
5562/// vmul d3, d0, d2
5563/// vmla d3, d1, d2
5564/// is faster than
5565/// vadd d3, d0, d1
5566/// vmul d3, d3, d2
5567static SDValue PerformVMULCombine(SDNode *N,
5568 TargetLowering::DAGCombinerInfo &DCI,
5569 const ARMSubtarget *Subtarget) {
5570 if (!Subtarget->hasVMLxForwarding())
5571 return SDValue();
5572
5573 SelectionDAG &DAG = DCI.DAG;
5574 SDValue N0 = N->getOperand(0);
5575 SDValue N1 = N->getOperand(1);
5576 unsigned Opcode = N0.getOpcode();
5577 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5578 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
5579 Opcode = N0.getOpcode();
5580 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5581 Opcode != ISD::FADD && Opcode != ISD::FSUB)
5582 return SDValue();
5583 std::swap(N0, N1);
5584 }
5585
5586 EVT VT = N->getValueType(0);
5587 DebugLoc DL = N->getDebugLoc();
5588 SDValue N00 = N0->getOperand(0);
5589 SDValue N01 = N0->getOperand(1);
5590 return DAG.getNode(Opcode, DL, VT,
5591 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
5592 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
5593}
5594
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005595static SDValue PerformMULCombine(SDNode *N,
5596 TargetLowering::DAGCombinerInfo &DCI,
5597 const ARMSubtarget *Subtarget) {
5598 SelectionDAG &DAG = DCI.DAG;
5599
5600 if (Subtarget->isThumb1Only())
5601 return SDValue();
5602
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005603 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5604 return SDValue();
5605
5606 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00005607 if (VT.is64BitVector() || VT.is128BitVector())
5608 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005609 if (VT != MVT::i32)
5610 return SDValue();
5611
5612 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
5613 if (!C)
5614 return SDValue();
5615
5616 uint64_t MulAmt = C->getZExtValue();
5617 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
5618 ShiftAmt = ShiftAmt & (32 - 1);
5619 SDValue V = N->getOperand(0);
5620 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005621
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005622 SDValue Res;
5623 MulAmt >>= ShiftAmt;
5624 if (isPowerOf2_32(MulAmt - 1)) {
5625 // (mul x, 2^N + 1) => (add (shl x, N), x)
5626 Res = DAG.getNode(ISD::ADD, DL, VT,
5627 V, DAG.getNode(ISD::SHL, DL, VT,
5628 V, DAG.getConstant(Log2_32(MulAmt-1),
5629 MVT::i32)));
5630 } else if (isPowerOf2_32(MulAmt + 1)) {
5631 // (mul x, 2^N - 1) => (sub (shl x, N), x)
5632 Res = DAG.getNode(ISD::SUB, DL, VT,
5633 DAG.getNode(ISD::SHL, DL, VT,
5634 V, DAG.getConstant(Log2_32(MulAmt+1),
5635 MVT::i32)),
5636 V);
5637 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005638 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005639
5640 if (ShiftAmt != 0)
5641 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
5642 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005643
5644 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005645 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005646 return SDValue();
5647}
5648
Owen Anderson080c0922010-11-05 19:27:46 +00005649static SDValue PerformANDCombine(SDNode *N,
5650 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00005651
Owen Anderson080c0922010-11-05 19:27:46 +00005652 // Attempt to use immediate-form VBIC
5653 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5654 DebugLoc dl = N->getDebugLoc();
5655 EVT VT = N->getValueType(0);
5656 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005657
Tanya Lattner0433b212011-04-07 15:24:20 +00005658 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5659 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005660
Owen Anderson080c0922010-11-05 19:27:46 +00005661 APInt SplatBits, SplatUndef;
5662 unsigned SplatBitSize;
5663 bool HasAnyUndefs;
5664 if (BVN &&
5665 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5666 if (SplatBitSize <= 64) {
5667 EVT VbicVT;
5668 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
5669 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005670 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005671 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00005672 if (Val.getNode()) {
5673 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005674 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00005675 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005676 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00005677 }
5678 }
5679 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005680
Owen Anderson080c0922010-11-05 19:27:46 +00005681 return SDValue();
5682}
5683
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005684/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
5685static SDValue PerformORCombine(SDNode *N,
5686 TargetLowering::DAGCombinerInfo &DCI,
5687 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00005688 // Attempt to use immediate-form VORR
5689 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5690 DebugLoc dl = N->getDebugLoc();
5691 EVT VT = N->getValueType(0);
5692 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005693
Tanya Lattner0433b212011-04-07 15:24:20 +00005694 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5695 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005696
Owen Anderson60f48702010-11-03 23:15:26 +00005697 APInt SplatBits, SplatUndef;
5698 unsigned SplatBitSize;
5699 bool HasAnyUndefs;
5700 if (BVN && Subtarget->hasNEON() &&
5701 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5702 if (SplatBitSize <= 64) {
5703 EVT VorrVT;
5704 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
5705 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005706 DAG, VorrVT, VT.is128BitVector(),
5707 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00005708 if (Val.getNode()) {
5709 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005710 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00005711 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005712 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00005713 }
5714 }
5715 }
5716
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005717 SDValue N0 = N->getOperand(0);
5718 if (N0.getOpcode() != ISD::AND)
5719 return SDValue();
5720 SDValue N1 = N->getOperand(1);
5721
5722 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
5723 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
5724 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
5725 APInt SplatUndef;
5726 unsigned SplatBitSize;
5727 bool HasAnyUndefs;
5728
5729 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
5730 APInt SplatBits0;
5731 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
5732 HasAnyUndefs) && !HasAnyUndefs) {
5733 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
5734 APInt SplatBits1;
5735 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
5736 HasAnyUndefs) && !HasAnyUndefs &&
5737 SplatBits0 == ~SplatBits1) {
5738 // Canonicalize the vector type to make instruction selection simpler.
5739 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
5740 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
5741 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00005742 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005743 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
5744 }
5745 }
5746 }
5747
Jim Grosbach54238562010-07-17 03:30:54 +00005748 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
5749 // reasonable.
5750
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005751 // BFI is only available on V6T2+
5752 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
5753 return SDValue();
5754
Jim Grosbach54238562010-07-17 03:30:54 +00005755 DebugLoc DL = N->getDebugLoc();
5756 // 1) or (and A, mask), val => ARMbfi A, val, mask
5757 // iff (val & mask) == val
5758 //
5759 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
5760 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005761 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005762 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005763 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005764 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005765
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005766 if (VT != MVT::i32)
5767 return SDValue();
5768
Evan Cheng30fb13f2010-12-13 20:32:54 +00005769 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00005770
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005771 // The value and the mask need to be constants so we can verify this is
5772 // actually a bitfield set. If the mask is 0xffff, we can do better
5773 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00005774 SDValue MaskOp = N0.getOperand(1);
5775 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
5776 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005777 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005778 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005779 if (Mask == 0xffff)
5780 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005781 SDValue Res;
5782 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005783 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
5784 if (N1C) {
5785 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005786 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00005787 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005788
Evan Chenga9688c42010-12-11 04:11:38 +00005789 if (ARM::isBitFieldInvertedMask(Mask)) {
5790 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005791
Evan Cheng30fb13f2010-12-13 20:32:54 +00005792 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00005793 DAG.getConstant(Val, MVT::i32),
5794 DAG.getConstant(Mask, MVT::i32));
5795
5796 // Do not add new nodes to DAG combiner worklist.
5797 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005798 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005799 }
Jim Grosbach54238562010-07-17 03:30:54 +00005800 } else if (N1.getOpcode() == ISD::AND) {
5801 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005802 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5803 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00005804 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005805 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005806
Eric Christopher29aeed12011-03-26 01:21:03 +00005807 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
5808 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00005809 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005810 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005811 // The pack halfword instruction works better for masks that fit it,
5812 // so use that when it's available.
5813 if (Subtarget->hasT2ExtractPack() &&
5814 (Mask == 0xffff || Mask == 0xffff0000))
5815 return SDValue();
5816 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00005817 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00005818 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00005819 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00005820 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00005821 DAG.getConstant(Mask, MVT::i32));
5822 // Do not add new nodes to DAG combiner worklist.
5823 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005824 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005825 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005826 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005827 // The pack halfword instruction works better for masks that fit it,
5828 // so use that when it's available.
5829 if (Subtarget->hasT2ExtractPack() &&
5830 (Mask2 == 0xffff || Mask2 == 0xffff0000))
5831 return SDValue();
5832 // 2b
5833 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005834 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00005835 DAG.getConstant(lsb, MVT::i32));
5836 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00005837 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00005838 // Do not add new nodes to DAG combiner worklist.
5839 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005840 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005841 }
5842 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005843
Evan Cheng30fb13f2010-12-13 20:32:54 +00005844 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
5845 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
5846 ARM::isBitFieldInvertedMask(~Mask)) {
5847 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
5848 // where lsb(mask) == #shamt and masked bits of B are known zero.
5849 SDValue ShAmt = N00.getOperand(1);
5850 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
5851 unsigned LSB = CountTrailingZeros_32(Mask);
5852 if (ShAmtC != LSB)
5853 return SDValue();
5854
5855 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
5856 DAG.getConstant(~Mask, MVT::i32));
5857
5858 // Do not add new nodes to DAG combiner worklist.
5859 DCI.CombineTo(N, Res, false);
5860 }
5861
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005862 return SDValue();
5863}
5864
Evan Cheng0c1aec12010-12-14 03:22:07 +00005865/// PerformBFICombine - (bfi A, (and B, C1), C2) -> (bfi A, B, C2) iff
5866/// C1 & C2 == C1.
5867static SDValue PerformBFICombine(SDNode *N,
5868 TargetLowering::DAGCombinerInfo &DCI) {
5869 SDValue N1 = N->getOperand(1);
5870 if (N1.getOpcode() == ISD::AND) {
5871 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5872 if (!N11C)
5873 return SDValue();
5874 unsigned Mask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
5875 unsigned Mask2 = N11C->getZExtValue();
5876 if ((Mask & Mask2) == Mask2)
5877 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
5878 N->getOperand(0), N1.getOperand(0),
5879 N->getOperand(2));
5880 }
5881 return SDValue();
5882}
5883
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005884/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
5885/// ARMISD::VMOVRRD.
5886static SDValue PerformVMOVRRDCombine(SDNode *N,
5887 TargetLowering::DAGCombinerInfo &DCI) {
5888 // vmovrrd(vmovdrr x, y) -> x,y
5889 SDValue InDouble = N->getOperand(0);
5890 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
5891 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00005892
5893 // vmovrrd(load f64) -> (load i32), (load i32)
5894 SDNode *InNode = InDouble.getNode();
5895 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
5896 InNode->getValueType(0) == MVT::f64 &&
5897 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
5898 !cast<LoadSDNode>(InNode)->isVolatile()) {
5899 // TODO: Should this be done for non-FrameIndex operands?
5900 LoadSDNode *LD = cast<LoadSDNode>(InNode);
5901
5902 SelectionDAG &DAG = DCI.DAG;
5903 DebugLoc DL = LD->getDebugLoc();
5904 SDValue BasePtr = LD->getBasePtr();
5905 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
5906 LD->getPointerInfo(), LD->isVolatile(),
5907 LD->isNonTemporal(), LD->getAlignment());
5908
5909 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
5910 DAG.getConstant(4, MVT::i32));
5911 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
5912 LD->getPointerInfo(), LD->isVolatile(),
5913 LD->isNonTemporal(),
5914 std::min(4U, LD->getAlignment() / 2));
5915
5916 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
5917 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
5918 DCI.RemoveFromWorklist(LD);
5919 DAG.DeleteNode(LD);
5920 return Result;
5921 }
5922
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005923 return SDValue();
5924}
5925
5926/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
5927/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
5928static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
5929 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
5930 SDValue Op0 = N->getOperand(0);
5931 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005932 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005933 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005934 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005935 Op1 = Op1.getOperand(0);
5936 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
5937 Op0.getNode() == Op1.getNode() &&
5938 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005939 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005940 N->getValueType(0), Op0.getOperand(0));
5941 return SDValue();
5942}
5943
Bob Wilson31600902010-12-21 06:43:19 +00005944/// PerformSTORECombine - Target-specific dag combine xforms for
5945/// ISD::STORE.
5946static SDValue PerformSTORECombine(SDNode *N,
5947 TargetLowering::DAGCombinerInfo &DCI) {
5948 // Bitcast an i64 store extracted from a vector to f64.
5949 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5950 StoreSDNode *St = cast<StoreSDNode>(N);
5951 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00005952 if (!ISD::isNormalStore(St) || St->isVolatile())
5953 return SDValue();
5954
5955 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
5956 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
5957 SelectionDAG &DAG = DCI.DAG;
5958 DebugLoc DL = St->getDebugLoc();
5959 SDValue BasePtr = St->getBasePtr();
5960 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
5961 StVal.getNode()->getOperand(0), BasePtr,
5962 St->getPointerInfo(), St->isVolatile(),
5963 St->isNonTemporal(), St->getAlignment());
5964
5965 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
5966 DAG.getConstant(4, MVT::i32));
5967 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
5968 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
5969 St->isNonTemporal(),
5970 std::min(4U, St->getAlignment() / 2));
5971 }
5972
5973 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00005974 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5975 return SDValue();
5976
5977 SelectionDAG &DAG = DCI.DAG;
5978 DebugLoc dl = StVal.getDebugLoc();
5979 SDValue IntVec = StVal.getOperand(0);
5980 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5981 IntVec.getValueType().getVectorNumElements());
5982 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
5983 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5984 Vec, StVal.getOperand(1));
5985 dl = N->getDebugLoc();
5986 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
5987 // Make the DAGCombiner fold the bitcasts.
5988 DCI.AddToWorklist(Vec.getNode());
5989 DCI.AddToWorklist(ExtElt.getNode());
5990 DCI.AddToWorklist(V.getNode());
5991 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
5992 St->getPointerInfo(), St->isVolatile(),
5993 St->isNonTemporal(), St->getAlignment(),
5994 St->getTBAAInfo());
5995}
5996
5997/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
5998/// are normal, non-volatile loads. If so, it is profitable to bitcast an
5999/// i64 vector to have f64 elements, since the value can then be loaded
6000/// directly into a VFP register.
6001static bool hasNormalLoadOperand(SDNode *N) {
6002 unsigned NumElts = N->getValueType(0).getVectorNumElements();
6003 for (unsigned i = 0; i < NumElts; ++i) {
6004 SDNode *Elt = N->getOperand(i).getNode();
6005 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
6006 return true;
6007 }
6008 return false;
6009}
6010
Bob Wilson75f02882010-09-17 22:59:05 +00006011/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
6012/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00006013static SDValue PerformBUILD_VECTORCombine(SDNode *N,
6014 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00006015 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
6016 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
6017 // into a pair of GPRs, which is fine when the value is used as a scalar,
6018 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00006019 SelectionDAG &DAG = DCI.DAG;
6020 if (N->getNumOperands() == 2) {
6021 SDValue RV = PerformVMOVDRRCombine(N, DAG);
6022 if (RV.getNode())
6023 return RV;
6024 }
Bob Wilson75f02882010-09-17 22:59:05 +00006025
Bob Wilson31600902010-12-21 06:43:19 +00006026 // Load i64 elements as f64 values so that type legalization does not split
6027 // them up into i32 values.
6028 EVT VT = N->getValueType(0);
6029 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
6030 return SDValue();
6031 DebugLoc dl = N->getDebugLoc();
6032 SmallVector<SDValue, 8> Ops;
6033 unsigned NumElts = VT.getVectorNumElements();
6034 for (unsigned i = 0; i < NumElts; ++i) {
6035 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
6036 Ops.push_back(V);
6037 // Make the DAGCombiner fold the bitcast.
6038 DCI.AddToWorklist(V.getNode());
6039 }
6040 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
6041 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
6042 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
6043}
6044
6045/// PerformInsertEltCombine - Target-specific dag combine xforms for
6046/// ISD::INSERT_VECTOR_ELT.
6047static SDValue PerformInsertEltCombine(SDNode *N,
6048 TargetLowering::DAGCombinerInfo &DCI) {
6049 // Bitcast an i64 load inserted into a vector to f64.
6050 // Otherwise, the i64 value will be legalized to a pair of i32 values.
6051 EVT VT = N->getValueType(0);
6052 SDNode *Elt = N->getOperand(1).getNode();
6053 if (VT.getVectorElementType() != MVT::i64 ||
6054 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
6055 return SDValue();
6056
6057 SelectionDAG &DAG = DCI.DAG;
6058 DebugLoc dl = N->getDebugLoc();
6059 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
6060 VT.getVectorNumElements());
6061 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
6062 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
6063 // Make the DAGCombiner fold the bitcasts.
6064 DCI.AddToWorklist(Vec.getNode());
6065 DCI.AddToWorklist(V.getNode());
6066 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
6067 Vec, V, N->getOperand(2));
6068 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00006069}
6070
Bob Wilsonf20700c2010-10-27 20:38:28 +00006071/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
6072/// ISD::VECTOR_SHUFFLE.
6073static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
6074 // The LLVM shufflevector instruction does not require the shuffle mask
6075 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
6076 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
6077 // operands do not match the mask length, they are extended by concatenating
6078 // them with undef vectors. That is probably the right thing for other
6079 // targets, but for NEON it is better to concatenate two double-register
6080 // size vector operands into a single quad-register size vector. Do that
6081 // transformation here:
6082 // shuffle(concat(v1, undef), concat(v2, undef)) ->
6083 // shuffle(concat(v1, v2), undef)
6084 SDValue Op0 = N->getOperand(0);
6085 SDValue Op1 = N->getOperand(1);
6086 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
6087 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
6088 Op0.getNumOperands() != 2 ||
6089 Op1.getNumOperands() != 2)
6090 return SDValue();
6091 SDValue Concat0Op1 = Op0.getOperand(1);
6092 SDValue Concat1Op1 = Op1.getOperand(1);
6093 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
6094 Concat1Op1.getOpcode() != ISD::UNDEF)
6095 return SDValue();
6096 // Skip the transformation if any of the types are illegal.
6097 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6098 EVT VT = N->getValueType(0);
6099 if (!TLI.isTypeLegal(VT) ||
6100 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
6101 !TLI.isTypeLegal(Concat1Op1.getValueType()))
6102 return SDValue();
6103
6104 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
6105 Op0.getOperand(0), Op1.getOperand(0));
6106 // Translate the shuffle mask.
6107 SmallVector<int, 16> NewMask;
6108 unsigned NumElts = VT.getVectorNumElements();
6109 unsigned HalfElts = NumElts/2;
6110 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
6111 for (unsigned n = 0; n < NumElts; ++n) {
6112 int MaskElt = SVN->getMaskElt(n);
6113 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006114 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00006115 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006116 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00006117 NewElt = HalfElts + MaskElt - NumElts;
6118 NewMask.push_back(NewElt);
6119 }
6120 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
6121 DAG.getUNDEF(VT), NewMask.data());
6122}
6123
Bob Wilson1c3ef902011-02-07 17:43:21 +00006124/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
6125/// NEON load/store intrinsics to merge base address updates.
6126static SDValue CombineBaseUpdate(SDNode *N,
6127 TargetLowering::DAGCombinerInfo &DCI) {
6128 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6129 return SDValue();
6130
6131 SelectionDAG &DAG = DCI.DAG;
6132 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
6133 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
6134 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
6135 SDValue Addr = N->getOperand(AddrOpIdx);
6136
6137 // Search for a use of the address operand that is an increment.
6138 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
6139 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
6140 SDNode *User = *UI;
6141 if (User->getOpcode() != ISD::ADD ||
6142 UI.getUse().getResNo() != Addr.getResNo())
6143 continue;
6144
6145 // Check that the add is independent of the load/store. Otherwise, folding
6146 // it would create a cycle.
6147 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
6148 continue;
6149
6150 // Find the new opcode for the updating load/store.
6151 bool isLoad = true;
6152 bool isLaneOp = false;
6153 unsigned NewOpc = 0;
6154 unsigned NumVecs = 0;
6155 if (isIntrinsic) {
6156 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
6157 switch (IntNo) {
6158 default: assert(0 && "unexpected intrinsic for Neon base update");
6159 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
6160 NumVecs = 1; break;
6161 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
6162 NumVecs = 2; break;
6163 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
6164 NumVecs = 3; break;
6165 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
6166 NumVecs = 4; break;
6167 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
6168 NumVecs = 2; isLaneOp = true; break;
6169 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
6170 NumVecs = 3; isLaneOp = true; break;
6171 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
6172 NumVecs = 4; isLaneOp = true; break;
6173 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
6174 NumVecs = 1; isLoad = false; break;
6175 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
6176 NumVecs = 2; isLoad = false; break;
6177 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
6178 NumVecs = 3; isLoad = false; break;
6179 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
6180 NumVecs = 4; isLoad = false; break;
6181 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
6182 NumVecs = 2; isLoad = false; isLaneOp = true; break;
6183 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
6184 NumVecs = 3; isLoad = false; isLaneOp = true; break;
6185 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
6186 NumVecs = 4; isLoad = false; isLaneOp = true; break;
6187 }
6188 } else {
6189 isLaneOp = true;
6190 switch (N->getOpcode()) {
6191 default: assert(0 && "unexpected opcode for Neon base update");
6192 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
6193 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
6194 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
6195 }
6196 }
6197
6198 // Find the size of memory referenced by the load/store.
6199 EVT VecTy;
6200 if (isLoad)
6201 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00006202 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00006203 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
6204 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
6205 if (isLaneOp)
6206 NumBytes /= VecTy.getVectorNumElements();
6207
6208 // If the increment is a constant, it must match the memory ref size.
6209 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
6210 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
6211 uint64_t IncVal = CInc->getZExtValue();
6212 if (IncVal != NumBytes)
6213 continue;
6214 } else if (NumBytes >= 3 * 16) {
6215 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
6216 // separate instructions that make it harder to use a non-constant update.
6217 continue;
6218 }
6219
6220 // Create the new updating load/store node.
6221 EVT Tys[6];
6222 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
6223 unsigned n;
6224 for (n = 0; n < NumResultVecs; ++n)
6225 Tys[n] = VecTy;
6226 Tys[n++] = MVT::i32;
6227 Tys[n] = MVT::Other;
6228 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
6229 SmallVector<SDValue, 8> Ops;
6230 Ops.push_back(N->getOperand(0)); // incoming chain
6231 Ops.push_back(N->getOperand(AddrOpIdx));
6232 Ops.push_back(Inc);
6233 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
6234 Ops.push_back(N->getOperand(i));
6235 }
6236 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
6237 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
6238 Ops.data(), Ops.size(),
6239 MemInt->getMemoryVT(),
6240 MemInt->getMemOperand());
6241
6242 // Update the uses.
6243 std::vector<SDValue> NewResults;
6244 for (unsigned i = 0; i < NumResultVecs; ++i) {
6245 NewResults.push_back(SDValue(UpdN.getNode(), i));
6246 }
6247 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
6248 DCI.CombineTo(N, NewResults);
6249 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
6250
6251 break;
Owen Anderson76706012011-04-05 21:48:57 +00006252 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00006253 return SDValue();
6254}
6255
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006256/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
6257/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
6258/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
6259/// return true.
6260static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
6261 SelectionDAG &DAG = DCI.DAG;
6262 EVT VT = N->getValueType(0);
6263 // vldN-dup instructions only support 64-bit vectors for N > 1.
6264 if (!VT.is64BitVector())
6265 return false;
6266
6267 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
6268 SDNode *VLD = N->getOperand(0).getNode();
6269 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
6270 return false;
6271 unsigned NumVecs = 0;
6272 unsigned NewOpc = 0;
6273 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
6274 if (IntNo == Intrinsic::arm_neon_vld2lane) {
6275 NumVecs = 2;
6276 NewOpc = ARMISD::VLD2DUP;
6277 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
6278 NumVecs = 3;
6279 NewOpc = ARMISD::VLD3DUP;
6280 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
6281 NumVecs = 4;
6282 NewOpc = ARMISD::VLD4DUP;
6283 } else {
6284 return false;
6285 }
6286
6287 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
6288 // numbers match the load.
6289 unsigned VLDLaneNo =
6290 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
6291 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6292 UI != UE; ++UI) {
6293 // Ignore uses of the chain result.
6294 if (UI.getUse().getResNo() == NumVecs)
6295 continue;
6296 SDNode *User = *UI;
6297 if (User->getOpcode() != ARMISD::VDUPLANE ||
6298 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
6299 return false;
6300 }
6301
6302 // Create the vldN-dup node.
6303 EVT Tys[5];
6304 unsigned n;
6305 for (n = 0; n < NumVecs; ++n)
6306 Tys[n] = VT;
6307 Tys[n] = MVT::Other;
6308 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
6309 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
6310 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
6311 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
6312 Ops, 2, VLDMemInt->getMemoryVT(),
6313 VLDMemInt->getMemOperand());
6314
6315 // Update the uses.
6316 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6317 UI != UE; ++UI) {
6318 unsigned ResNo = UI.getUse().getResNo();
6319 // Ignore uses of the chain result.
6320 if (ResNo == NumVecs)
6321 continue;
6322 SDNode *User = *UI;
6323 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
6324 }
6325
6326 // Now the vldN-lane intrinsic is dead except for its chain result.
6327 // Update uses of the chain.
6328 std::vector<SDValue> VLDDupResults;
6329 for (unsigned n = 0; n < NumVecs; ++n)
6330 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
6331 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
6332 DCI.CombineTo(VLD, VLDDupResults);
6333
6334 return true;
6335}
6336
Bob Wilson9e82bf12010-07-14 01:22:12 +00006337/// PerformVDUPLANECombine - Target-specific dag combine xforms for
6338/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006339static SDValue PerformVDUPLANECombine(SDNode *N,
6340 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00006341 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006342
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006343 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
6344 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
6345 if (CombineVLDDUP(N, DCI))
6346 return SDValue(N, 0);
6347
6348 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
6349 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006350 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006351 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00006352 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006353 return SDValue();
6354
6355 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
6356 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
6357 // The canonical VMOV for a zero vector uses a 32-bit element size.
6358 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6359 unsigned EltBits;
6360 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
6361 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006362 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006363 if (EltSize > VT.getVectorElementType().getSizeInBits())
6364 return SDValue();
6365
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006366 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006367}
6368
Bob Wilson5bafff32009-06-22 23:27:02 +00006369/// getVShiftImm - Check if this is a valid build_vector for the immediate
6370/// operand of a vector shift operation, where all the elements of the
6371/// build_vector must have the same constant integer value.
6372static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
6373 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006374 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00006375 Op = Op.getOperand(0);
6376 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
6377 APInt SplatBits, SplatUndef;
6378 unsigned SplatBitSize;
6379 bool HasAnyUndefs;
6380 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
6381 HasAnyUndefs, ElementBits) ||
6382 SplatBitSize > ElementBits)
6383 return false;
6384 Cnt = SplatBits.getSExtValue();
6385 return true;
6386}
6387
6388/// isVShiftLImm - Check if this is a valid build_vector for the immediate
6389/// operand of a vector shift left operation. That value must be in the range:
6390/// 0 <= Value < ElementBits for a left shift; or
6391/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006392static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006393 assert(VT.isVector() && "vector shift count is not a vector type");
6394 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6395 if (! getVShiftImm(Op, ElementBits, Cnt))
6396 return false;
6397 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
6398}
6399
6400/// isVShiftRImm - Check if this is a valid build_vector for the immediate
6401/// operand of a vector shift right operation. For a shift opcode, the value
6402/// is positive, but for an intrinsic the value count must be negative. The
6403/// absolute value must be in the range:
6404/// 1 <= |Value| <= ElementBits for a right shift; or
6405/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006406static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00006407 int64_t &Cnt) {
6408 assert(VT.isVector() && "vector shift count is not a vector type");
6409 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6410 if (! getVShiftImm(Op, ElementBits, Cnt))
6411 return false;
6412 if (isIntrinsic)
6413 Cnt = -Cnt;
6414 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
6415}
6416
6417/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
6418static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
6419 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
6420 switch (IntNo) {
6421 default:
6422 // Don't do anything for most intrinsics.
6423 break;
6424
6425 // Vector shifts: check for immediate versions and lower them.
6426 // Note: This is done during DAG combining instead of DAG legalizing because
6427 // the build_vectors for 64-bit vector element shift counts are generally
6428 // not legal, and it is hard to see their values after they get legalized to
6429 // loads from a constant pool.
6430 case Intrinsic::arm_neon_vshifts:
6431 case Intrinsic::arm_neon_vshiftu:
6432 case Intrinsic::arm_neon_vshiftls:
6433 case Intrinsic::arm_neon_vshiftlu:
6434 case Intrinsic::arm_neon_vshiftn:
6435 case Intrinsic::arm_neon_vrshifts:
6436 case Intrinsic::arm_neon_vrshiftu:
6437 case Intrinsic::arm_neon_vrshiftn:
6438 case Intrinsic::arm_neon_vqshifts:
6439 case Intrinsic::arm_neon_vqshiftu:
6440 case Intrinsic::arm_neon_vqshiftsu:
6441 case Intrinsic::arm_neon_vqshiftns:
6442 case Intrinsic::arm_neon_vqshiftnu:
6443 case Intrinsic::arm_neon_vqshiftnsu:
6444 case Intrinsic::arm_neon_vqrshiftns:
6445 case Intrinsic::arm_neon_vqrshiftnu:
6446 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00006447 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006448 int64_t Cnt;
6449 unsigned VShiftOpc = 0;
6450
6451 switch (IntNo) {
6452 case Intrinsic::arm_neon_vshifts:
6453 case Intrinsic::arm_neon_vshiftu:
6454 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
6455 VShiftOpc = ARMISD::VSHL;
6456 break;
6457 }
6458 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
6459 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
6460 ARMISD::VSHRs : ARMISD::VSHRu);
6461 break;
6462 }
6463 return SDValue();
6464
6465 case Intrinsic::arm_neon_vshiftls:
6466 case Intrinsic::arm_neon_vshiftlu:
6467 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
6468 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006469 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006470
6471 case Intrinsic::arm_neon_vrshifts:
6472 case Intrinsic::arm_neon_vrshiftu:
6473 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
6474 break;
6475 return SDValue();
6476
6477 case Intrinsic::arm_neon_vqshifts:
6478 case Intrinsic::arm_neon_vqshiftu:
6479 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6480 break;
6481 return SDValue();
6482
6483 case Intrinsic::arm_neon_vqshiftsu:
6484 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6485 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006486 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006487
6488 case Intrinsic::arm_neon_vshiftn:
6489 case Intrinsic::arm_neon_vrshiftn:
6490 case Intrinsic::arm_neon_vqshiftns:
6491 case Intrinsic::arm_neon_vqshiftnu:
6492 case Intrinsic::arm_neon_vqshiftnsu:
6493 case Intrinsic::arm_neon_vqrshiftns:
6494 case Intrinsic::arm_neon_vqrshiftnu:
6495 case Intrinsic::arm_neon_vqrshiftnsu:
6496 // Narrowing shifts require an immediate right shift.
6497 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
6498 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00006499 llvm_unreachable("invalid shift count for narrowing vector shift "
6500 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006501
6502 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006503 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00006504 }
6505
6506 switch (IntNo) {
6507 case Intrinsic::arm_neon_vshifts:
6508 case Intrinsic::arm_neon_vshiftu:
6509 // Opcode already set above.
6510 break;
6511 case Intrinsic::arm_neon_vshiftls:
6512 case Intrinsic::arm_neon_vshiftlu:
6513 if (Cnt == VT.getVectorElementType().getSizeInBits())
6514 VShiftOpc = ARMISD::VSHLLi;
6515 else
6516 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
6517 ARMISD::VSHLLs : ARMISD::VSHLLu);
6518 break;
6519 case Intrinsic::arm_neon_vshiftn:
6520 VShiftOpc = ARMISD::VSHRN; break;
6521 case Intrinsic::arm_neon_vrshifts:
6522 VShiftOpc = ARMISD::VRSHRs; break;
6523 case Intrinsic::arm_neon_vrshiftu:
6524 VShiftOpc = ARMISD::VRSHRu; break;
6525 case Intrinsic::arm_neon_vrshiftn:
6526 VShiftOpc = ARMISD::VRSHRN; break;
6527 case Intrinsic::arm_neon_vqshifts:
6528 VShiftOpc = ARMISD::VQSHLs; break;
6529 case Intrinsic::arm_neon_vqshiftu:
6530 VShiftOpc = ARMISD::VQSHLu; break;
6531 case Intrinsic::arm_neon_vqshiftsu:
6532 VShiftOpc = ARMISD::VQSHLsu; break;
6533 case Intrinsic::arm_neon_vqshiftns:
6534 VShiftOpc = ARMISD::VQSHRNs; break;
6535 case Intrinsic::arm_neon_vqshiftnu:
6536 VShiftOpc = ARMISD::VQSHRNu; break;
6537 case Intrinsic::arm_neon_vqshiftnsu:
6538 VShiftOpc = ARMISD::VQSHRNsu; break;
6539 case Intrinsic::arm_neon_vqrshiftns:
6540 VShiftOpc = ARMISD::VQRSHRNs; break;
6541 case Intrinsic::arm_neon_vqrshiftnu:
6542 VShiftOpc = ARMISD::VQRSHRNu; break;
6543 case Intrinsic::arm_neon_vqrshiftnsu:
6544 VShiftOpc = ARMISD::VQRSHRNsu; break;
6545 }
6546
6547 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006548 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006549 }
6550
6551 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00006552 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006553 int64_t Cnt;
6554 unsigned VShiftOpc = 0;
6555
6556 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
6557 VShiftOpc = ARMISD::VSLI;
6558 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
6559 VShiftOpc = ARMISD::VSRI;
6560 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006561 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006562 }
6563
6564 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
6565 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00006566 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006567 }
6568
6569 case Intrinsic::arm_neon_vqrshifts:
6570 case Intrinsic::arm_neon_vqrshiftu:
6571 // No immediate versions of these to check for.
6572 break;
6573 }
6574
6575 return SDValue();
6576}
6577
6578/// PerformShiftCombine - Checks for immediate versions of vector shifts and
6579/// lowers them. As with the vector shift intrinsics, this is done during DAG
6580/// combining instead of DAG legalizing because the build_vectors for 64-bit
6581/// vector element shift counts are generally not legal, and it is hard to see
6582/// their values after they get legalized to loads from a constant pool.
6583static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
6584 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00006585 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00006586
6587 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00006588 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6589 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00006590 return SDValue();
6591
6592 assert(ST->hasNEON() && "unexpected vector shift");
6593 int64_t Cnt;
6594
6595 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006596 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006597
6598 case ISD::SHL:
6599 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
6600 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006601 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006602 break;
6603
6604 case ISD::SRA:
6605 case ISD::SRL:
6606 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
6607 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
6608 ARMISD::VSHRs : ARMISD::VSHRu);
6609 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006610 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006611 }
6612 }
6613 return SDValue();
6614}
6615
6616/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
6617/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
6618static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
6619 const ARMSubtarget *ST) {
6620 SDValue N0 = N->getOperand(0);
6621
6622 // Check for sign- and zero-extensions of vector extract operations of 8-
6623 // and 16-bit vector elements. NEON supports these directly. They are
6624 // handled during DAG combining because type legalization will promote them
6625 // to 32-bit types and it is messy to recognize the operations after that.
6626 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
6627 SDValue Vec = N0.getOperand(0);
6628 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006629 EVT VT = N->getValueType(0);
6630 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006631 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6632
Owen Anderson825b72b2009-08-11 20:47:22 +00006633 if (VT == MVT::i32 &&
6634 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00006635 TLI.isTypeLegal(Vec.getValueType()) &&
6636 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006637
6638 unsigned Opc = 0;
6639 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006640 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006641 case ISD::SIGN_EXTEND:
6642 Opc = ARMISD::VGETLANEs;
6643 break;
6644 case ISD::ZERO_EXTEND:
6645 case ISD::ANY_EXTEND:
6646 Opc = ARMISD::VGETLANEu;
6647 break;
6648 }
6649 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
6650 }
6651 }
6652
6653 return SDValue();
6654}
6655
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006656/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
6657/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
6658static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
6659 const ARMSubtarget *ST) {
6660 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00006661 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006662 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
6663 // a NaN; only do the transformation when it matches that behavior.
6664
6665 // For now only do this when using NEON for FP operations; if using VFP, it
6666 // is not obvious that the benefit outweighs the cost of switching to the
6667 // NEON pipeline.
6668 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
6669 N->getValueType(0) != MVT::f32)
6670 return SDValue();
6671
6672 SDValue CondLHS = N->getOperand(0);
6673 SDValue CondRHS = N->getOperand(1);
6674 SDValue LHS = N->getOperand(2);
6675 SDValue RHS = N->getOperand(3);
6676 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
6677
6678 unsigned Opcode = 0;
6679 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00006680 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006681 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00006682 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006683 IsReversed = true ; // x CC y ? y : x
6684 } else {
6685 return SDValue();
6686 }
6687
Bob Wilsone742bb52010-02-24 22:15:53 +00006688 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006689 switch (CC) {
6690 default: break;
6691 case ISD::SETOLT:
6692 case ISD::SETOLE:
6693 case ISD::SETLT:
6694 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006695 case ISD::SETULT:
6696 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006697 // If LHS is NaN, an ordered comparison will be false and the result will
6698 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
6699 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6700 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
6701 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6702 break;
6703 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
6704 // will return -0, so vmin can only be used for unsafe math or if one of
6705 // the operands is known to be nonzero.
6706 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
6707 !UnsafeFPMath &&
6708 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6709 break;
6710 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006711 break;
6712
6713 case ISD::SETOGT:
6714 case ISD::SETOGE:
6715 case ISD::SETGT:
6716 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006717 case ISD::SETUGT:
6718 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006719 // If LHS is NaN, an ordered comparison will be false and the result will
6720 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
6721 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6722 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
6723 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6724 break;
6725 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
6726 // will return +0, so vmax can only be used for unsafe math or if one of
6727 // the operands is known to be nonzero.
6728 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
6729 !UnsafeFPMath &&
6730 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6731 break;
6732 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006733 break;
6734 }
6735
6736 if (!Opcode)
6737 return SDValue();
6738 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
6739}
6740
Dan Gohman475871a2008-07-27 21:46:04 +00006741SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006742 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006743 switch (N->getOpcode()) {
6744 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006745 case ISD::ADD: return PerformADDCombine(N, DCI);
6746 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006747 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006748 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00006749 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00006750 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00006751 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006752 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00006753 case ISD::STORE: return PerformSTORECombine(N, DCI);
6754 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
6755 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00006756 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006757 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006758 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00006759 case ISD::SHL:
6760 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006761 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00006762 case ISD::SIGN_EXTEND:
6763 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006764 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
6765 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Bob Wilson1c3ef902011-02-07 17:43:21 +00006766 case ARMISD::VLD2DUP:
6767 case ARMISD::VLD3DUP:
6768 case ARMISD::VLD4DUP:
6769 return CombineBaseUpdate(N, DCI);
6770 case ISD::INTRINSIC_VOID:
6771 case ISD::INTRINSIC_W_CHAIN:
6772 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
6773 case Intrinsic::arm_neon_vld1:
6774 case Intrinsic::arm_neon_vld2:
6775 case Intrinsic::arm_neon_vld3:
6776 case Intrinsic::arm_neon_vld4:
6777 case Intrinsic::arm_neon_vld2lane:
6778 case Intrinsic::arm_neon_vld3lane:
6779 case Intrinsic::arm_neon_vld4lane:
6780 case Intrinsic::arm_neon_vst1:
6781 case Intrinsic::arm_neon_vst2:
6782 case Intrinsic::arm_neon_vst3:
6783 case Intrinsic::arm_neon_vst4:
6784 case Intrinsic::arm_neon_vst2lane:
6785 case Intrinsic::arm_neon_vst3lane:
6786 case Intrinsic::arm_neon_vst4lane:
6787 return CombineBaseUpdate(N, DCI);
6788 default: break;
6789 }
6790 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006791 }
Dan Gohman475871a2008-07-27 21:46:04 +00006792 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006793}
6794
Evan Cheng31959b12011-02-02 01:06:55 +00006795bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
6796 EVT VT) const {
6797 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
6798}
6799
Bill Wendlingaf566342009-08-15 21:21:19 +00006800bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00006801 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00006802 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00006803
6804 switch (VT.getSimpleVT().SimpleTy) {
6805 default:
6806 return false;
6807 case MVT::i8:
6808 case MVT::i16:
6809 case MVT::i32:
6810 return true;
6811 // FIXME: VLD1 etc with standard alignment is legal.
6812 }
6813}
6814
Evan Chenge6c835f2009-08-14 20:09:37 +00006815static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
6816 if (V < 0)
6817 return false;
6818
6819 unsigned Scale = 1;
6820 switch (VT.getSimpleVT().SimpleTy) {
6821 default: return false;
6822 case MVT::i1:
6823 case MVT::i8:
6824 // Scale == 1;
6825 break;
6826 case MVT::i16:
6827 // Scale == 2;
6828 Scale = 2;
6829 break;
6830 case MVT::i32:
6831 // Scale == 4;
6832 Scale = 4;
6833 break;
6834 }
6835
6836 if ((V & (Scale - 1)) != 0)
6837 return false;
6838 V /= Scale;
6839 return V == (V & ((1LL << 5) - 1));
6840}
6841
6842static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
6843 const ARMSubtarget *Subtarget) {
6844 bool isNeg = false;
6845 if (V < 0) {
6846 isNeg = true;
6847 V = - V;
6848 }
6849
6850 switch (VT.getSimpleVT().SimpleTy) {
6851 default: return false;
6852 case MVT::i1:
6853 case MVT::i8:
6854 case MVT::i16:
6855 case MVT::i32:
6856 // + imm12 or - imm8
6857 if (isNeg)
6858 return V == (V & ((1LL << 8) - 1));
6859 return V == (V & ((1LL << 12) - 1));
6860 case MVT::f32:
6861 case MVT::f64:
6862 // Same as ARM mode. FIXME: NEON?
6863 if (!Subtarget->hasVFP2())
6864 return false;
6865 if ((V & 3) != 0)
6866 return false;
6867 V >>= 2;
6868 return V == (V & ((1LL << 8) - 1));
6869 }
6870}
6871
Evan Chengb01fad62007-03-12 23:30:29 +00006872/// isLegalAddressImmediate - Return true if the integer value can be used
6873/// as the offset of the target addressing mode for load / store of the
6874/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00006875static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006876 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00006877 if (V == 0)
6878 return true;
6879
Evan Cheng65011532009-03-09 19:15:00 +00006880 if (!VT.isSimple())
6881 return false;
6882
Evan Chenge6c835f2009-08-14 20:09:37 +00006883 if (Subtarget->isThumb1Only())
6884 return isLegalT1AddressImmediate(V, VT);
6885 else if (Subtarget->isThumb2())
6886 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00006887
Evan Chenge6c835f2009-08-14 20:09:37 +00006888 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00006889 if (V < 0)
6890 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00006891 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00006892 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006893 case MVT::i1:
6894 case MVT::i8:
6895 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00006896 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006897 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006898 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00006899 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006900 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006901 case MVT::f32:
6902 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00006903 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00006904 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00006905 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00006906 return false;
6907 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006908 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00006909 }
Evan Chenga8e29892007-01-19 07:51:42 +00006910}
6911
Evan Chenge6c835f2009-08-14 20:09:37 +00006912bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
6913 EVT VT) const {
6914 int Scale = AM.Scale;
6915 if (Scale < 0)
6916 return false;
6917
6918 switch (VT.getSimpleVT().SimpleTy) {
6919 default: return false;
6920 case MVT::i1:
6921 case MVT::i8:
6922 case MVT::i16:
6923 case MVT::i32:
6924 if (Scale == 1)
6925 return true;
6926 // r + r << imm
6927 Scale = Scale & ~1;
6928 return Scale == 2 || Scale == 4 || Scale == 8;
6929 case MVT::i64:
6930 // r + r
6931 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
6932 return true;
6933 return false;
6934 case MVT::isVoid:
6935 // Note, we allow "void" uses (basically, uses that aren't loads or
6936 // stores), because arm allows folding a scale into many arithmetic
6937 // operations. This should be made more precise and revisited later.
6938
6939 // Allow r << imm, but the imm has to be a multiple of two.
6940 if (Scale & 1) return false;
6941 return isPowerOf2_32(Scale);
6942 }
6943}
6944
Chris Lattner37caf8c2007-04-09 23:33:39 +00006945/// isLegalAddressingMode - Return true if the addressing mode represented
6946/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006947bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006948 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006949 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00006950 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00006951 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006952
Chris Lattner37caf8c2007-04-09 23:33:39 +00006953 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006954 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006955 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006956
Chris Lattner37caf8c2007-04-09 23:33:39 +00006957 switch (AM.Scale) {
6958 case 0: // no scale reg, must be "r+i" or "r", or "i".
6959 break;
6960 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00006961 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00006962 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006963 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00006964 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006965 // ARM doesn't support any R+R*scale+imm addr modes.
6966 if (AM.BaseOffs)
6967 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006968
Bob Wilson2c7dab12009-04-08 17:55:28 +00006969 if (!VT.isSimple())
6970 return false;
6971
Evan Chenge6c835f2009-08-14 20:09:37 +00006972 if (Subtarget->isThumb2())
6973 return isLegalT2ScaledAddressingMode(AM, VT);
6974
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006975 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00006976 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00006977 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006978 case MVT::i1:
6979 case MVT::i8:
6980 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006981 if (Scale < 0) Scale = -Scale;
6982 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006983 return true;
6984 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00006985 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006986 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00006987 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00006988 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006989 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006990 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00006991 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006992
Owen Anderson825b72b2009-08-11 20:47:22 +00006993 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00006994 // Note, we allow "void" uses (basically, uses that aren't loads or
6995 // stores), because arm allows folding a scale into many arithmetic
6996 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006997
Chris Lattner37caf8c2007-04-09 23:33:39 +00006998 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00006999 if (Scale & 1) return false;
7000 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00007001 }
7002 break;
Evan Chengb01fad62007-03-12 23:30:29 +00007003 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00007004 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00007005}
7006
Evan Cheng77e47512009-11-11 19:05:52 +00007007/// isLegalICmpImmediate - Return true if the specified immediate is legal
7008/// icmp immediate, that is the target has icmp instructions which can compare
7009/// a register against the immediate without having to materialize the
7010/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00007011bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00007012 if (!Subtarget->isThumb())
7013 return ARM_AM::getSOImmVal(Imm) != -1;
7014 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00007015 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00007016 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00007017}
7018
Dan Gohmancca82142011-05-03 00:46:49 +00007019/// isLegalAddImmediate - Return true if the specified immediate is legal
7020/// add immediate, that is the target has add instructions which can add
7021/// a register with the immediate without having to materialize the
7022/// immediate into a register.
7023bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
7024 return ARM_AM::getSOImmVal(Imm) != -1;
7025}
7026
Owen Andersone50ed302009-08-10 22:56:29 +00007027static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007028 bool isSEXTLoad, SDValue &Base,
7029 SDValue &Offset, bool &isInc,
7030 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00007031 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7032 return false;
7033
Owen Anderson825b72b2009-08-11 20:47:22 +00007034 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00007035 // AddressingMode 3
7036 Base = Ptr->getOperand(0);
7037 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007038 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007039 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007040 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007041 isInc = false;
7042 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7043 return true;
7044 }
7045 }
7046 isInc = (Ptr->getOpcode() == ISD::ADD);
7047 Offset = Ptr->getOperand(1);
7048 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00007049 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00007050 // AddressingMode 2
7051 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007052 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007053 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007054 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007055 isInc = false;
7056 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7057 Base = Ptr->getOperand(0);
7058 return true;
7059 }
7060 }
7061
7062 if (Ptr->getOpcode() == ISD::ADD) {
7063 isInc = true;
7064 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
7065 if (ShOpcVal != ARM_AM::no_shift) {
7066 Base = Ptr->getOperand(1);
7067 Offset = Ptr->getOperand(0);
7068 } else {
7069 Base = Ptr->getOperand(0);
7070 Offset = Ptr->getOperand(1);
7071 }
7072 return true;
7073 }
7074
7075 isInc = (Ptr->getOpcode() == ISD::ADD);
7076 Base = Ptr->getOperand(0);
7077 Offset = Ptr->getOperand(1);
7078 return true;
7079 }
7080
Jim Grosbache5165492009-11-09 00:11:35 +00007081 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00007082 return false;
7083}
7084
Owen Andersone50ed302009-08-10 22:56:29 +00007085static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007086 bool isSEXTLoad, SDValue &Base,
7087 SDValue &Offset, bool &isInc,
7088 SelectionDAG &DAG) {
7089 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7090 return false;
7091
7092 Base = Ptr->getOperand(0);
7093 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
7094 int RHSC = (int)RHS->getZExtValue();
7095 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
7096 assert(Ptr->getOpcode() == ISD::ADD);
7097 isInc = false;
7098 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7099 return true;
7100 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
7101 isInc = Ptr->getOpcode() == ISD::ADD;
7102 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
7103 return true;
7104 }
7105 }
7106
7107 return false;
7108}
7109
Evan Chenga8e29892007-01-19 07:51:42 +00007110/// getPreIndexedAddressParts - returns true by value, base pointer and
7111/// offset pointer and addressing mode by reference if the node's address
7112/// can be legally represented as pre-indexed load / store address.
7113bool
Dan Gohman475871a2008-07-27 21:46:04 +00007114ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
7115 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007116 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007117 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007118 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007119 return false;
7120
Owen Andersone50ed302009-08-10 22:56:29 +00007121 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007122 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007123 bool isSEXTLoad = false;
7124 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
7125 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007126 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007127 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7128 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
7129 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007130 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007131 } else
7132 return false;
7133
7134 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007135 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007136 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007137 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
7138 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007139 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007140 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00007141 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00007142 if (!isLegal)
7143 return false;
7144
7145 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
7146 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007147}
7148
7149/// getPostIndexedAddressParts - returns true by value, base pointer and
7150/// offset pointer and addressing mode by reference if this node can be
7151/// combined with a load / store to form a post-indexed load / store.
7152bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00007153 SDValue &Base,
7154 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007155 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007156 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007157 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007158 return false;
7159
Owen Andersone50ed302009-08-10 22:56:29 +00007160 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007161 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007162 bool isSEXTLoad = false;
7163 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007164 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007165 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007166 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7167 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007168 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007169 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007170 } else
7171 return false;
7172
7173 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007174 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007175 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007176 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00007177 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007178 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007179 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
7180 isInc, DAG);
7181 if (!isLegal)
7182 return false;
7183
Evan Cheng28dad2a2010-05-18 21:31:17 +00007184 if (Ptr != Base) {
7185 // Swap base ptr and offset to catch more post-index load / store when
7186 // it's legal. In Thumb2 mode, offset must be an immediate.
7187 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
7188 !Subtarget->isThumb2())
7189 std::swap(Base, Offset);
7190
7191 // Post-indexed load / store update the base pointer.
7192 if (Ptr != Base)
7193 return false;
7194 }
7195
Evan Chenge88d5ce2009-07-02 07:28:31 +00007196 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
7197 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007198}
7199
Dan Gohman475871a2008-07-27 21:46:04 +00007200void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007201 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00007202 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007203 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007204 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00007205 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007206 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007207 switch (Op.getOpcode()) {
7208 default: break;
7209 case ARMISD::CMOV: {
7210 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00007211 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007212 if (KnownZero == 0 && KnownOne == 0) return;
7213
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007214 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00007215 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
7216 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007217 KnownZero &= KnownZeroRHS;
7218 KnownOne &= KnownOneRHS;
7219 return;
7220 }
7221 }
7222}
7223
7224//===----------------------------------------------------------------------===//
7225// ARM Inline Assembly Support
7226//===----------------------------------------------------------------------===//
7227
Evan Cheng55d42002011-01-08 01:24:27 +00007228bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
7229 // Looking for "rev" which is V6+.
7230 if (!Subtarget->hasV6Ops())
7231 return false;
7232
7233 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
7234 std::string AsmStr = IA->getAsmString();
7235 SmallVector<StringRef, 4> AsmPieces;
7236 SplitString(AsmStr, AsmPieces, ";\n");
7237
7238 switch (AsmPieces.size()) {
7239 default: return false;
7240 case 1:
7241 AsmStr = AsmPieces[0];
7242 AsmPieces.clear();
7243 SplitString(AsmStr, AsmPieces, " \t,");
7244
7245 // rev $0, $1
7246 if (AsmPieces.size() == 3 &&
7247 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
7248 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
7249 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
7250 if (Ty && Ty->getBitWidth() == 32)
7251 return IntrinsicLowering::LowerToByteSwap(CI);
7252 }
7253 break;
7254 }
7255
7256 return false;
7257}
7258
Evan Chenga8e29892007-01-19 07:51:42 +00007259/// getConstraintType - Given a constraint letter, return the type of
7260/// constraint it is for this target.
7261ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00007262ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
7263 if (Constraint.size() == 1) {
7264 switch (Constraint[0]) {
7265 default: break;
7266 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007267 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00007268 }
Eric Christophercf714d42011-06-03 17:24:37 +00007269 } else {
7270 if (Constraint == "Uv")
7271 return C_Memory;
Evan Chenga8e29892007-01-19 07:51:42 +00007272 }
Chris Lattner4234f572007-03-25 02:14:49 +00007273 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00007274}
7275
John Thompson44ab89e2010-10-29 17:29:13 +00007276/// Examine constraint type and operand type and determine a weight value.
7277/// This object must already have been set up with the operand type
7278/// and the current alternative constraint selected.
7279TargetLowering::ConstraintWeight
7280ARMTargetLowering::getSingleConstraintMatchWeight(
7281 AsmOperandInfo &info, const char *constraint) const {
7282 ConstraintWeight weight = CW_Invalid;
7283 Value *CallOperandVal = info.CallOperandVal;
7284 // If we don't have a value, we can't do a match,
7285 // but allow it at the lowest weight.
7286 if (CallOperandVal == NULL)
7287 return CW_Default;
7288 const Type *type = CallOperandVal->getType();
7289 // Look at the constraint type.
7290 switch (*constraint) {
7291 default:
7292 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
7293 break;
7294 case 'l':
7295 if (type->isIntegerTy()) {
7296 if (Subtarget->isThumb())
7297 weight = CW_SpecificReg;
7298 else
7299 weight = CW_Register;
7300 }
7301 break;
7302 case 'w':
7303 if (type->isFloatingPointTy())
7304 weight = CW_Register;
7305 break;
7306 }
7307 return weight;
7308}
7309
Bob Wilson2dc4f542009-03-20 22:42:55 +00007310std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00007311ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007312 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007313 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007314 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00007315 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007316 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007317 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007318 return std::make_pair(0U, ARM::tGPRRegisterClass);
7319 else
7320 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007321 case 'r':
7322 return std::make_pair(0U, ARM::GPRRegisterClass);
7323 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007324 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007325 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00007326 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007327 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00007328 if (VT.getSizeInBits() == 128)
7329 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007330 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007331 }
7332 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007333 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00007334 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007335
Evan Chenga8e29892007-01-19 07:51:42 +00007336 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7337}
7338
7339std::vector<unsigned> ARMTargetLowering::
7340getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007341 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007342 if (Constraint.size() != 1)
7343 return std::vector<unsigned>();
7344
7345 switch (Constraint[0]) { // GCC ARM Constraint Letters
7346 default: break;
7347 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007348 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
7349 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
7350 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007351 case 'r':
7352 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
7353 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
7354 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
7355 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007356 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007357 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007358 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
7359 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
7360 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
7361 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
7362 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
7363 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
7364 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
7365 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00007366 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007367 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
7368 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
7369 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
7370 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00007371 if (VT.getSizeInBits() == 128)
7372 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
7373 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007374 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007375 }
7376
7377 return std::vector<unsigned>();
7378}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007379
7380/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7381/// vector. If it is invalid, don't add anything to Ops.
7382void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00007383 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007384 std::vector<SDValue>&Ops,
7385 SelectionDAG &DAG) const {
7386 SDValue Result(0, 0);
7387
Eric Christopher100c8332011-06-02 23:16:42 +00007388 // Currently only support length 1 constraints.
7389 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00007390
Eric Christopher100c8332011-06-02 23:16:42 +00007391 char ConstraintLetter = Constraint[0];
7392 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007393 default: break;
7394 case 'I': case 'J': case 'K': case 'L':
7395 case 'M': case 'N': case 'O':
7396 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
7397 if (!C)
7398 return;
7399
7400 int64_t CVal64 = C->getSExtValue();
7401 int CVal = (int) CVal64;
7402 // None of these constraints allow values larger than 32 bits. Check
7403 // that the value fits in an int.
7404 if (CVal != CVal64)
7405 return;
7406
Eric Christopher100c8332011-06-02 23:16:42 +00007407 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007408 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007409 if (Subtarget->isThumb1Only()) {
7410 // This must be a constant between 0 and 255, for ADD
7411 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007412 if (CVal >= 0 && CVal <= 255)
7413 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007414 } else if (Subtarget->isThumb2()) {
7415 // A constant that can be used as an immediate value in a
7416 // data-processing instruction.
7417 if (ARM_AM::getT2SOImmVal(CVal) != -1)
7418 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007419 } else {
7420 // A constant that can be used as an immediate value in a
7421 // data-processing instruction.
7422 if (ARM_AM::getSOImmVal(CVal) != -1)
7423 break;
7424 }
7425 return;
7426
7427 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007428 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007429 // This must be a constant between -255 and -1, for negated ADD
7430 // immediates. This can be used in GCC with an "n" modifier that
7431 // prints the negated value, for use with SUB instructions. It is
7432 // not useful otherwise but is implemented for compatibility.
7433 if (CVal >= -255 && CVal <= -1)
7434 break;
7435 } else {
7436 // This must be a constant between -4095 and 4095. It is not clear
7437 // what this constraint is intended for. Implemented for
7438 // compatibility with GCC.
7439 if (CVal >= -4095 && CVal <= 4095)
7440 break;
7441 }
7442 return;
7443
7444 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007445 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007446 // A 32-bit value where only one byte has a nonzero value. Exclude
7447 // zero to match GCC. This constraint is used by GCC internally for
7448 // constants that can be loaded with a move/shift combination.
7449 // It is not useful otherwise but is implemented for compatibility.
7450 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
7451 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007452 } else if (Subtarget->isThumb2()) {
7453 // A constant whose bitwise inverse can be used as an immediate
7454 // value in a data-processing instruction. This can be used in GCC
7455 // with a "B" modifier that prints the inverted value, for use with
7456 // BIC and MVN instructions. It is not useful otherwise but is
7457 // implemented for compatibility.
7458 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
7459 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007460 } else {
7461 // A constant whose bitwise inverse can be used as an immediate
7462 // value in a data-processing instruction. This can be used in GCC
7463 // with a "B" modifier that prints the inverted value, for use with
7464 // BIC and MVN instructions. It is not useful otherwise but is
7465 // implemented for compatibility.
7466 if (ARM_AM::getSOImmVal(~CVal) != -1)
7467 break;
7468 }
7469 return;
7470
7471 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007472 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007473 // This must be a constant between -7 and 7,
7474 // for 3-operand ADD/SUB immediate instructions.
7475 if (CVal >= -7 && CVal < 7)
7476 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007477 } else if (Subtarget->isThumb2()) {
7478 // A constant whose negation can be used as an immediate value in a
7479 // data-processing instruction. This can be used in GCC with an "n"
7480 // modifier that prints the negated value, for use with SUB
7481 // instructions. It is not useful otherwise but is implemented for
7482 // compatibility.
7483 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
7484 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007485 } else {
7486 // A constant whose negation can be used as an immediate value in a
7487 // data-processing instruction. This can be used in GCC with an "n"
7488 // modifier that prints the negated value, for use with SUB
7489 // instructions. It is not useful otherwise but is implemented for
7490 // compatibility.
7491 if (ARM_AM::getSOImmVal(-CVal) != -1)
7492 break;
7493 }
7494 return;
7495
7496 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007497 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007498 // This must be a multiple of 4 between 0 and 1020, for
7499 // ADD sp + immediate.
7500 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
7501 break;
7502 } else {
7503 // A power of two or a constant between 0 and 32. This is used in
7504 // GCC for the shift amount on shifted register operands, but it is
7505 // useful in general for any shift amounts.
7506 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
7507 break;
7508 }
7509 return;
7510
7511 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007512 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007513 // This must be a constant between 0 and 31, for shift amounts.
7514 if (CVal >= 0 && CVal <= 31)
7515 break;
7516 }
7517 return;
7518
7519 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007520 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007521 // This must be a multiple of 4 between -508 and 508, for
7522 // ADD/SUB sp = sp + immediate.
7523 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
7524 break;
7525 }
7526 return;
7527 }
7528 Result = DAG.getTargetConstant(CVal, Op.getValueType());
7529 break;
7530 }
7531
7532 if (Result.getNode()) {
7533 Ops.push_back(Result);
7534 return;
7535 }
Dale Johannesen1784d162010-06-25 21:55:36 +00007536 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007537}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00007538
7539bool
7540ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7541 // The ARM target isn't yet aware of offsets.
7542 return false;
7543}
Evan Cheng39382422009-10-28 01:44:26 +00007544
7545int ARM::getVFPf32Imm(const APFloat &FPImm) {
7546 APInt Imm = FPImm.bitcastToAPInt();
7547 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
7548 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
7549 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
7550
7551 // We can handle 4 bits of mantissa.
7552 // mantissa = (16+UInt(e:f:g:h))/16.
7553 if (Mantissa & 0x7ffff)
7554 return -1;
7555 Mantissa >>= 19;
7556 if ((Mantissa & 0xf) != Mantissa)
7557 return -1;
7558
7559 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7560 if (Exp < -3 || Exp > 4)
7561 return -1;
7562 Exp = ((Exp+3) & 0x7) ^ 4;
7563
7564 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7565}
7566
7567int ARM::getVFPf64Imm(const APFloat &FPImm) {
7568 APInt Imm = FPImm.bitcastToAPInt();
7569 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
7570 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
7571 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
7572
7573 // We can handle 4 bits of mantissa.
7574 // mantissa = (16+UInt(e:f:g:h))/16.
7575 if (Mantissa & 0xffffffffffffLL)
7576 return -1;
7577 Mantissa >>= 48;
7578 if ((Mantissa & 0xf) != Mantissa)
7579 return -1;
7580
7581 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7582 if (Exp < -3 || Exp > 4)
7583 return -1;
7584 Exp = ((Exp+3) & 0x7) ^ 4;
7585
7586 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7587}
7588
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007589bool ARM::isBitFieldInvertedMask(unsigned v) {
7590 if (v == 0xffffffff)
7591 return 0;
7592 // there can be 1's on either or both "outsides", all the "inside"
7593 // bits must be 0's
7594 unsigned int lsb = 0, msb = 31;
7595 while (v & (1 << msb)) --msb;
7596 while (v & (1 << lsb)) ++lsb;
7597 for (unsigned int i = lsb; i <= msb; ++i) {
7598 if (v & (1 << i))
7599 return 0;
7600 }
7601 return 1;
7602}
7603
Evan Cheng39382422009-10-28 01:44:26 +00007604/// isFPImmLegal - Returns true if the target can instruction select the
7605/// specified FP immediate natively. If false, the legalizer will
7606/// materialize the FP immediate as a load from a constant pool.
7607bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
7608 if (!Subtarget->hasVFP3())
7609 return false;
7610 if (VT == MVT::f32)
7611 return ARM::getVFPf32Imm(Imm) != -1;
7612 if (VT == MVT::f64)
7613 return ARM::getVFPf64Imm(Imm) != -1;
7614 return false;
7615}
Bob Wilson65ffec42010-09-21 17:56:22 +00007616
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007617/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00007618/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
7619/// specified in the intrinsic calls.
7620bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
7621 const CallInst &I,
7622 unsigned Intrinsic) const {
7623 switch (Intrinsic) {
7624 case Intrinsic::arm_neon_vld1:
7625 case Intrinsic::arm_neon_vld2:
7626 case Intrinsic::arm_neon_vld3:
7627 case Intrinsic::arm_neon_vld4:
7628 case Intrinsic::arm_neon_vld2lane:
7629 case Intrinsic::arm_neon_vld3lane:
7630 case Intrinsic::arm_neon_vld4lane: {
7631 Info.opc = ISD::INTRINSIC_W_CHAIN;
7632 // Conservatively set memVT to the entire set of vectors loaded.
7633 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
7634 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7635 Info.ptrVal = I.getArgOperand(0);
7636 Info.offset = 0;
7637 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7638 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7639 Info.vol = false; // volatile loads with NEON intrinsics not supported
7640 Info.readMem = true;
7641 Info.writeMem = false;
7642 return true;
7643 }
7644 case Intrinsic::arm_neon_vst1:
7645 case Intrinsic::arm_neon_vst2:
7646 case Intrinsic::arm_neon_vst3:
7647 case Intrinsic::arm_neon_vst4:
7648 case Intrinsic::arm_neon_vst2lane:
7649 case Intrinsic::arm_neon_vst3lane:
7650 case Intrinsic::arm_neon_vst4lane: {
7651 Info.opc = ISD::INTRINSIC_VOID;
7652 // Conservatively set memVT to the entire set of vectors stored.
7653 unsigned NumElts = 0;
7654 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
7655 const Type *ArgTy = I.getArgOperand(ArgI)->getType();
7656 if (!ArgTy->isVectorTy())
7657 break;
7658 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
7659 }
7660 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7661 Info.ptrVal = I.getArgOperand(0);
7662 Info.offset = 0;
7663 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7664 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7665 Info.vol = false; // volatile stores with NEON intrinsics not supported
7666 Info.readMem = false;
7667 Info.writeMem = true;
7668 return true;
7669 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00007670 case Intrinsic::arm_strexd: {
7671 Info.opc = ISD::INTRINSIC_W_CHAIN;
7672 Info.memVT = MVT::i64;
7673 Info.ptrVal = I.getArgOperand(2);
7674 Info.offset = 0;
7675 Info.align = 8;
7676 Info.vol = false;
7677 Info.readMem = false;
7678 Info.writeMem = true;
7679 return true;
7680 }
7681 case Intrinsic::arm_ldrexd: {
7682 Info.opc = ISD::INTRINSIC_W_CHAIN;
7683 Info.memVT = MVT::i64;
7684 Info.ptrVal = I.getArgOperand(0);
7685 Info.offset = 0;
7686 Info.align = 8;
7687 Info.vol = false;
7688 Info.readMem = true;
7689 Info.writeMem = false;
7690 return true;
7691 }
Bob Wilson65ffec42010-09-21 17:56:22 +00007692 default:
7693 break;
7694 }
7695
7696 return false;
7697}