blob: 802c99985a9f67162ec90218a4d1738c50067c5a [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);
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000399 }
400
David Goodwinf1daf7d2009-07-08 23:10:31 +0000401 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000402 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000403 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000405 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000406 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000407 if (!Subtarget->isFPOnlySP())
408 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000409
Owen Anderson825b72b2009-08-11 20:47:22 +0000410 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000411 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000412
413 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 addDRTypeForNEON(MVT::v2f32);
415 addDRTypeForNEON(MVT::v8i8);
416 addDRTypeForNEON(MVT::v4i16);
417 addDRTypeForNEON(MVT::v2i32);
418 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000419
Owen Anderson825b72b2009-08-11 20:47:22 +0000420 addQRTypeForNEON(MVT::v4f32);
421 addQRTypeForNEON(MVT::v2f64);
422 addQRTypeForNEON(MVT::v16i8);
423 addQRTypeForNEON(MVT::v8i16);
424 addQRTypeForNEON(MVT::v4i32);
425 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000426
Bob Wilson74dc72e2009-09-15 23:55:57 +0000427 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
428 // neither Neon nor VFP support any arithmetic operations on it.
429 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
430 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
431 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
432 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
433 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
434 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
435 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
436 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
437 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
438 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
439 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
440 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
441 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
442 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
443 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
444 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
445 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
446 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
447 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
448 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
449 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
450 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
451 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
452 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
453
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000454 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
455
Bob Wilson642b3292009-09-16 00:32:15 +0000456 // Neon does not support some operations on v1i64 and v2i64 types.
457 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000458 // Custom handling for some quad-vector types to detect VMULL.
459 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
460 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
461 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000462 // Custom handling for some vector types to avoid expensive expansions
463 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
464 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
465 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
466 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000467 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
468 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000469 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
470 // a destination type that is wider than the source.
471 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
472 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000473
Bob Wilson1c3ef902011-02-07 17:43:21 +0000474 setTargetDAGCombine(ISD::INTRINSIC_VOID);
475 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000476 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
477 setTargetDAGCombine(ISD::SHL);
478 setTargetDAGCombine(ISD::SRL);
479 setTargetDAGCombine(ISD::SRA);
480 setTargetDAGCombine(ISD::SIGN_EXTEND);
481 setTargetDAGCombine(ISD::ZERO_EXTEND);
482 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000483 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000484 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000485 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000486 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
487 setTargetDAGCombine(ISD::STORE);
Bob Wilson5bafff32009-06-22 23:27:02 +0000488 }
489
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000490 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000491
492 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000493 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000494
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000495 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000496 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000497
Evan Chenga8e29892007-01-19 07:51:42 +0000498 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000499 if (!Subtarget->isThumb1Only()) {
500 for (unsigned im = (unsigned)ISD::PRE_INC;
501 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000502 setIndexedLoadAction(im, MVT::i1, Legal);
503 setIndexedLoadAction(im, MVT::i8, Legal);
504 setIndexedLoadAction(im, MVT::i16, Legal);
505 setIndexedLoadAction(im, MVT::i32, Legal);
506 setIndexedStoreAction(im, MVT::i1, Legal);
507 setIndexedStoreAction(im, MVT::i8, Legal);
508 setIndexedStoreAction(im, MVT::i16, Legal);
509 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000510 }
Evan Chenga8e29892007-01-19 07:51:42 +0000511 }
512
513 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000514 setOperationAction(ISD::MUL, MVT::i64, Expand);
515 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000516 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
518 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000519 }
Eric Christopher2cc40132011-04-19 18:49:19 +0000520 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops())
521 setOperationAction(ISD::MULHS, MVT::i32, Expand);
522
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000523 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000524 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000525 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000526 setOperationAction(ISD::SRL, MVT::i64, Custom);
527 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000528
529 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000530 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000531 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000532 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000533 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000534 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000535
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000536 // Only ARMv6 has BSWAP.
537 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000539
Evan Chenga8e29892007-01-19 07:51:42 +0000540 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000541 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000542 // v7M has a hardware divider
543 setOperationAction(ISD::SDIV, MVT::i32, Expand);
544 setOperationAction(ISD::UDIV, MVT::i32, Expand);
545 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000546 setOperationAction(ISD::SREM, MVT::i32, Expand);
547 setOperationAction(ISD::UREM, MVT::i32, Expand);
548 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
549 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000550
Owen Anderson825b72b2009-08-11 20:47:22 +0000551 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
552 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
553 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
554 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000555 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000556
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000557 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000558
Evan Chenga8e29892007-01-19 07:51:42 +0000559 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000560 setOperationAction(ISD::VASTART, MVT::Other, Custom);
561 setOperationAction(ISD::VAARG, MVT::Other, Expand);
562 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
563 setOperationAction(ISD::VAEND, MVT::Other, Expand);
564 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
565 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000566 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000567 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
568 setExceptionPointerRegister(ARM::R0);
569 setExceptionSelectorRegister(ARM::R1);
570
Evan Cheng3a1588a2010-04-15 22:20:34 +0000571 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000572 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
573 // the default expansion.
574 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000575 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000576 // membarrier needs custom lowering; the rest are legal and handled
577 // normally.
578 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
579 } else {
580 // Set them all for expansion, which will force libcalls.
581 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
582 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
583 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
584 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000585 setOperationAction(ISD::ATOMIC_SWAP, MVT::i8, Expand);
586 setOperationAction(ISD::ATOMIC_SWAP, MVT::i16, Expand);
587 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000588 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
589 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
590 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
591 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
592 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
593 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
594 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
595 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
596 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
597 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
598 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
599 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
600 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
601 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
602 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
603 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
604 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
605 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000606 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i8, Expand);
607 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i16, Expand);
608 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
609 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i8, Expand);
610 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i16, Expand);
611 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
612 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i8, Expand);
613 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i16, Expand);
614 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
615 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i8, Expand);
616 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i16, Expand);
617 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000618 // Since the libcalls include locking, fold in the fences
619 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000620 }
621 // 64-bit versions are always libcalls (for now)
622 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000623 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000624 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
625 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
626 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
627 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
628 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
629 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000630
Evan Cheng416941d2010-11-04 05:19:35 +0000631 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000632
Eli Friedmana2c6f452010-06-26 04:36:50 +0000633 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
634 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
636 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000637 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000638 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000639
Nate Begemand1fb5832010-08-03 21:31:55 +0000640 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000641 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
642 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000643 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000644 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
645 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000646
647 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000648 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000649 if (Subtarget->isTargetDarwin()) {
650 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
651 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000652 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000653 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000654
Owen Anderson825b72b2009-08-11 20:47:22 +0000655 setOperationAction(ISD::SETCC, MVT::i32, Expand);
656 setOperationAction(ISD::SETCC, MVT::f32, Expand);
657 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000658 setOperationAction(ISD::SELECT, MVT::i32, Custom);
659 setOperationAction(ISD::SELECT, MVT::f32, Custom);
660 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
662 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
663 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000664
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
666 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
667 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
668 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
669 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000670
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000671 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 setOperationAction(ISD::FSIN, MVT::f64, Expand);
673 setOperationAction(ISD::FSIN, MVT::f32, Expand);
674 setOperationAction(ISD::FCOS, MVT::f32, Expand);
675 setOperationAction(ISD::FCOS, MVT::f64, Expand);
676 setOperationAction(ISD::FREM, MVT::f64, Expand);
677 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000678 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000679 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
680 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000681 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000682 setOperationAction(ISD::FPOW, MVT::f64, Expand);
683 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000684
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000685 // Various VFP goodness
686 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000687 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
688 if (Subtarget->hasVFP2()) {
689 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
690 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
691 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
692 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
693 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000694 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000695 if (!Subtarget->hasFP16()) {
696 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
697 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000698 }
Evan Cheng110cf482008-04-01 01:50:16 +0000699 }
Evan Chenga8e29892007-01-19 07:51:42 +0000700
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000701 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000702 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000703 setTargetDAGCombine(ISD::ADD);
704 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000705 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000706
Owen Anderson080c0922010-11-05 19:27:46 +0000707 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000708 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000709 if (Subtarget->hasNEON())
710 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000711
Evan Chenga8e29892007-01-19 07:51:42 +0000712 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000713
Evan Chengf7d87ee2010-05-21 00:43:17 +0000714 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
715 setSchedulingPreference(Sched::RegPressure);
716 else
717 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000718
Evan Cheng05219282011-01-06 06:52:41 +0000719 //// temporary - rewrite interface to use type
720 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000721
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000722 // On ARM arguments smaller than 4 bytes are extended, so all arguments
723 // are at least 4 bytes aligned.
724 setMinStackArgumentAlignment(4);
725
Evan Chengfff606d2010-09-24 19:07:23 +0000726 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000727
728 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000729}
730
Andrew Trick32cec0a2011-01-19 02:35:27 +0000731// FIXME: It might make sense to define the representative register class as the
732// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
733// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
734// SPR's representative would be DPR_VFP2. This should work well if register
735// pressure tracking were modified such that a register use would increment the
736// pressure of the register class's representative and all of it's super
737// classes' representatives transitively. We have not implemented this because
738// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000739// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000740// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000741std::pair<const TargetRegisterClass*, uint8_t>
742ARMTargetLowering::findRepresentativeClass(EVT VT) const{
743 const TargetRegisterClass *RRC = 0;
744 uint8_t Cost = 1;
745 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000746 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000747 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000748 // Use DPR as representative register class for all floating point
749 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
750 // the cost is 1 for both f32 and f64.
751 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000752 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000753 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000754 // When NEON is used for SP, only half of the register file is available
755 // because operations that define both SP and DP results will be constrained
756 // to the VFP2 class (D0-D15). We currently model this constraint prior to
757 // coalescing by double-counting the SP regs. See the FIXME above.
758 if (Subtarget->useNEONForSinglePrecisionFP())
759 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000760 break;
761 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
762 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000763 RRC = ARM::DPRRegisterClass;
764 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000765 break;
766 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000767 RRC = ARM::DPRRegisterClass;
768 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000769 break;
770 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000771 RRC = ARM::DPRRegisterClass;
772 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000773 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000774 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000775 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000776}
777
Evan Chenga8e29892007-01-19 07:51:42 +0000778const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
779 switch (Opcode) {
780 default: return 0;
781 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000782 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000783 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000784 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
785 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000786 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000787 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
788 case ARMISD::tCALL: return "ARMISD::tCALL";
789 case ARMISD::BRCOND: return "ARMISD::BRCOND";
790 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000791 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000792 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
793 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
794 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000795 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000796 case ARMISD::CMPFP: return "ARMISD::CMPFP";
797 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000798 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000799 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
800 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000801
Jim Grosbach3482c802010-01-18 19:58:49 +0000802 case ARMISD::RBIT: return "ARMISD::RBIT";
803
Bob Wilson76a312b2010-03-19 22:51:32 +0000804 case ARMISD::FTOSI: return "ARMISD::FTOSI";
805 case ARMISD::FTOUI: return "ARMISD::FTOUI";
806 case ARMISD::SITOF: return "ARMISD::SITOF";
807 case ARMISD::UITOF: return "ARMISD::UITOF";
808
Evan Chenga8e29892007-01-19 07:51:42 +0000809 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
810 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
811 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000812
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000813 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
814 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000815
Evan Chengc5942082009-10-28 06:55:03 +0000816 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
817 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000818 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000819
Dale Johannesen51e28e62010-06-03 21:09:53 +0000820 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000821
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000822 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000823
Evan Cheng86198642009-08-07 00:34:42 +0000824 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
825
Jim Grosbach3728e962009-12-10 00:11:09 +0000826 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000827 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000828
Evan Chengdfed19f2010-11-03 06:34:55 +0000829 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
830
Bob Wilson5bafff32009-06-22 23:27:02 +0000831 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000832 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000833 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000834 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
835 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000836 case ARMISD::VCGEU: return "ARMISD::VCGEU";
837 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000838 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
839 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000840 case ARMISD::VCGTU: return "ARMISD::VCGTU";
841 case ARMISD::VTST: return "ARMISD::VTST";
842
843 case ARMISD::VSHL: return "ARMISD::VSHL";
844 case ARMISD::VSHRs: return "ARMISD::VSHRs";
845 case ARMISD::VSHRu: return "ARMISD::VSHRu";
846 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
847 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
848 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
849 case ARMISD::VSHRN: return "ARMISD::VSHRN";
850 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
851 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
852 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
853 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
854 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
855 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
856 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
857 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
858 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
859 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
860 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
861 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
862 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
863 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000864 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000865 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000866 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000867 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000868 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000869 case ARMISD::VREV64: return "ARMISD::VREV64";
870 case ARMISD::VREV32: return "ARMISD::VREV32";
871 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000872 case ARMISD::VZIP: return "ARMISD::VZIP";
873 case ARMISD::VUZP: return "ARMISD::VUZP";
874 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000875 case ARMISD::VTBL1: return "ARMISD::VTBL1";
876 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000877 case ARMISD::VMULLs: return "ARMISD::VMULLs";
878 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000879 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000880 case ARMISD::FMAX: return "ARMISD::FMAX";
881 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000882 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000883 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
884 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000885 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000886 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
887 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
888 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000889 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
890 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
891 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
892 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
893 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
894 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
895 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
896 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
897 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
898 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
899 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
900 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
901 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
902 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
903 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
904 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
905 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000906 }
907}
908
Evan Cheng06b666c2010-05-15 02:18:07 +0000909/// getRegClassFor - Return the register class that should be used for the
910/// specified value type.
911TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
912 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
913 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
914 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000915 if (Subtarget->hasNEON()) {
916 if (VT == MVT::v4i64)
917 return ARM::QQPRRegisterClass;
918 else if (VT == MVT::v8i64)
919 return ARM::QQQQPRRegisterClass;
920 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000921 return TargetLowering::getRegClassFor(VT);
922}
923
Eric Christopherab695882010-07-21 22:26:11 +0000924// Create a fast isel object.
925FastISel *
926ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
927 return ARM::createFastISel(funcInfo);
928}
929
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000930/// getMaximalGlobalOffset - Returns the maximal possible offset which can
931/// be used for loads / stores from the global.
932unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
933 return (Subtarget->isThumb1Only() ? 127 : 4095);
934}
935
Evan Cheng1cc39842010-05-20 23:26:43 +0000936Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000937 unsigned NumVals = N->getNumValues();
938 if (!NumVals)
939 return Sched::RegPressure;
940
941 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000942 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000943 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000944 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000945 if (VT.isFloatingPoint() || VT.isVector())
946 return Sched::Latency;
947 }
Evan Chengc10f5432010-05-28 23:25:23 +0000948
949 if (!N->isMachineOpcode())
950 return Sched::RegPressure;
951
952 // Load are scheduled for latency even if there instruction itinerary
953 // is not available.
954 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
955 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000956
957 if (TID.getNumDefs() == 0)
958 return Sched::RegPressure;
959 if (!Itins->isEmpty() &&
960 Itins->getOperandCycle(TID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000961 return Sched::Latency;
962
Evan Cheng1cc39842010-05-20 23:26:43 +0000963 return Sched::RegPressure;
964}
965
Evan Chenga8e29892007-01-19 07:51:42 +0000966//===----------------------------------------------------------------------===//
967// Lowering Code
968//===----------------------------------------------------------------------===//
969
Evan Chenga8e29892007-01-19 07:51:42 +0000970/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
971static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
972 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000973 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000974 case ISD::SETNE: return ARMCC::NE;
975 case ISD::SETEQ: return ARMCC::EQ;
976 case ISD::SETGT: return ARMCC::GT;
977 case ISD::SETGE: return ARMCC::GE;
978 case ISD::SETLT: return ARMCC::LT;
979 case ISD::SETLE: return ARMCC::LE;
980 case ISD::SETUGT: return ARMCC::HI;
981 case ISD::SETUGE: return ARMCC::HS;
982 case ISD::SETULT: return ARMCC::LO;
983 case ISD::SETULE: return ARMCC::LS;
984 }
985}
986
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000987/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
988static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000989 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000990 CondCode2 = ARMCC::AL;
991 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000992 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000993 case ISD::SETEQ:
994 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
995 case ISD::SETGT:
996 case ISD::SETOGT: CondCode = ARMCC::GT; break;
997 case ISD::SETGE:
998 case ISD::SETOGE: CondCode = ARMCC::GE; break;
999 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001000 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001001 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1002 case ISD::SETO: CondCode = ARMCC::VC; break;
1003 case ISD::SETUO: CondCode = ARMCC::VS; break;
1004 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1005 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1006 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1007 case ISD::SETLT:
1008 case ISD::SETULT: CondCode = ARMCC::LT; break;
1009 case ISD::SETLE:
1010 case ISD::SETULE: CondCode = ARMCC::LE; break;
1011 case ISD::SETNE:
1012 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1013 }
Evan Chenga8e29892007-01-19 07:51:42 +00001014}
1015
Bob Wilson1f595bb2009-04-17 19:07:39 +00001016//===----------------------------------------------------------------------===//
1017// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001018//===----------------------------------------------------------------------===//
1019
1020#include "ARMGenCallingConv.inc"
1021
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001022/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1023/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001024CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001025 bool Return,
1026 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001027 switch (CC) {
1028 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001029 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001030 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001031 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001032 if (!Subtarget->isAAPCS_ABI())
1033 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1034 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1035 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1036 }
1037 // Fallthrough
1038 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001039 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001040 if (!Subtarget->isAAPCS_ABI())
1041 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1042 else if (Subtarget->hasVFP2() &&
1043 FloatABIType == FloatABI::Hard && !isVarArg)
1044 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1045 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1046 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001047 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001048 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001049 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001050 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001051 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001052 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001053 }
1054}
1055
Dan Gohman98ca4f22009-08-05 01:29:28 +00001056/// LowerCallResult - Lower the result values of a call into the
1057/// appropriate copies out of appropriate physical registers.
1058SDValue
1059ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001060 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001061 const SmallVectorImpl<ISD::InputArg> &Ins,
1062 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001063 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001064
Bob Wilson1f595bb2009-04-17 19:07:39 +00001065 // Assign locations to each value returned by this call.
1066 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001067 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001068 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001069 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001070 CCAssignFnForNode(CallConv, /* Return*/ true,
1071 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001072
1073 // Copy all of the result registers out of their specified physreg.
1074 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1075 CCValAssign VA = RVLocs[i];
1076
Bob Wilson80915242009-04-25 00:33:20 +00001077 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001078 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001079 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001080 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001081 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001082 Chain = Lo.getValue(1);
1083 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001084 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001085 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001086 InFlag);
1087 Chain = Hi.getValue(1);
1088 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001089 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001090
Owen Anderson825b72b2009-08-11 20:47:22 +00001091 if (VA.getLocVT() == MVT::v2f64) {
1092 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1093 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1094 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001095
1096 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001097 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001098 Chain = Lo.getValue(1);
1099 InFlag = Lo.getValue(2);
1100 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001101 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001102 Chain = Hi.getValue(1);
1103 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001104 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001105 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1106 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001107 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001108 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001109 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1110 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001111 Chain = Val.getValue(1);
1112 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001113 }
Bob Wilson80915242009-04-25 00:33:20 +00001114
1115 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001116 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001117 case CCValAssign::Full: break;
1118 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001119 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001120 break;
1121 }
1122
Dan Gohman98ca4f22009-08-05 01:29:28 +00001123 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001124 }
1125
Dan Gohman98ca4f22009-08-05 01:29:28 +00001126 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001127}
1128
Bob Wilsondee46d72009-04-17 20:35:10 +00001129/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001130SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001131ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1132 SDValue StackPtr, SDValue Arg,
1133 DebugLoc dl, SelectionDAG &DAG,
1134 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001135 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001136 unsigned LocMemOffset = VA.getLocMemOffset();
1137 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1138 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001139 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001140 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001141 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001142}
1143
Dan Gohman98ca4f22009-08-05 01:29:28 +00001144void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001145 SDValue Chain, SDValue &Arg,
1146 RegsToPassVector &RegsToPass,
1147 CCValAssign &VA, CCValAssign &NextVA,
1148 SDValue &StackPtr,
1149 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001150 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001151
Jim Grosbache5165492009-11-09 00:11:35 +00001152 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001153 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001154 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1155
1156 if (NextVA.isRegLoc())
1157 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1158 else {
1159 assert(NextVA.isMemLoc());
1160 if (StackPtr.getNode() == 0)
1161 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1162
Dan Gohman98ca4f22009-08-05 01:29:28 +00001163 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1164 dl, DAG, NextVA,
1165 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001166 }
1167}
1168
Dan Gohman98ca4f22009-08-05 01:29:28 +00001169/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001170/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1171/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001172SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001173ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001174 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001175 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001176 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001177 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001178 const SmallVectorImpl<ISD::InputArg> &Ins,
1179 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001180 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001181 MachineFunction &MF = DAG.getMachineFunction();
1182 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1183 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001184 // Temporarily disable tail calls so things don't break.
1185 if (!EnableARMTailCalls)
1186 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001187 if (isTailCall) {
1188 // Check if it's really possible to do a tail call.
1189 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1190 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001191 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001192 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1193 // detected sibcalls.
1194 if (isTailCall) {
1195 ++NumTailCalls;
1196 IsSibCall = true;
1197 }
1198 }
Evan Chenga8e29892007-01-19 07:51:42 +00001199
Bob Wilson1f595bb2009-04-17 19:07:39 +00001200 // Analyze operands of the call, assigning locations to each operand.
1201 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001202 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1203 *DAG.getContext());
Stuart Hastingsc7315872011-04-20 16:47:52 +00001204 CCInfo.setCallOrPrologue(Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001205 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001206 CCAssignFnForNode(CallConv, /* Return*/ false,
1207 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001208
Bob Wilson1f595bb2009-04-17 19:07:39 +00001209 // Get a count of how many bytes are to be pushed on the stack.
1210 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001211
Dale Johannesen51e28e62010-06-03 21:09:53 +00001212 // For tail calls, memory operands are available in our caller's stack.
1213 if (IsSibCall)
1214 NumBytes = 0;
1215
Evan Chenga8e29892007-01-19 07:51:42 +00001216 // Adjust the stack pointer for the new arguments...
1217 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001218 if (!IsSibCall)
1219 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001220
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001221 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001222
Bob Wilson5bafff32009-06-22 23:27:02 +00001223 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001224 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001225
Bob Wilson1f595bb2009-04-17 19:07:39 +00001226 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001227 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001228 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1229 i != e;
1230 ++i, ++realArgIdx) {
1231 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001232 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001233 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001234 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001235
Bob Wilson1f595bb2009-04-17 19:07:39 +00001236 // Promote the value if needed.
1237 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001238 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001239 case CCValAssign::Full: break;
1240 case CCValAssign::SExt:
1241 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1242 break;
1243 case CCValAssign::ZExt:
1244 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1245 break;
1246 case CCValAssign::AExt:
1247 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1248 break;
1249 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001250 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001251 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001252 }
1253
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001254 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001255 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001256 if (VA.getLocVT() == MVT::v2f64) {
1257 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1258 DAG.getConstant(0, MVT::i32));
1259 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1260 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001261
Dan Gohman98ca4f22009-08-05 01:29:28 +00001262 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001263 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1264
1265 VA = ArgLocs[++i]; // skip ahead to next loc
1266 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001267 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001268 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1269 } else {
1270 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001271
Dan Gohman98ca4f22009-08-05 01:29:28 +00001272 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1273 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001274 }
1275 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001276 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001277 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001278 }
1279 } else if (VA.isRegLoc()) {
1280 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001281 } else if (isByVal) {
1282 assert(VA.isMemLoc());
1283 unsigned offset = 0;
1284
1285 // True if this byval aggregate will be split between registers
1286 // and memory.
1287 if (CCInfo.isFirstByValRegValid()) {
1288 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1289 unsigned int i, j;
1290 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1291 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1292 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1293 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1294 MachinePointerInfo(),
1295 false, false, 0);
1296 MemOpChains.push_back(Load.getValue(1));
1297 RegsToPass.push_back(std::make_pair(j, Load));
1298 }
1299 offset = ARM::R4 - CCInfo.getFirstByValReg();
1300 CCInfo.clearFirstByValReg();
1301 }
1302
1303 unsigned LocMemOffset = VA.getLocMemOffset();
1304 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1305 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1306 StkPtrOff);
1307 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1308 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1309 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1310 MVT::i32);
1311 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1312 Flags.getByValAlign(),
1313 /*isVolatile=*/false,
1314 /*AlwaysInline=*/false,
1315 MachinePointerInfo(0),
1316 MachinePointerInfo(0)));
1317
1318 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001319 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001320
Dan Gohman98ca4f22009-08-05 01:29:28 +00001321 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1322 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001323 }
Evan Chenga8e29892007-01-19 07:51:42 +00001324 }
1325
1326 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001327 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001328 &MemOpChains[0], MemOpChains.size());
1329
1330 // Build a sequence of copy-to-reg nodes chained together with token chain
1331 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001332 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001333 // Tail call byval lowering might overwrite argument registers so in case of
1334 // tail call optimization the copies to registers are lowered later.
1335 if (!isTailCall)
1336 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1337 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1338 RegsToPass[i].second, InFlag);
1339 InFlag = Chain.getValue(1);
1340 }
Evan Chenga8e29892007-01-19 07:51:42 +00001341
Dale Johannesen51e28e62010-06-03 21:09:53 +00001342 // For tail calls lower the arguments to the 'real' stack slot.
1343 if (isTailCall) {
1344 // Force all the incoming stack arguments to be loaded from the stack
1345 // before any new outgoing arguments are stored to the stack, because the
1346 // outgoing stack slots may alias the incoming argument stack slots, and
1347 // the alias isn't otherwise explicit. This is slightly more conservative
1348 // than necessary, because it means that each store effectively depends
1349 // on every argument instead of just those arguments it would clobber.
1350
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001351 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001352 InFlag = SDValue();
1353 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1354 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1355 RegsToPass[i].second, InFlag);
1356 InFlag = Chain.getValue(1);
1357 }
1358 InFlag =SDValue();
1359 }
1360
Bill Wendling056292f2008-09-16 21:48:12 +00001361 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1362 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1363 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001364 bool isDirect = false;
1365 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001366 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001367 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001368
1369 if (EnableARMLongCalls) {
1370 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1371 && "long-calls with non-static relocation model!");
1372 // Handle a global address or an external symbol. If it's not one of
1373 // those, the target's already in a register, so we don't need to do
1374 // anything extra.
1375 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001376 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001377 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001378 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001379 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1380 ARMPCLabelIndex,
1381 ARMCP::CPValue, 0);
1382 // Get the address of the callee into a register
1383 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1384 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1385 Callee = DAG.getLoad(getPointerTy(), dl,
1386 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001387 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001388 false, false, 0);
1389 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1390 const char *Sym = S->getSymbol();
1391
1392 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001393 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001394 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1395 Sym, ARMPCLabelIndex, 0);
1396 // Get the address of the callee into a register
1397 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1398 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1399 Callee = DAG.getLoad(getPointerTy(), dl,
1400 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001401 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001402 false, false, 0);
1403 }
1404 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001405 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001406 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001407 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001408 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001409 getTargetMachine().getRelocationModel() != Reloc::Static;
1410 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001411 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001412 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001413 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001414 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001415 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001416 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001417 ARMPCLabelIndex,
1418 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001419 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001420 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001421 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001422 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001423 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001424 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001425 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001426 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001427 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001428 } else {
1429 // On ELF targets for PIC code, direct calls should go through the PLT
1430 unsigned OpFlags = 0;
1431 if (Subtarget->isTargetELF() &&
1432 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1433 OpFlags = ARMII::MO_PLT;
1434 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1435 }
Bill Wendling056292f2008-09-16 21:48:12 +00001436 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001437 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001438 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001439 getTargetMachine().getRelocationModel() != Reloc::Static;
1440 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001441 // tBX takes a register source operand.
1442 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001443 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001444 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001445 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001446 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001447 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001448 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001449 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001450 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001451 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001452 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001453 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001454 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001455 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001456 } else {
1457 unsigned OpFlags = 0;
1458 // On ELF targets for PIC code, direct calls should go through the PLT
1459 if (Subtarget->isTargetELF() &&
1460 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1461 OpFlags = ARMII::MO_PLT;
1462 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1463 }
Evan Chenga8e29892007-01-19 07:51:42 +00001464 }
1465
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001466 // FIXME: handle tail calls differently.
1467 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001468 if (Subtarget->isThumb()) {
1469 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001470 CallOpc = ARMISD::CALL_NOLINK;
1471 else
1472 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1473 } else {
1474 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001475 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1476 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001477 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001478
Dan Gohman475871a2008-07-27 21:46:04 +00001479 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001480 Ops.push_back(Chain);
1481 Ops.push_back(Callee);
1482
1483 // Add argument registers to the end of the list so that they are known live
1484 // into the call.
1485 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1486 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1487 RegsToPass[i].second.getValueType()));
1488
Gabor Greifba36cb52008-08-28 21:40:38 +00001489 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001490 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001491
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001492 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001493 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001494 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001495
Duncan Sands4bdcb612008-07-02 17:40:58 +00001496 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001497 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001498 InFlag = Chain.getValue(1);
1499
Chris Lattnere563bbc2008-10-11 22:08:30 +00001500 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1501 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001502 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001503 InFlag = Chain.getValue(1);
1504
Bob Wilson1f595bb2009-04-17 19:07:39 +00001505 // Handle result values, copying them out of physregs into vregs that we
1506 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001507 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1508 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001509}
1510
Stuart Hastingsf222e592011-02-28 17:17:53 +00001511/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001512/// on the stack. Remember the next parameter register to allocate,
1513/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001514/// this.
1515void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001516llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1517 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1518 assert((State->getCallOrPrologue() == Prologue ||
1519 State->getCallOrPrologue() == Call) &&
1520 "unhandled ParmContext");
1521 if ((!State->isFirstByValRegValid()) &&
1522 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1523 State->setFirstByValReg(reg);
1524 // At a call site, a byval parameter that is split between
1525 // registers and memory needs its size truncated here. In a
1526 // function prologue, such byval parameters are reassembled in
1527 // memory, and are not truncated.
1528 if (State->getCallOrPrologue() == Call) {
1529 unsigned excess = 4 * (ARM::R4 - reg);
1530 assert(size >= excess && "expected larger existing stack allocation");
1531 size -= excess;
1532 }
1533 }
1534 // Confiscate any remaining parameter registers to preclude their
1535 // assignment to subsequent parameters.
1536 while (State->AllocateReg(GPRArgRegs, 4))
1537 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001538}
1539
Dale Johannesen51e28e62010-06-03 21:09:53 +00001540/// MatchingStackOffset - Return true if the given stack call argument is
1541/// already available in the same position (relatively) of the caller's
1542/// incoming argument stack.
1543static
1544bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1545 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1546 const ARMInstrInfo *TII) {
1547 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1548 int FI = INT_MAX;
1549 if (Arg.getOpcode() == ISD::CopyFromReg) {
1550 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001551 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001552 return false;
1553 MachineInstr *Def = MRI->getVRegDef(VR);
1554 if (!Def)
1555 return false;
1556 if (!Flags.isByVal()) {
1557 if (!TII->isLoadFromStackSlot(Def, FI))
1558 return false;
1559 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001560 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001561 }
1562 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1563 if (Flags.isByVal())
1564 // ByVal argument is passed in as a pointer but it's now being
1565 // dereferenced. e.g.
1566 // define @foo(%struct.X* %A) {
1567 // tail call @bar(%struct.X* byval %A)
1568 // }
1569 return false;
1570 SDValue Ptr = Ld->getBasePtr();
1571 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1572 if (!FINode)
1573 return false;
1574 FI = FINode->getIndex();
1575 } else
1576 return false;
1577
1578 assert(FI != INT_MAX);
1579 if (!MFI->isFixedObjectIndex(FI))
1580 return false;
1581 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1582}
1583
1584/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1585/// for tail call optimization. Targets which want to do tail call
1586/// optimization should implement this function.
1587bool
1588ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1589 CallingConv::ID CalleeCC,
1590 bool isVarArg,
1591 bool isCalleeStructRet,
1592 bool isCallerStructRet,
1593 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001594 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001595 const SmallVectorImpl<ISD::InputArg> &Ins,
1596 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001597 const Function *CallerF = DAG.getMachineFunction().getFunction();
1598 CallingConv::ID CallerCC = CallerF->getCallingConv();
1599 bool CCMatch = CallerCC == CalleeCC;
1600
1601 // Look for obvious safe cases to perform tail call optimization that do not
1602 // require ABI changes. This is what gcc calls sibcall.
1603
Jim Grosbach7616b642010-06-16 23:45:49 +00001604 // Do not sibcall optimize vararg calls unless the call site is not passing
1605 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001606 if (isVarArg && !Outs.empty())
1607 return false;
1608
1609 // Also avoid sibcall optimization if either caller or callee uses struct
1610 // return semantics.
1611 if (isCalleeStructRet || isCallerStructRet)
1612 return false;
1613
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001614 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Evan Cheng0110ac62010-06-19 01:01:32 +00001615 // emitEpilogue is not ready for them.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001616 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1617 // LR. This means if we need to reload LR, it takes an extra instructions,
1618 // which outweighs the value of the tail call; but here we don't know yet
1619 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001620 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001621 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001622
1623 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1624 // but we need to make sure there are enough registers; the only valid
1625 // registers are the 4 used for parameters. We don't currently do this
1626 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001627 if (Subtarget->isThumb1Only())
1628 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001629
Dale Johannesen51e28e62010-06-03 21:09:53 +00001630 // If the calling conventions do not match, then we'd better make sure the
1631 // results are returned in the same way as what the caller expects.
1632 if (!CCMatch) {
1633 SmallVector<CCValAssign, 16> RVLocs1;
1634 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
1635 RVLocs1, *DAG.getContext());
1636 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1637
1638 SmallVector<CCValAssign, 16> RVLocs2;
1639 CCState CCInfo2(CallerCC, false, getTargetMachine(),
1640 RVLocs2, *DAG.getContext());
1641 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1642
1643 if (RVLocs1.size() != RVLocs2.size())
1644 return false;
1645 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1646 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1647 return false;
1648 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1649 return false;
1650 if (RVLocs1[i].isRegLoc()) {
1651 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1652 return false;
1653 } else {
1654 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1655 return false;
1656 }
1657 }
1658 }
1659
1660 // If the callee takes no arguments then go on to check the results of the
1661 // call.
1662 if (!Outs.empty()) {
1663 // Check if stack adjustment is needed. For now, do not do this if any
1664 // argument is passed on the stack.
1665 SmallVector<CCValAssign, 16> ArgLocs;
1666 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
1667 ArgLocs, *DAG.getContext());
1668 CCInfo.AnalyzeCallOperands(Outs,
1669 CCAssignFnForNode(CalleeCC, false, isVarArg));
1670 if (CCInfo.getNextStackOffset()) {
1671 MachineFunction &MF = DAG.getMachineFunction();
1672
1673 // Check if the arguments are already laid out in the right way as
1674 // the caller's fixed stack objects.
1675 MachineFrameInfo *MFI = MF.getFrameInfo();
1676 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1677 const ARMInstrInfo *TII =
1678 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001679 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1680 i != e;
1681 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001682 CCValAssign &VA = ArgLocs[i];
1683 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001684 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001685 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001686 if (VA.getLocInfo() == CCValAssign::Indirect)
1687 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001688 if (VA.needsCustom()) {
1689 // f64 and vector types are split into multiple registers or
1690 // register/stack-slot combinations. The types will not match
1691 // the registers; give up on memory f64 refs until we figure
1692 // out what to do about this.
1693 if (!VA.isRegLoc())
1694 return false;
1695 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001696 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001697 if (RegVT == MVT::v2f64) {
1698 if (!ArgLocs[++i].isRegLoc())
1699 return false;
1700 if (!ArgLocs[++i].isRegLoc())
1701 return false;
1702 }
1703 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001704 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1705 MFI, MRI, TII))
1706 return false;
1707 }
1708 }
1709 }
1710 }
1711
1712 return true;
1713}
1714
Dan Gohman98ca4f22009-08-05 01:29:28 +00001715SDValue
1716ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001717 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001718 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001719 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001720 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001721
Bob Wilsondee46d72009-04-17 20:35:10 +00001722 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001723 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001724
Bob Wilsondee46d72009-04-17 20:35:10 +00001725 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001726 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1727 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001728
Dan Gohman98ca4f22009-08-05 01:29:28 +00001729 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001730 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1731 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001732
1733 // If this is the first return lowered for this function, add
1734 // the regs to the liveout set for the function.
1735 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1736 for (unsigned i = 0; i != RVLocs.size(); ++i)
1737 if (RVLocs[i].isRegLoc())
1738 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001739 }
1740
Bob Wilson1f595bb2009-04-17 19:07:39 +00001741 SDValue Flag;
1742
1743 // Copy the result values into the output registers.
1744 for (unsigned i = 0, realRVLocIdx = 0;
1745 i != RVLocs.size();
1746 ++i, ++realRVLocIdx) {
1747 CCValAssign &VA = RVLocs[i];
1748 assert(VA.isRegLoc() && "Can only return in registers!");
1749
Dan Gohmanc9403652010-07-07 15:54:55 +00001750 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001751
1752 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001753 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001754 case CCValAssign::Full: break;
1755 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001756 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001757 break;
1758 }
1759
Bob Wilson1f595bb2009-04-17 19:07:39 +00001760 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001761 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001762 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001763 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1764 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001765 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001766 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001767
1768 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1769 Flag = Chain.getValue(1);
1770 VA = RVLocs[++i]; // skip ahead to next loc
1771 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1772 HalfGPRs.getValue(1), Flag);
1773 Flag = Chain.getValue(1);
1774 VA = RVLocs[++i]; // skip ahead to next loc
1775
1776 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001777 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1778 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001779 }
1780 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1781 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001782 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001783 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001784 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001785 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001786 VA = RVLocs[++i]; // skip ahead to next loc
1787 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1788 Flag);
1789 } else
1790 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1791
Bob Wilsondee46d72009-04-17 20:35:10 +00001792 // Guarantee that all emitted copies are
1793 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001794 Flag = Chain.getValue(1);
1795 }
1796
1797 SDValue result;
1798 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001799 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001800 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001801 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001802
1803 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001804}
1805
Evan Cheng3d2125c2010-11-30 23:55:39 +00001806bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1807 if (N->getNumValues() != 1)
1808 return false;
1809 if (!N->hasNUsesOfValue(1, 0))
1810 return false;
1811
1812 unsigned NumCopies = 0;
1813 SDNode* Copies[2];
1814 SDNode *Use = *N->use_begin();
1815 if (Use->getOpcode() == ISD::CopyToReg) {
1816 Copies[NumCopies++] = Use;
1817 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1818 // f64 returned in a pair of GPRs.
1819 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1820 UI != UE; ++UI) {
1821 if (UI->getOpcode() != ISD::CopyToReg)
1822 return false;
1823 Copies[UI.getUse().getResNo()] = *UI;
1824 ++NumCopies;
1825 }
1826 } else if (Use->getOpcode() == ISD::BITCAST) {
1827 // f32 returned in a single GPR.
1828 if (!Use->hasNUsesOfValue(1, 0))
1829 return false;
1830 Use = *Use->use_begin();
1831 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1832 return false;
1833 Copies[NumCopies++] = Use;
1834 } else {
1835 return false;
1836 }
1837
1838 if (NumCopies != 1 && NumCopies != 2)
1839 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001840
1841 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001842 for (unsigned i = 0; i < NumCopies; ++i) {
1843 SDNode *Copy = Copies[i];
1844 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1845 UI != UE; ++UI) {
1846 if (UI->getOpcode() == ISD::CopyToReg) {
1847 SDNode *Use = *UI;
1848 if (Use == Copies[0] || Use == Copies[1])
1849 continue;
1850 return false;
1851 }
1852 if (UI->getOpcode() != ARMISD::RET_FLAG)
1853 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001854 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001855 }
1856 }
1857
Evan Cheng1bf891a2010-12-01 22:59:46 +00001858 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001859}
1860
Evan Cheng485fafc2011-03-21 01:19:09 +00001861bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1862 if (!EnableARMTailCalls)
1863 return false;
1864
1865 if (!CI->isTailCall())
1866 return false;
1867
1868 return !Subtarget->isThumb1Only();
1869}
1870
Bob Wilsonb62d2572009-11-03 00:02:05 +00001871// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1872// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1873// one of the above mentioned nodes. It has to be wrapped because otherwise
1874// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1875// be used to form addressing mode. These wrapped nodes will be selected
1876// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001877static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001878 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001879 // FIXME there is no actual debug info here
1880 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001881 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001882 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001883 if (CP->isMachineConstantPoolEntry())
1884 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1885 CP->getAlignment());
1886 else
1887 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1888 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001889 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001890}
1891
Jim Grosbache1102ca2010-07-19 17:20:38 +00001892unsigned ARMTargetLowering::getJumpTableEncoding() const {
1893 return MachineJumpTableInfo::EK_Inline;
1894}
1895
Dan Gohmand858e902010-04-17 15:26:15 +00001896SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1897 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001898 MachineFunction &MF = DAG.getMachineFunction();
1899 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1900 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001901 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001902 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001903 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001904 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1905 SDValue CPAddr;
1906 if (RelocM == Reloc::Static) {
1907 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1908 } else {
1909 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001910 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001911 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1912 ARMCP::CPBlockAddress,
1913 PCAdj);
1914 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1915 }
1916 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1917 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001918 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001919 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001920 if (RelocM == Reloc::Static)
1921 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001922 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001923 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001924}
1925
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001926// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001927SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001928ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001929 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001930 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001931 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001932 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001933 MachineFunction &MF = DAG.getMachineFunction();
1934 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001935 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001936 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001937 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001938 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001939 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001940 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001941 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001942 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001943 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001944 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001945
Evan Chenge7e0d622009-11-06 22:24:13 +00001946 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001947 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001948
1949 // call __tls_get_addr.
1950 ArgListTy Args;
1951 ArgListEntry Entry;
1952 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001953 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001954 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001955 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001956 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001957 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1958 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001959 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001960 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001961 return CallResult.first;
1962}
1963
1964// Lower ISD::GlobalTLSAddress using the "initial exec" or
1965// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001966SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001967ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001968 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001969 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001970 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001971 SDValue Offset;
1972 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001973 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001974 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001975 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001976
Chris Lattner4fb63d02009-07-15 04:12:33 +00001977 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001978 MachineFunction &MF = DAG.getMachineFunction();
1979 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001980 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00001981 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001982 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1983 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001984 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001985 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001986 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001987 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001988 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001989 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001990 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001991 Chain = Offset.getValue(1);
1992
Evan Chenge7e0d622009-11-06 22:24:13 +00001993 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001994 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001995
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 } else {
2000 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002001 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002002 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002003 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
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 }
2008
2009 // The address of the thread local variable is the add of the thread
2010 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002011 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002012}
2013
Dan Gohman475871a2008-07-27 21:46:04 +00002014SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002015ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002016 // TODO: implement the "local dynamic" model
2017 assert(Subtarget->isTargetELF() &&
2018 "TLS not implemented for non-ELF targets");
2019 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2020 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2021 // otherwise use the "Local Exec" TLS Model
2022 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2023 return LowerToTLSGeneralDynamicModel(GA, DAG);
2024 else
2025 return LowerToTLSExecModels(GA, DAG);
2026}
2027
Dan Gohman475871a2008-07-27 21:46:04 +00002028SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002029 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002030 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002031 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002032 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002033 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2034 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002035 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002036 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002037 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002038 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002039 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002040 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002041 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002042 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002043 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002044 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002045 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002046 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002047 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002048 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002049 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002050 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002051 }
2052
2053 // If we have T2 ops, we can materialize the address directly via movt/movw
2054 // pair. This is always cheaper.
2055 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002056 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002057 // FIXME: Once remat is capable of dealing with instructions with register
2058 // operands, expand this into two nodes.
2059 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2060 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002061 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002062 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2063 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2064 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2065 MachinePointerInfo::getConstantPool(),
2066 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002067 }
2068}
2069
Dan Gohman475871a2008-07-27 21:46:04 +00002070SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002071 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002072 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002073 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002074 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002075 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002076 MachineFunction &MF = DAG.getMachineFunction();
2077 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2078
2079 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002080 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002081 // FIXME: Once remat is capable of dealing with instructions with register
2082 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002083 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002084 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2085 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2086
Evan Cheng53519f02011-01-21 18:55:51 +00002087 unsigned Wrapper = (RelocM == Reloc::PIC_)
2088 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2089 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002090 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002091 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2092 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2093 MachinePointerInfo::getGOT(), false, false, 0);
2094 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002095 }
2096
2097 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002098 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002099 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002100 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002101 } else {
2102 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002103 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2104 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002105 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002106 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002107 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002108 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002109
Evan Cheng9eda6892009-10-31 03:39:36 +00002110 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002111 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002112 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002113 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002114
2115 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002116 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002117 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002118 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002119
Evan Cheng63476a82009-09-03 07:04:02 +00002120 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002121 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002122 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002123
2124 return Result;
2125}
2126
Dan Gohman475871a2008-07-27 21:46:04 +00002127SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002128 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002129 assert(Subtarget->isTargetELF() &&
2130 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002131 MachineFunction &MF = DAG.getMachineFunction();
2132 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002133 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002134 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002135 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002136 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002137 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2138 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002139 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002140 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002141 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002142 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002143 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002144 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002145 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002146 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002147}
2148
Jim Grosbach0e0da732009-05-12 23:59:14 +00002149SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002150ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2151 const {
2152 DebugLoc dl = Op.getDebugLoc();
2153 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
Bill Wendling61512ba2011-05-11 01:11:55 +00002154 Op.getOperand(0), Op.getOperand(1));
Jim Grosbache4ad3872010-10-19 23:27:08 +00002155}
2156
2157SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002158ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2159 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002160 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002161 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2162 Op.getOperand(1), Val);
2163}
2164
2165SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002166ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2167 DebugLoc dl = Op.getDebugLoc();
2168 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2169 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2170}
2171
2172SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002173ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002174 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002175 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002176 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002177 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002178 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002179 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002180 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002181 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2182 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002183 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002184 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002185 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002186 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002187 EVT PtrVT = getPointerTy();
2188 DebugLoc dl = Op.getDebugLoc();
2189 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2190 SDValue CPAddr;
2191 unsigned PCAdj = (RelocM != Reloc::PIC_)
2192 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002193 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002194 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2195 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002196 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002197 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002198 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002199 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002200 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002201 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002202
2203 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002204 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002205 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2206 }
2207 return Result;
2208 }
Evan Cheng92e39162011-03-29 23:06:19 +00002209 case Intrinsic::arm_neon_vmulls:
2210 case Intrinsic::arm_neon_vmullu: {
2211 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2212 ? ARMISD::VMULLs : ARMISD::VMULLu;
2213 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2214 Op.getOperand(1), Op.getOperand(2));
2215 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002216 }
2217}
2218
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002219static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002220 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002221 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002222 if (!Subtarget->hasDataBarrier()) {
2223 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2224 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2225 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002226 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002227 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002228 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002229 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002230 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002231
2232 SDValue Op5 = Op.getOperand(5);
2233 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2234 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2235 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2236 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2237
2238 ARM_MB::MemBOpt DMBOpt;
2239 if (isDeviceBarrier)
2240 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2241 else
2242 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2243 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2244 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002245}
2246
Evan Chengdfed19f2010-11-03 06:34:55 +00002247static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2248 const ARMSubtarget *Subtarget) {
2249 // ARM pre v5TE and Thumb1 does not have preload instructions.
2250 if (!(Subtarget->isThumb2() ||
2251 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2252 // Just preserve the chain.
2253 return Op.getOperand(0);
2254
2255 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002256 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2257 if (!isRead &&
2258 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2259 // ARMv7 with MP extension has PLDW.
2260 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002261
2262 if (Subtarget->isThumb())
2263 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002264 isRead = ~isRead & 1;
2265 unsigned isData = Subtarget->isThumb() ? 0 : 1;
Evan Chengdfed19f2010-11-03 06:34:55 +00002266
Evan Cheng416941d2010-11-04 05:19:35 +00002267 // Currently there is no intrinsic that matches pli.
Evan Chengdfed19f2010-11-03 06:34:55 +00002268 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002269 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2270 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002271}
2272
Dan Gohman1e93df62010-04-17 14:41:14 +00002273static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2274 MachineFunction &MF = DAG.getMachineFunction();
2275 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2276
Evan Chenga8e29892007-01-19 07:51:42 +00002277 // vastart just stores the address of the VarArgsFrameIndex slot into the
2278 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002279 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002280 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002281 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002282 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002283 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2284 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002285}
2286
Dan Gohman475871a2008-07-27 21:46:04 +00002287SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002288ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2289 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002290 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002291 MachineFunction &MF = DAG.getMachineFunction();
2292 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2293
2294 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002295 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002296 RC = ARM::tGPRRegisterClass;
2297 else
2298 RC = ARM::GPRRegisterClass;
2299
2300 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002301 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002302 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002303
2304 SDValue ArgValue2;
2305 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002306 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002307 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002308
2309 // Create load node to retrieve arguments from the stack.
2310 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002311 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002312 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002313 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002314 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002315 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002316 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002317 }
2318
Jim Grosbache5165492009-11-09 00:11:35 +00002319 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002320}
2321
Stuart Hastingsc7315872011-04-20 16:47:52 +00002322void
2323ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2324 unsigned &VARegSize, unsigned &VARegSaveSize)
2325 const {
2326 unsigned NumGPRs;
2327 if (CCInfo.isFirstByValRegValid())
2328 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2329 else {
2330 unsigned int firstUnalloced;
2331 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2332 sizeof(GPRArgRegs) /
2333 sizeof(GPRArgRegs[0]));
2334 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2335 }
2336
2337 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2338 VARegSize = NumGPRs * 4;
2339 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2340}
2341
2342// The remaining GPRs hold either the beginning of variable-argument
2343// data, or the beginning of an aggregate passed by value (usuall
2344// byval). Either way, we allocate stack slots adjacent to the data
2345// provided by our caller, and store the unallocated registers there.
2346// If this is a variadic function, the va_list pointer will begin with
2347// these values; otherwise, this reassembles a (byval) structure that
2348// was split between registers and memory.
2349void
2350ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2351 DebugLoc dl, SDValue &Chain,
2352 unsigned ArgOffset) const {
2353 MachineFunction &MF = DAG.getMachineFunction();
2354 MachineFrameInfo *MFI = MF.getFrameInfo();
2355 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2356 unsigned firstRegToSaveIndex;
2357 if (CCInfo.isFirstByValRegValid())
2358 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2359 else {
2360 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2361 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2362 }
2363
2364 unsigned VARegSize, VARegSaveSize;
2365 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2366 if (VARegSaveSize) {
2367 // If this function is vararg, store any remaining integer argument regs
2368 // to their spots on the stack so that they may be loaded by deferencing
2369 // the result of va_next.
2370 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002371 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2372 ArgOffset + VARegSaveSize
2373 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002374 false));
2375 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2376 getPointerTy());
2377
2378 SmallVector<SDValue, 4> MemOps;
2379 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2380 TargetRegisterClass *RC;
2381 if (AFI->isThumb1OnlyFunction())
2382 RC = ARM::tGPRRegisterClass;
2383 else
2384 RC = ARM::GPRRegisterClass;
2385
2386 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2387 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2388 SDValue Store =
2389 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002390 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002391 false, false, 0);
2392 MemOps.push_back(Store);
2393 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2394 DAG.getConstant(4, getPointerTy()));
2395 }
2396 if (!MemOps.empty())
2397 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2398 &MemOps[0], MemOps.size());
2399 } else
2400 // This will point to the next argument passed via stack.
2401 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2402}
2403
Bob Wilson5bafff32009-06-22 23:27:02 +00002404SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002405ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002406 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002407 const SmallVectorImpl<ISD::InputArg>
2408 &Ins,
2409 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002410 SmallVectorImpl<SDValue> &InVals)
2411 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002412 MachineFunction &MF = DAG.getMachineFunction();
2413 MachineFrameInfo *MFI = MF.getFrameInfo();
2414
Bob Wilson1f595bb2009-04-17 19:07:39 +00002415 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2416
2417 // Assign locations to all of the incoming arguments.
2418 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002419 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
2420 *DAG.getContext());
Stuart Hastingsc7315872011-04-20 16:47:52 +00002421 CCInfo.setCallOrPrologue(Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002422 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002423 CCAssignFnForNode(CallConv, /* Return*/ false,
2424 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002425
2426 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002427 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002428
Stuart Hastingsf222e592011-02-28 17:17:53 +00002429 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002430 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2431 CCValAssign &VA = ArgLocs[i];
2432
Bob Wilsondee46d72009-04-17 20:35:10 +00002433 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002434 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002435 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002436
Bob Wilson1f595bb2009-04-17 19:07:39 +00002437 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002438 // f64 and vector types are split up into multiple registers or
2439 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002440 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002441 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002442 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002443 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002444 SDValue ArgValue2;
2445 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002446 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002447 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2448 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002449 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002450 false, false, 0);
2451 } else {
2452 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2453 Chain, DAG, dl);
2454 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002455 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2456 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002457 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002458 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002459 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2460 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002461 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002462
Bob Wilson5bafff32009-06-22 23:27:02 +00002463 } else {
2464 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002465
Owen Anderson825b72b2009-08-11 20:47:22 +00002466 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002467 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002468 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002469 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002470 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002471 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002472 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002473 RC = (AFI->isThumb1OnlyFunction() ?
2474 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002475 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002476 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002477
2478 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002479 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002480 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002481 }
2482
2483 // If this is an 8 or 16-bit value, it is really passed promoted
2484 // to 32 bits. Insert an assert[sz]ext to capture this, then
2485 // truncate to the right size.
2486 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002487 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002488 case CCValAssign::Full: break;
2489 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002490 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002491 break;
2492 case CCValAssign::SExt:
2493 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2494 DAG.getValueType(VA.getValVT()));
2495 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2496 break;
2497 case CCValAssign::ZExt:
2498 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2499 DAG.getValueType(VA.getValVT()));
2500 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2501 break;
2502 }
2503
Dan Gohman98ca4f22009-08-05 01:29:28 +00002504 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002505
2506 } else { // VA.isRegLoc()
2507
2508 // sanity check
2509 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002510 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002511
Stuart Hastingsf222e592011-02-28 17:17:53 +00002512 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002513
Stuart Hastingsf222e592011-02-28 17:17:53 +00002514 // Some Ins[] entries become multiple ArgLoc[] entries.
2515 // Process them only once.
2516 if (index != lastInsIndex)
2517 {
2518 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher5ac179c2011-04-29 23:12:01 +00002519 // FIXME: For now, all byval parameter objects are marked mutable.
2520 // This can be changed with more analysis.
2521 // In case of tail call optimization mark all arguments mutable.
2522 // Since they could be overwritten by lowering of arguments in case of
2523 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002524 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002525 unsigned VARegSize, VARegSaveSize;
2526 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2527 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2528 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002529 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002530 int FI = MFI->CreateFixedObject(Bytes,
2531 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002532 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2533 } else {
2534 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2535 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002536
Stuart Hastingsf222e592011-02-28 17:17:53 +00002537 // Create load nodes to retrieve arguments from the stack.
2538 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2539 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2540 MachinePointerInfo::getFixedStack(FI),
2541 false, false, 0));
2542 }
2543 lastInsIndex = index;
2544 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002545 }
2546 }
2547
2548 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002549 if (isVarArg)
2550 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002551
Dan Gohman98ca4f22009-08-05 01:29:28 +00002552 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002553}
2554
2555/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002556static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002557 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002558 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002559 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002560 // Maybe this has already been legalized into the constant pool?
2561 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002562 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002563 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002564 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002565 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002566 }
2567 }
2568 return false;
2569}
2570
Evan Chenga8e29892007-01-19 07:51:42 +00002571/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2572/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002573SDValue
2574ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002575 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002576 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002577 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002578 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002579 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002580 // Constant does not fit, try adjusting it by one?
2581 switch (CC) {
2582 default: break;
2583 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002584 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002585 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002586 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002587 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002588 }
2589 break;
2590 case ISD::SETULT:
2591 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002592 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002593 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002594 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002595 }
2596 break;
2597 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002598 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002599 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002600 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002601 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002602 }
2603 break;
2604 case ISD::SETULE:
2605 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002606 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002607 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002608 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002609 }
2610 break;
2611 }
2612 }
2613 }
2614
2615 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002616 ARMISD::NodeType CompareType;
2617 switch (CondCode) {
2618 default:
2619 CompareType = ARMISD::CMP;
2620 break;
2621 case ARMCC::EQ:
2622 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002623 // Uses only Z Flag
2624 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002625 break;
2626 }
Evan Cheng218977b2010-07-13 19:27:42 +00002627 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002628 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002629}
2630
2631/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002632SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002633ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002634 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002635 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002636 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002637 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002638 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002639 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2640 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002641}
2642
Bob Wilson79f56c92011-03-08 01:17:20 +00002643/// duplicateCmp - Glue values can have only one use, so this function
2644/// duplicates a comparison node.
2645SDValue
2646ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2647 unsigned Opc = Cmp.getOpcode();
2648 DebugLoc DL = Cmp.getDebugLoc();
2649 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2650 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2651
2652 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2653 Cmp = Cmp.getOperand(0);
2654 Opc = Cmp.getOpcode();
2655 if (Opc == ARMISD::CMPFP)
2656 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2657 else {
2658 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2659 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2660 }
2661 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2662}
2663
Bill Wendlingde2b1512010-08-11 08:43:16 +00002664SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2665 SDValue Cond = Op.getOperand(0);
2666 SDValue SelectTrue = Op.getOperand(1);
2667 SDValue SelectFalse = Op.getOperand(2);
2668 DebugLoc dl = Op.getDebugLoc();
2669
2670 // Convert:
2671 //
2672 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2673 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2674 //
2675 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2676 const ConstantSDNode *CMOVTrue =
2677 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2678 const ConstantSDNode *CMOVFalse =
2679 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2680
2681 if (CMOVTrue && CMOVFalse) {
2682 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2683 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2684
2685 SDValue True;
2686 SDValue False;
2687 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2688 True = SelectTrue;
2689 False = SelectFalse;
2690 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2691 True = SelectFalse;
2692 False = SelectTrue;
2693 }
2694
2695 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002696 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002697 SDValue ARMcc = Cond.getOperand(2);
2698 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002699 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002700 assert(True.getValueType() == VT);
2701 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002702 }
2703 }
2704 }
2705
2706 return DAG.getSelectCC(dl, Cond,
2707 DAG.getConstant(0, Cond.getValueType()),
2708 SelectTrue, SelectFalse, ISD::SETNE);
2709}
2710
Dan Gohmand858e902010-04-17 15:26:15 +00002711SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002712 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002713 SDValue LHS = Op.getOperand(0);
2714 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002715 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002716 SDValue TrueVal = Op.getOperand(2);
2717 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002718 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002719
Owen Anderson825b72b2009-08-11 20:47:22 +00002720 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002721 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002722 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002723 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2724 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002725 }
2726
2727 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002728 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002729
Evan Cheng218977b2010-07-13 19:27:42 +00002730 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2731 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002732 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002733 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002734 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002735 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002736 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002737 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002738 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002739 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002740 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002741 }
2742 return Result;
2743}
2744
Evan Cheng218977b2010-07-13 19:27:42 +00002745/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2746/// to morph to an integer compare sequence.
2747static bool canChangeToInt(SDValue Op, bool &SeenZero,
2748 const ARMSubtarget *Subtarget) {
2749 SDNode *N = Op.getNode();
2750 if (!N->hasOneUse())
2751 // Otherwise it requires moving the value from fp to integer registers.
2752 return false;
2753 if (!N->getNumValues())
2754 return false;
2755 EVT VT = Op.getValueType();
2756 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2757 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2758 // vmrs are very slow, e.g. cortex-a8.
2759 return false;
2760
2761 if (isFloatingPointZero(Op)) {
2762 SeenZero = true;
2763 return true;
2764 }
2765 return ISD::isNormalLoad(N);
2766}
2767
2768static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2769 if (isFloatingPointZero(Op))
2770 return DAG.getConstant(0, MVT::i32);
2771
2772 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2773 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002774 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002775 Ld->isVolatile(), Ld->isNonTemporal(),
2776 Ld->getAlignment());
2777
2778 llvm_unreachable("Unknown VFP cmp argument!");
2779}
2780
2781static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2782 SDValue &RetVal1, SDValue &RetVal2) {
2783 if (isFloatingPointZero(Op)) {
2784 RetVal1 = DAG.getConstant(0, MVT::i32);
2785 RetVal2 = DAG.getConstant(0, MVT::i32);
2786 return;
2787 }
2788
2789 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2790 SDValue Ptr = Ld->getBasePtr();
2791 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2792 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002793 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002794 Ld->isVolatile(), Ld->isNonTemporal(),
2795 Ld->getAlignment());
2796
2797 EVT PtrType = Ptr.getValueType();
2798 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2799 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2800 PtrType, Ptr, DAG.getConstant(4, PtrType));
2801 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2802 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002803 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002804 Ld->isVolatile(), Ld->isNonTemporal(),
2805 NewAlign);
2806 return;
2807 }
2808
2809 llvm_unreachable("Unknown VFP cmp argument!");
2810}
2811
2812/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2813/// f32 and even f64 comparisons to integer ones.
2814SDValue
2815ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2816 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002817 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002818 SDValue LHS = Op.getOperand(2);
2819 SDValue RHS = Op.getOperand(3);
2820 SDValue Dest = Op.getOperand(4);
2821 DebugLoc dl = Op.getDebugLoc();
2822
2823 bool SeenZero = false;
2824 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2825 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002826 // If one of the operand is zero, it's safe to ignore the NaN case since
2827 // we only care about equality comparisons.
2828 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002829 // If unsafe fp math optimization is enabled and there are no other uses of
2830 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002831 // to an integer comparison.
2832 if (CC == ISD::SETOEQ)
2833 CC = ISD::SETEQ;
2834 else if (CC == ISD::SETUNE)
2835 CC = ISD::SETNE;
2836
2837 SDValue ARMcc;
2838 if (LHS.getValueType() == MVT::f32) {
2839 LHS = bitcastf32Toi32(LHS, DAG);
2840 RHS = bitcastf32Toi32(RHS, DAG);
2841 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2842 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2843 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2844 Chain, Dest, ARMcc, CCR, Cmp);
2845 }
2846
2847 SDValue LHS1, LHS2;
2848 SDValue RHS1, RHS2;
2849 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2850 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2851 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2852 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002853 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002854 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2855 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2856 }
2857
2858 return SDValue();
2859}
2860
2861SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2862 SDValue Chain = Op.getOperand(0);
2863 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2864 SDValue LHS = Op.getOperand(2);
2865 SDValue RHS = Op.getOperand(3);
2866 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002867 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002868
Owen Anderson825b72b2009-08-11 20:47:22 +00002869 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002870 SDValue ARMcc;
2871 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002872 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002873 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002874 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002875 }
2876
Owen Anderson825b72b2009-08-11 20:47:22 +00002877 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002878
2879 if (UnsafeFPMath &&
2880 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2881 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2882 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2883 if (Result.getNode())
2884 return Result;
2885 }
2886
Evan Chenga8e29892007-01-19 07:51:42 +00002887 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002888 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002889
Evan Cheng218977b2010-07-13 19:27:42 +00002890 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2891 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002892 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002893 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002894 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002895 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002896 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002897 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2898 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002899 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002900 }
2901 return Res;
2902}
2903
Dan Gohmand858e902010-04-17 15:26:15 +00002904SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002905 SDValue Chain = Op.getOperand(0);
2906 SDValue Table = Op.getOperand(1);
2907 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002908 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002909
Owen Andersone50ed302009-08-10 22:56:29 +00002910 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002911 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2912 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002913 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002914 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002915 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002916 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2917 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002918 if (Subtarget->isThumb2()) {
2919 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2920 // which does another jump to the destination. This also makes it easier
2921 // to translate it to TBB / TBH later.
2922 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002923 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002924 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002925 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002926 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002927 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002928 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002929 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002930 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002931 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002932 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002933 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002934 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002935 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002936 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002937 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002938 }
Evan Chenga8e29892007-01-19 07:51:42 +00002939}
2940
Bob Wilson76a312b2010-03-19 22:51:32 +00002941static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2942 DebugLoc dl = Op.getDebugLoc();
2943 unsigned Opc;
2944
2945 switch (Op.getOpcode()) {
2946 default:
2947 assert(0 && "Invalid opcode!");
2948 case ISD::FP_TO_SINT:
2949 Opc = ARMISD::FTOSI;
2950 break;
2951 case ISD::FP_TO_UINT:
2952 Opc = ARMISD::FTOUI;
2953 break;
2954 }
2955 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002956 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00002957}
2958
Cameron Zwarich3007d332011-03-29 21:41:55 +00002959static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2960 EVT VT = Op.getValueType();
2961 DebugLoc dl = Op.getDebugLoc();
2962
2963 EVT OperandVT = Op.getOperand(0).getValueType();
2964 assert(OperandVT == MVT::v4i16 && "Invalid type for custom lowering!");
2965 if (VT != MVT::v4f32)
2966 return DAG.UnrollVectorOp(Op.getNode());
2967
2968 unsigned CastOpc;
2969 unsigned Opc;
2970 switch (Op.getOpcode()) {
2971 default:
2972 assert(0 && "Invalid opcode!");
2973 case ISD::SINT_TO_FP:
2974 CastOpc = ISD::SIGN_EXTEND;
2975 Opc = ISD::SINT_TO_FP;
2976 break;
2977 case ISD::UINT_TO_FP:
2978 CastOpc = ISD::ZERO_EXTEND;
2979 Opc = ISD::UINT_TO_FP;
2980 break;
2981 }
2982
2983 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
2984 return DAG.getNode(Opc, dl, VT, Op);
2985}
2986
Bob Wilson76a312b2010-03-19 22:51:32 +00002987static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2988 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00002989 if (VT.isVector())
2990 return LowerVectorINT_TO_FP(Op, DAG);
2991
Bob Wilson76a312b2010-03-19 22:51:32 +00002992 DebugLoc dl = Op.getDebugLoc();
2993 unsigned Opc;
2994
2995 switch (Op.getOpcode()) {
2996 default:
2997 assert(0 && "Invalid opcode!");
2998 case ISD::SINT_TO_FP:
2999 Opc = ARMISD::SITOF;
3000 break;
3001 case ISD::UINT_TO_FP:
3002 Opc = ARMISD::UITOF;
3003 break;
3004 }
3005
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003006 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003007 return DAG.getNode(Opc, dl, VT, Op);
3008}
3009
Evan Cheng515fe3a2010-07-08 02:08:50 +00003010SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003011 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003012 SDValue Tmp0 = Op.getOperand(0);
3013 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003014 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003015 EVT VT = Op.getValueType();
3016 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003017 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3018 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3019 bool UseNEON = !InGPR && Subtarget->hasNEON();
3020
3021 if (UseNEON) {
3022 // Use VBSL to copy the sign bit.
3023 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3024 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3025 DAG.getTargetConstant(EncodedVal, MVT::i32));
3026 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3027 if (VT == MVT::f64)
3028 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3029 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3030 DAG.getConstant(32, MVT::i32));
3031 else /*if (VT == MVT::f32)*/
3032 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3033 if (SrcVT == MVT::f32) {
3034 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3035 if (VT == MVT::f64)
3036 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3037 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3038 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003039 } else if (VT == MVT::f32)
3040 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3041 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3042 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003043 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3044 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3045
3046 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3047 MVT::i32);
3048 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3049 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3050 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003051
Evan Chenge573fb32011-02-23 02:24:55 +00003052 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3053 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3054 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003055 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003056 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3057 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3058 DAG.getConstant(0, MVT::i32));
3059 } else {
3060 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3061 }
3062
3063 return Res;
3064 }
Evan Chengc143dd42011-02-11 02:28:55 +00003065
3066 // Bitcast operand 1 to i32.
3067 if (SrcVT == MVT::f64)
3068 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3069 &Tmp1, 1).getValue(1);
3070 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3071
Evan Chenge573fb32011-02-23 02:24:55 +00003072 // Or in the signbit with integer operations.
3073 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3074 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3075 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3076 if (VT == MVT::f32) {
3077 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3078 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3079 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3080 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003081 }
3082
Evan Chenge573fb32011-02-23 02:24:55 +00003083 // f64: Or the high part with signbit and then combine two parts.
3084 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3085 &Tmp0, 1);
3086 SDValue Lo = Tmp0.getValue(0);
3087 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3088 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3089 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003090}
3091
Evan Cheng2457f2c2010-05-22 01:47:14 +00003092SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3093 MachineFunction &MF = DAG.getMachineFunction();
3094 MachineFrameInfo *MFI = MF.getFrameInfo();
3095 MFI->setReturnAddressIsTaken(true);
3096
3097 EVT VT = Op.getValueType();
3098 DebugLoc dl = Op.getDebugLoc();
3099 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3100 if (Depth) {
3101 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3102 SDValue Offset = DAG.getConstant(4, MVT::i32);
3103 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3104 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003105 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003106 }
3107
3108 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003109 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003110 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3111}
3112
Dan Gohmand858e902010-04-17 15:26:15 +00003113SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003114 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3115 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003116
Owen Andersone50ed302009-08-10 22:56:29 +00003117 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003118 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3119 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003120 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003121 ? ARM::R7 : ARM::R11;
3122 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3123 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003124 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3125 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00003126 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003127 return FrameAddr;
3128}
3129
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003130/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003131/// expand a bit convert where either the source or destination type is i64 to
3132/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3133/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3134/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003135static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003136 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3137 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003138 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003139
Bob Wilson9f3f0612010-04-17 05:30:19 +00003140 // This function is only supposed to be called for i64 types, either as the
3141 // source or destination of the bit convert.
3142 EVT SrcVT = Op.getValueType();
3143 EVT DstVT = N->getValueType(0);
3144 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003145 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003146
Bob Wilson9f3f0612010-04-17 05:30:19 +00003147 // Turn i64->f64 into VMOVDRR.
3148 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003149 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3150 DAG.getConstant(0, MVT::i32));
3151 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3152 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003153 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003154 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003155 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003156
Jim Grosbache5165492009-11-09 00:11:35 +00003157 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003158 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3159 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3160 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3161 // Merge the pieces into a single i64 value.
3162 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3163 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003164
Bob Wilson9f3f0612010-04-17 05:30:19 +00003165 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003166}
3167
Bob Wilson5bafff32009-06-22 23:27:02 +00003168/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003169/// Zero vectors are used to represent vector negation and in those cases
3170/// will be implemented with the NEON VNEG instruction. However, VNEG does
3171/// not support i64 elements, so sometimes the zero vectors will need to be
3172/// explicitly constructed. Regardless, use a canonical VMOV to create the
3173/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003174static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003175 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003176 // The canonical modified immediate encoding of a zero vector is....0!
3177 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3178 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3179 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003180 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003181}
3182
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003183/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3184/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003185SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3186 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003187 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3188 EVT VT = Op.getValueType();
3189 unsigned VTBits = VT.getSizeInBits();
3190 DebugLoc dl = Op.getDebugLoc();
3191 SDValue ShOpLo = Op.getOperand(0);
3192 SDValue ShOpHi = Op.getOperand(1);
3193 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003194 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003195 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003196
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003197 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3198
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003199 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3200 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3201 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3202 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3203 DAG.getConstant(VTBits, MVT::i32));
3204 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3205 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003206 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003207
3208 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3209 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003210 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003211 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003212 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003213 CCR, Cmp);
3214
3215 SDValue Ops[2] = { Lo, Hi };
3216 return DAG.getMergeValues(Ops, 2, dl);
3217}
3218
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003219/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3220/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003221SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3222 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003223 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3224 EVT VT = Op.getValueType();
3225 unsigned VTBits = VT.getSizeInBits();
3226 DebugLoc dl = Op.getDebugLoc();
3227 SDValue ShOpLo = Op.getOperand(0);
3228 SDValue ShOpHi = Op.getOperand(1);
3229 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003230 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003231
3232 assert(Op.getOpcode() == ISD::SHL_PARTS);
3233 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3234 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3235 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3236 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3237 DAG.getConstant(VTBits, MVT::i32));
3238 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3239 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3240
3241 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3242 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3243 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003244 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003245 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003246 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003247 CCR, Cmp);
3248
3249 SDValue Ops[2] = { Lo, Hi };
3250 return DAG.getMergeValues(Ops, 2, dl);
3251}
3252
Jim Grosbach4725ca72010-09-08 03:54:02 +00003253SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003254 SelectionDAG &DAG) const {
3255 // The rounding mode is in bits 23:22 of the FPSCR.
3256 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3257 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3258 // so that the shift + and get folded into a bitfield extract.
3259 DebugLoc dl = Op.getDebugLoc();
3260 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3261 DAG.getConstant(Intrinsic::arm_get_fpscr,
3262 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003263 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003264 DAG.getConstant(1U << 22, MVT::i32));
3265 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3266 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003267 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003268 DAG.getConstant(3, MVT::i32));
3269}
3270
Jim Grosbach3482c802010-01-18 19:58:49 +00003271static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3272 const ARMSubtarget *ST) {
3273 EVT VT = N->getValueType(0);
3274 DebugLoc dl = N->getDebugLoc();
3275
3276 if (!ST->hasV6T2Ops())
3277 return SDValue();
3278
3279 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3280 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3281}
3282
Bob Wilson5bafff32009-06-22 23:27:02 +00003283static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3284 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003285 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003286 DebugLoc dl = N->getDebugLoc();
3287
Bob Wilsond5448bb2010-11-18 21:16:28 +00003288 if (!VT.isVector())
3289 return SDValue();
3290
Bob Wilson5bafff32009-06-22 23:27:02 +00003291 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003292 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003293
Bob Wilsond5448bb2010-11-18 21:16:28 +00003294 // Left shifts translate directly to the vshiftu intrinsic.
3295 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003296 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003297 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3298 N->getOperand(0), N->getOperand(1));
3299
3300 assert((N->getOpcode() == ISD::SRA ||
3301 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3302
3303 // NEON uses the same intrinsics for both left and right shifts. For
3304 // right shifts, the shift amounts are negative, so negate the vector of
3305 // shift amounts.
3306 EVT ShiftVT = N->getOperand(1).getValueType();
3307 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3308 getZeroVector(ShiftVT, DAG, dl),
3309 N->getOperand(1));
3310 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3311 Intrinsic::arm_neon_vshifts :
3312 Intrinsic::arm_neon_vshiftu);
3313 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3314 DAG.getConstant(vshiftInt, MVT::i32),
3315 N->getOperand(0), NegatedCount);
3316}
3317
3318static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3319 const ARMSubtarget *ST) {
3320 EVT VT = N->getValueType(0);
3321 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003322
Eli Friedmance392eb2009-08-22 03:13:10 +00003323 // We can get here for a node like i32 = ISD::SHL i32, i64
3324 if (VT != MVT::i64)
3325 return SDValue();
3326
3327 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003328 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003329
Chris Lattner27a6c732007-11-24 07:07:01 +00003330 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3331 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003332 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003333 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003334
Chris Lattner27a6c732007-11-24 07:07:01 +00003335 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003336 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003337
Chris Lattner27a6c732007-11-24 07:07:01 +00003338 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003339 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003340 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003341 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003342 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003343
Chris Lattner27a6c732007-11-24 07:07:01 +00003344 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3345 // captures the result into a carry flag.
3346 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003347 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003348
Chris Lattner27a6c732007-11-24 07:07:01 +00003349 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003350 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003351
Chris Lattner27a6c732007-11-24 07:07:01 +00003352 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003353 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003354}
3355
Bob Wilson5bafff32009-06-22 23:27:02 +00003356static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3357 SDValue TmpOp0, TmpOp1;
3358 bool Invert = false;
3359 bool Swap = false;
3360 unsigned Opc = 0;
3361
3362 SDValue Op0 = Op.getOperand(0);
3363 SDValue Op1 = Op.getOperand(1);
3364 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003365 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003366 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3367 DebugLoc dl = Op.getDebugLoc();
3368
3369 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3370 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003371 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003372 case ISD::SETUNE:
3373 case ISD::SETNE: Invert = true; // Fallthrough
3374 case ISD::SETOEQ:
3375 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3376 case ISD::SETOLT:
3377 case ISD::SETLT: Swap = true; // Fallthrough
3378 case ISD::SETOGT:
3379 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3380 case ISD::SETOLE:
3381 case ISD::SETLE: Swap = true; // Fallthrough
3382 case ISD::SETOGE:
3383 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3384 case ISD::SETUGE: Swap = true; // Fallthrough
3385 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3386 case ISD::SETUGT: Swap = true; // Fallthrough
3387 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3388 case ISD::SETUEQ: Invert = true; // Fallthrough
3389 case ISD::SETONE:
3390 // Expand this to (OLT | OGT).
3391 TmpOp0 = Op0;
3392 TmpOp1 = Op1;
3393 Opc = ISD::OR;
3394 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3395 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3396 break;
3397 case ISD::SETUO: Invert = true; // Fallthrough
3398 case ISD::SETO:
3399 // Expand this to (OLT | OGE).
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::VCGE, dl, VT, TmpOp0, TmpOp1);
3405 break;
3406 }
3407 } else {
3408 // Integer comparisons.
3409 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003410 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003411 case ISD::SETNE: Invert = true;
3412 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3413 case ISD::SETLT: Swap = true;
3414 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3415 case ISD::SETLE: Swap = true;
3416 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3417 case ISD::SETULT: Swap = true;
3418 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3419 case ISD::SETULE: Swap = true;
3420 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3421 }
3422
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003423 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003424 if (Opc == ARMISD::VCEQ) {
3425
3426 SDValue AndOp;
3427 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3428 AndOp = Op0;
3429 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3430 AndOp = Op1;
3431
3432 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003433 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003434 AndOp = AndOp.getOperand(0);
3435
3436 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3437 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003438 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3439 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003440 Invert = !Invert;
3441 }
3442 }
3443 }
3444
3445 if (Swap)
3446 std::swap(Op0, Op1);
3447
Owen Andersonc24cb352010-11-08 23:21:22 +00003448 // If one of the operands is a constant vector zero, attempt to fold the
3449 // comparison to a specialized compare-against-zero form.
3450 SDValue SingleOp;
3451 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3452 SingleOp = Op0;
3453 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3454 if (Opc == ARMISD::VCGE)
3455 Opc = ARMISD::VCLEZ;
3456 else if (Opc == ARMISD::VCGT)
3457 Opc = ARMISD::VCLTZ;
3458 SingleOp = Op1;
3459 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003460
Owen Andersonc24cb352010-11-08 23:21:22 +00003461 SDValue Result;
3462 if (SingleOp.getNode()) {
3463 switch (Opc) {
3464 case ARMISD::VCEQ:
3465 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3466 case ARMISD::VCGE:
3467 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3468 case ARMISD::VCLEZ:
3469 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3470 case ARMISD::VCGT:
3471 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3472 case ARMISD::VCLTZ:
3473 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3474 default:
3475 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3476 }
3477 } else {
3478 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3479 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003480
3481 if (Invert)
3482 Result = DAG.getNOT(dl, Result, VT);
3483
3484 return Result;
3485}
3486
Bob Wilsond3c42842010-06-14 22:19:57 +00003487/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3488/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003489/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003490static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3491 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003492 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003493 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003494
Bob Wilson827b2102010-06-15 19:05:35 +00003495 // SplatBitSize is set to the smallest size that splats the vector, so a
3496 // zero vector will always have SplatBitSize == 8. However, NEON modified
3497 // immediate instructions others than VMOV do not support the 8-bit encoding
3498 // of a zero vector, and the default encoding of zero is supposed to be the
3499 // 32-bit version.
3500 if (SplatBits == 0)
3501 SplatBitSize = 32;
3502
Bob Wilson5bafff32009-06-22 23:27:02 +00003503 switch (SplatBitSize) {
3504 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003505 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003506 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003507 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003508 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003509 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003510 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003511 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003512 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003513
3514 case 16:
3515 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003516 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003517 if ((SplatBits & ~0xff) == 0) {
3518 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003519 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003520 Imm = SplatBits;
3521 break;
3522 }
3523 if ((SplatBits & ~0xff00) == 0) {
3524 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003525 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003526 Imm = SplatBits >> 8;
3527 break;
3528 }
3529 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003530
3531 case 32:
3532 // NEON's 32-bit VMOV supports splat values where:
3533 // * only one byte is nonzero, or
3534 // * the least significant byte is 0xff and the second byte is nonzero, or
3535 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003536 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003537 if ((SplatBits & ~0xff) == 0) {
3538 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003539 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003540 Imm = SplatBits;
3541 break;
3542 }
3543 if ((SplatBits & ~0xff00) == 0) {
3544 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003545 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003546 Imm = SplatBits >> 8;
3547 break;
3548 }
3549 if ((SplatBits & ~0xff0000) == 0) {
3550 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003551 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003552 Imm = SplatBits >> 16;
3553 break;
3554 }
3555 if ((SplatBits & ~0xff000000) == 0) {
3556 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003557 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003558 Imm = SplatBits >> 24;
3559 break;
3560 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003561
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003562 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3563 if (type == OtherModImm) return SDValue();
3564
Bob Wilson5bafff32009-06-22 23:27:02 +00003565 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003566 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3567 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003568 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003569 Imm = SplatBits >> 8;
3570 SplatBits |= 0xff;
3571 break;
3572 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003573
3574 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003575 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3576 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003577 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003578 Imm = SplatBits >> 16;
3579 SplatBits |= 0xffff;
3580 break;
3581 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003582
3583 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3584 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3585 // VMOV.I32. A (very) minor optimization would be to replicate the value
3586 // and fall through here to test for a valid 64-bit splat. But, then the
3587 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003588 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003589
3590 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003591 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003592 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003593 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003594 uint64_t BitMask = 0xff;
3595 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003596 unsigned ImmMask = 1;
3597 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003598 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003599 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003600 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003601 Imm |= ImmMask;
3602 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003603 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003604 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003605 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003606 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003607 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003608 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003609 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003610 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003611 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003612 break;
3613 }
3614
Bob Wilson1a913ed2010-06-11 21:34:50 +00003615 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003616 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003617 return SDValue();
3618 }
3619
Bob Wilsoncba270d2010-07-13 21:16:48 +00003620 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3621 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003622}
3623
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003624static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3625 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003626 unsigned NumElts = VT.getVectorNumElements();
3627 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003628
3629 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3630 if (M[0] < 0)
3631 return false;
3632
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003633 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003634
3635 // If this is a VEXT shuffle, the immediate value is the index of the first
3636 // element. The other shuffle indices must be the successive elements after
3637 // the first one.
3638 unsigned ExpectedElt = Imm;
3639 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003640 // Increment the expected index. If it wraps around, it may still be
3641 // a VEXT but the source vectors must be swapped.
3642 ExpectedElt += 1;
3643 if (ExpectedElt == NumElts * 2) {
3644 ExpectedElt = 0;
3645 ReverseVEXT = true;
3646 }
3647
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003648 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003649 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003650 return false;
3651 }
3652
3653 // Adjust the index value if the source operands will be swapped.
3654 if (ReverseVEXT)
3655 Imm -= NumElts;
3656
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003657 return true;
3658}
3659
Bob Wilson8bb9e482009-07-26 00:39:34 +00003660/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3661/// instruction with the specified blocksize. (The order of the elements
3662/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003663static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3664 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003665 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3666 "Only possible block sizes for VREV are: 16, 32, 64");
3667
Bob Wilson8bb9e482009-07-26 00:39:34 +00003668 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003669 if (EltSz == 64)
3670 return false;
3671
3672 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003673 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003674 // If the first shuffle index is UNDEF, be optimistic.
3675 if (M[0] < 0)
3676 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003677
3678 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3679 return false;
3680
3681 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003682 if (M[i] < 0) continue; // ignore UNDEF indices
3683 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003684 return false;
3685 }
3686
3687 return true;
3688}
3689
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003690static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3691 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3692 // range, then 0 is placed into the resulting vector. So pretty much any mask
3693 // of 8 elements can work here.
3694 return VT == MVT::v8i8 && M.size() == 8;
3695}
3696
Bob Wilsonc692cb72009-08-21 20:54:19 +00003697static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3698 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003699 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3700 if (EltSz == 64)
3701 return false;
3702
Bob Wilsonc692cb72009-08-21 20:54:19 +00003703 unsigned NumElts = VT.getVectorNumElements();
3704 WhichResult = (M[0] == 0 ? 0 : 1);
3705 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003706 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3707 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003708 return false;
3709 }
3710 return true;
3711}
3712
Bob Wilson324f4f12009-12-03 06:40:55 +00003713/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3714/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3715/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3716static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3717 unsigned &WhichResult) {
3718 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3719 if (EltSz == 64)
3720 return false;
3721
3722 unsigned NumElts = VT.getVectorNumElements();
3723 WhichResult = (M[0] == 0 ? 0 : 1);
3724 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003725 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3726 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003727 return false;
3728 }
3729 return true;
3730}
3731
Bob Wilsonc692cb72009-08-21 20:54:19 +00003732static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3733 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003734 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3735 if (EltSz == 64)
3736 return false;
3737
Bob Wilsonc692cb72009-08-21 20:54:19 +00003738 unsigned NumElts = VT.getVectorNumElements();
3739 WhichResult = (M[0] == 0 ? 0 : 1);
3740 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003741 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003742 if ((unsigned) M[i] != 2 * i + WhichResult)
3743 return false;
3744 }
3745
3746 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003747 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003748 return false;
3749
3750 return true;
3751}
3752
Bob Wilson324f4f12009-12-03 06:40:55 +00003753/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3754/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3755/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3756static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3757 unsigned &WhichResult) {
3758 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3759 if (EltSz == 64)
3760 return false;
3761
3762 unsigned Half = VT.getVectorNumElements() / 2;
3763 WhichResult = (M[0] == 0 ? 0 : 1);
3764 for (unsigned j = 0; j != 2; ++j) {
3765 unsigned Idx = WhichResult;
3766 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003767 int MIdx = M[i + j * Half];
3768 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003769 return false;
3770 Idx += 2;
3771 }
3772 }
3773
3774 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3775 if (VT.is64BitVector() && EltSz == 32)
3776 return false;
3777
3778 return true;
3779}
3780
Bob Wilsonc692cb72009-08-21 20:54:19 +00003781static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3782 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003783 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3784 if (EltSz == 64)
3785 return false;
3786
Bob Wilsonc692cb72009-08-21 20:54:19 +00003787 unsigned NumElts = VT.getVectorNumElements();
3788 WhichResult = (M[0] == 0 ? 0 : 1);
3789 unsigned Idx = WhichResult * NumElts / 2;
3790 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003791 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3792 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003793 return false;
3794 Idx += 1;
3795 }
3796
3797 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003798 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003799 return false;
3800
3801 return true;
3802}
3803
Bob Wilson324f4f12009-12-03 06:40:55 +00003804/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3805/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3806/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3807static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3808 unsigned &WhichResult) {
3809 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3810 if (EltSz == 64)
3811 return false;
3812
3813 unsigned NumElts = VT.getVectorNumElements();
3814 WhichResult = (M[0] == 0 ? 0 : 1);
3815 unsigned Idx = WhichResult * NumElts / 2;
3816 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003817 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3818 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003819 return false;
3820 Idx += 1;
3821 }
3822
3823 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3824 if (VT.is64BitVector() && EltSz == 32)
3825 return false;
3826
3827 return true;
3828}
3829
Dale Johannesenf630c712010-07-29 20:10:08 +00003830// If N is an integer constant that can be moved into a register in one
3831// instruction, return an SDValue of such a constant (will become a MOV
3832// instruction). Otherwise return null.
3833static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3834 const ARMSubtarget *ST, DebugLoc dl) {
3835 uint64_t Val;
3836 if (!isa<ConstantSDNode>(N))
3837 return SDValue();
3838 Val = cast<ConstantSDNode>(N)->getZExtValue();
3839
3840 if (ST->isThumb1Only()) {
3841 if (Val <= 255 || ~Val <= 255)
3842 return DAG.getConstant(Val, MVT::i32);
3843 } else {
3844 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3845 return DAG.getConstant(Val, MVT::i32);
3846 }
3847 return SDValue();
3848}
3849
Bob Wilson5bafff32009-06-22 23:27:02 +00003850// If this is a case we can't handle, return null and let the default
3851// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003852SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3853 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003854 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003855 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003856 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003857
3858 APInt SplatBits, SplatUndef;
3859 unsigned SplatBitSize;
3860 bool HasAnyUndefs;
3861 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003862 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003863 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003864 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003865 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003866 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003867 DAG, VmovVT, VT.is128BitVector(),
3868 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003869 if (Val.getNode()) {
3870 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003871 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003872 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003873
3874 // Try an immediate VMVN.
3875 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3876 ((1LL << SplatBitSize) - 1));
3877 Val = isNEONModifiedImm(NegatedImm,
3878 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003879 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003880 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003881 if (Val.getNode()) {
3882 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003883 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003884 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003885 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003886 }
3887
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003888 // Scan through the operands to see if only one value is used.
3889 unsigned NumElts = VT.getVectorNumElements();
3890 bool isOnlyLowElement = true;
3891 bool usesOnlyOneValue = true;
3892 bool isConstant = true;
3893 SDValue Value;
3894 for (unsigned i = 0; i < NumElts; ++i) {
3895 SDValue V = Op.getOperand(i);
3896 if (V.getOpcode() == ISD::UNDEF)
3897 continue;
3898 if (i > 0)
3899 isOnlyLowElement = false;
3900 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3901 isConstant = false;
3902
3903 if (!Value.getNode())
3904 Value = V;
3905 else if (V != Value)
3906 usesOnlyOneValue = false;
3907 }
3908
3909 if (!Value.getNode())
3910 return DAG.getUNDEF(VT);
3911
3912 if (isOnlyLowElement)
3913 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3914
Dale Johannesenf630c712010-07-29 20:10:08 +00003915 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3916
Dale Johannesen575cd142010-10-19 20:00:17 +00003917 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3918 // i32 and try again.
3919 if (usesOnlyOneValue && EltSize <= 32) {
3920 if (!isConstant)
3921 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3922 if (VT.getVectorElementType().isFloatingPoint()) {
3923 SmallVector<SDValue, 8> Ops;
3924 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003925 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003926 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003927 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3928 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003929 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3930 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003931 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003932 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003933 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3934 if (Val.getNode())
3935 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003936 }
3937
3938 // If all elements are constants and the case above didn't get hit, fall back
3939 // to the default expansion, which will generate a load from the constant
3940 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003941 if (isConstant)
3942 return SDValue();
3943
Bob Wilson11a1dff2011-01-07 21:37:30 +00003944 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3945 if (NumElts >= 4) {
3946 SDValue shuffle = ReconstructShuffle(Op, DAG);
3947 if (shuffle != SDValue())
3948 return shuffle;
3949 }
3950
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003951 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003952 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3953 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003954 if (EltSize >= 32) {
3955 // Do the expansion with floating-point types, since that is what the VFP
3956 // registers are defined to use, and since i64 is not legal.
3957 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3958 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003959 SmallVector<SDValue, 8> Ops;
3960 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003961 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003962 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003963 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003964 }
3965
3966 return SDValue();
3967}
3968
Bob Wilson11a1dff2011-01-07 21:37:30 +00003969// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003970// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00003971SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
3972 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00003973 DebugLoc dl = Op.getDebugLoc();
3974 EVT VT = Op.getValueType();
3975 unsigned NumElts = VT.getVectorNumElements();
3976
3977 SmallVector<SDValue, 2> SourceVecs;
3978 SmallVector<unsigned, 2> MinElts;
3979 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003980
Bob Wilson11a1dff2011-01-07 21:37:30 +00003981 for (unsigned i = 0; i < NumElts; ++i) {
3982 SDValue V = Op.getOperand(i);
3983 if (V.getOpcode() == ISD::UNDEF)
3984 continue;
3985 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
3986 // A shuffle can only come from building a vector from various
3987 // elements of other vectors.
3988 return SDValue();
3989 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003990
Bob Wilson11a1dff2011-01-07 21:37:30 +00003991 // Record this extraction against the appropriate vector if possible...
3992 SDValue SourceVec = V.getOperand(0);
3993 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
3994 bool FoundSource = false;
3995 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
3996 if (SourceVecs[j] == SourceVec) {
3997 if (MinElts[j] > EltNo)
3998 MinElts[j] = EltNo;
3999 if (MaxElts[j] < EltNo)
4000 MaxElts[j] = EltNo;
4001 FoundSource = true;
4002 break;
4003 }
4004 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004005
Bob Wilson11a1dff2011-01-07 21:37:30 +00004006 // Or record a new source if not...
4007 if (!FoundSource) {
4008 SourceVecs.push_back(SourceVec);
4009 MinElts.push_back(EltNo);
4010 MaxElts.push_back(EltNo);
4011 }
4012 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004013
Bob Wilson11a1dff2011-01-07 21:37:30 +00004014 // Currently only do something sane when at most two source vectors
4015 // involved.
4016 if (SourceVecs.size() > 2)
4017 return SDValue();
4018
4019 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4020 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004021
Bob Wilson11a1dff2011-01-07 21:37:30 +00004022 // This loop extracts the usage patterns of the source vectors
4023 // and prepares appropriate SDValues for a shuffle if possible.
4024 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4025 if (SourceVecs[i].getValueType() == VT) {
4026 // No VEXT necessary
4027 ShuffleSrcs[i] = SourceVecs[i];
4028 VEXTOffsets[i] = 0;
4029 continue;
4030 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4031 // It probably isn't worth padding out a smaller vector just to
4032 // break it down again in a shuffle.
4033 return SDValue();
4034 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004035
Bob Wilson11a1dff2011-01-07 21:37:30 +00004036 // Since only 64-bit and 128-bit vectors are legal on ARM and
4037 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004038 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4039 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004040
Bob Wilson11a1dff2011-01-07 21:37:30 +00004041 if (MaxElts[i] - MinElts[i] >= NumElts) {
4042 // Span too large for a VEXT to cope
4043 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004044 }
4045
Bob Wilson11a1dff2011-01-07 21:37:30 +00004046 if (MinElts[i] >= NumElts) {
4047 // The extraction can just take the second half
4048 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004049 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4050 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004051 DAG.getIntPtrConstant(NumElts));
4052 } else if (MaxElts[i] < NumElts) {
4053 // The extraction can just take the first half
4054 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004055 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4056 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004057 DAG.getIntPtrConstant(0));
4058 } else {
4059 // An actual VEXT is needed
4060 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004061 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4062 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004063 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004064 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4065 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004066 DAG.getIntPtrConstant(NumElts));
4067 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4068 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4069 }
4070 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004071
Bob Wilson11a1dff2011-01-07 21:37:30 +00004072 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004073
Bob Wilson11a1dff2011-01-07 21:37:30 +00004074 for (unsigned i = 0; i < NumElts; ++i) {
4075 SDValue Entry = Op.getOperand(i);
4076 if (Entry.getOpcode() == ISD::UNDEF) {
4077 Mask.push_back(-1);
4078 continue;
4079 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004080
Bob Wilson11a1dff2011-01-07 21:37:30 +00004081 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004082 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4083 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004084 if (ExtractVec == SourceVecs[0]) {
4085 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4086 } else {
4087 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4088 }
4089 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004090
Bob Wilson11a1dff2011-01-07 21:37:30 +00004091 // Final check before we try to produce nonsense...
4092 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004093 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4094 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004095
Bob Wilson11a1dff2011-01-07 21:37:30 +00004096 return SDValue();
4097}
4098
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004099/// isShuffleMaskLegal - Targets can use this to indicate that they only
4100/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4101/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4102/// are assumed to be legal.
4103bool
4104ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4105 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004106 if (VT.getVectorNumElements() == 4 &&
4107 (VT.is128BitVector() || VT.is64BitVector())) {
4108 unsigned PFIndexes[4];
4109 for (unsigned i = 0; i != 4; ++i) {
4110 if (M[i] < 0)
4111 PFIndexes[i] = 8;
4112 else
4113 PFIndexes[i] = M[i];
4114 }
4115
4116 // Compute the index in the perfect shuffle table.
4117 unsigned PFTableIndex =
4118 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4119 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4120 unsigned Cost = (PFEntry >> 30);
4121
4122 if (Cost <= 4)
4123 return true;
4124 }
4125
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004126 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004127 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004128
Bob Wilson53dd2452010-06-07 23:53:38 +00004129 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4130 return (EltSize >= 32 ||
4131 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004132 isVREVMask(M, VT, 64) ||
4133 isVREVMask(M, VT, 32) ||
4134 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004135 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004136 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004137 isVTRNMask(M, VT, WhichResult) ||
4138 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004139 isVZIPMask(M, VT, WhichResult) ||
4140 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4141 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4142 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004143}
4144
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004145/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4146/// the specified operations to build the shuffle.
4147static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4148 SDValue RHS, SelectionDAG &DAG,
4149 DebugLoc dl) {
4150 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4151 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4152 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4153
4154 enum {
4155 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4156 OP_VREV,
4157 OP_VDUP0,
4158 OP_VDUP1,
4159 OP_VDUP2,
4160 OP_VDUP3,
4161 OP_VEXT1,
4162 OP_VEXT2,
4163 OP_VEXT3,
4164 OP_VUZPL, // VUZP, left result
4165 OP_VUZPR, // VUZP, right result
4166 OP_VZIPL, // VZIP, left result
4167 OP_VZIPR, // VZIP, right result
4168 OP_VTRNL, // VTRN, left result
4169 OP_VTRNR // VTRN, right result
4170 };
4171
4172 if (OpNum == OP_COPY) {
4173 if (LHSID == (1*9+2)*9+3) return LHS;
4174 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4175 return RHS;
4176 }
4177
4178 SDValue OpLHS, OpRHS;
4179 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4180 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4181 EVT VT = OpLHS.getValueType();
4182
4183 switch (OpNum) {
4184 default: llvm_unreachable("Unknown shuffle opcode!");
4185 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004186 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004187 if (VT.getVectorElementType() == MVT::i32 ||
4188 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004189 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4190 // vrev <4 x i16> -> VREV32
4191 if (VT.getVectorElementType() == MVT::i16)
4192 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4193 // vrev <4 x i8> -> VREV16
4194 assert(VT.getVectorElementType() == MVT::i8);
4195 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004196 case OP_VDUP0:
4197 case OP_VDUP1:
4198 case OP_VDUP2:
4199 case OP_VDUP3:
4200 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004201 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004202 case OP_VEXT1:
4203 case OP_VEXT2:
4204 case OP_VEXT3:
4205 return DAG.getNode(ARMISD::VEXT, dl, VT,
4206 OpLHS, OpRHS,
4207 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4208 case OP_VUZPL:
4209 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004210 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004211 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4212 case OP_VZIPL:
4213 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004214 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004215 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4216 case OP_VTRNL:
4217 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004218 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4219 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004220 }
4221}
4222
Bill Wendling69a05a72011-03-14 23:02:38 +00004223static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4224 SmallVectorImpl<int> &ShuffleMask,
4225 SelectionDAG &DAG) {
4226 // Check to see if we can use the VTBL instruction.
4227 SDValue V1 = Op.getOperand(0);
4228 SDValue V2 = Op.getOperand(1);
4229 DebugLoc DL = Op.getDebugLoc();
4230
4231 SmallVector<SDValue, 8> VTBLMask;
4232 for (SmallVectorImpl<int>::iterator
4233 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4234 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4235
4236 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4237 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4238 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4239 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004240
Owen Anderson76706012011-04-05 21:48:57 +00004241 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004242 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4243 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004244}
4245
Bob Wilson5bafff32009-06-22 23:27:02 +00004246static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004247 SDValue V1 = Op.getOperand(0);
4248 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004249 DebugLoc dl = Op.getDebugLoc();
4250 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004251 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004252 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004253
Bob Wilson28865062009-08-13 02:13:04 +00004254 // Convert shuffles that are directly supported on NEON to target-specific
4255 // DAG nodes, instead of keeping them as shuffles and matching them again
4256 // during code selection. This is more efficient and avoids the possibility
4257 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004258 // FIXME: floating-point vectors should be canonicalized to integer vectors
4259 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004260 SVN->getMask(ShuffleMask);
4261
Bob Wilson53dd2452010-06-07 23:53:38 +00004262 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4263 if (EltSize <= 32) {
4264 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4265 int Lane = SVN->getSplatIndex();
4266 // If this is undef splat, generate it via "just" vdup, if possible.
4267 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004268
Bob Wilson53dd2452010-06-07 23:53:38 +00004269 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4270 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4271 }
4272 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4273 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004274 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004275
4276 bool ReverseVEXT;
4277 unsigned Imm;
4278 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4279 if (ReverseVEXT)
4280 std::swap(V1, V2);
4281 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4282 DAG.getConstant(Imm, MVT::i32));
4283 }
4284
4285 if (isVREVMask(ShuffleMask, VT, 64))
4286 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4287 if (isVREVMask(ShuffleMask, VT, 32))
4288 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4289 if (isVREVMask(ShuffleMask, VT, 16))
4290 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4291
4292 // Check for Neon shuffles that modify both input vectors in place.
4293 // If both results are used, i.e., if there are two shuffles with the same
4294 // source operands and with masks corresponding to both results of one of
4295 // these operations, DAG memoization will ensure that a single node is
4296 // used for both shuffles.
4297 unsigned WhichResult;
4298 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4299 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4300 V1, V2).getValue(WhichResult);
4301 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4302 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4303 V1, V2).getValue(WhichResult);
4304 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4305 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4306 V1, V2).getValue(WhichResult);
4307
4308 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4309 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4310 V1, V1).getValue(WhichResult);
4311 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4312 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4313 V1, V1).getValue(WhichResult);
4314 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4315 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4316 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004317 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004318
Bob Wilsonc692cb72009-08-21 20:54:19 +00004319 // If the shuffle is not directly supported and it has 4 elements, use
4320 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004321 unsigned NumElts = VT.getVectorNumElements();
4322 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004323 unsigned PFIndexes[4];
4324 for (unsigned i = 0; i != 4; ++i) {
4325 if (ShuffleMask[i] < 0)
4326 PFIndexes[i] = 8;
4327 else
4328 PFIndexes[i] = ShuffleMask[i];
4329 }
4330
4331 // Compute the index in the perfect shuffle table.
4332 unsigned PFTableIndex =
4333 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004334 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4335 unsigned Cost = (PFEntry >> 30);
4336
4337 if (Cost <= 4)
4338 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4339 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004340
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004341 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004342 if (EltSize >= 32) {
4343 // Do the expansion with floating-point types, since that is what the VFP
4344 // registers are defined to use, and since i64 is not legal.
4345 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4346 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004347 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4348 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004349 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004350 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004351 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004352 Ops.push_back(DAG.getUNDEF(EltVT));
4353 else
4354 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4355 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4356 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4357 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004358 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004359 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004360 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004361 }
4362
Bill Wendling69a05a72011-03-14 23:02:38 +00004363 if (VT == MVT::v8i8) {
4364 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4365 if (NewOp.getNode())
4366 return NewOp;
4367 }
4368
Bob Wilson22cac0d2009-08-14 05:16:33 +00004369 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004370}
4371
Bob Wilson5bafff32009-06-22 23:27:02 +00004372static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004373 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004374 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004375 if (!isa<ConstantSDNode>(Lane))
4376 return SDValue();
4377
4378 SDValue Vec = Op.getOperand(0);
4379 if (Op.getValueType() == MVT::i32 &&
4380 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4381 DebugLoc dl = Op.getDebugLoc();
4382 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4383 }
4384
4385 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004386}
4387
Bob Wilsona6d65862009-08-03 20:36:38 +00004388static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4389 // The only time a CONCAT_VECTORS operation can have legal types is when
4390 // two 64-bit vectors are concatenated to a 128-bit vector.
4391 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4392 "unexpected CONCAT_VECTORS");
4393 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004394 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004395 SDValue Op0 = Op.getOperand(0);
4396 SDValue Op1 = Op.getOperand(1);
4397 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004398 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004399 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004400 DAG.getIntPtrConstant(0));
4401 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004402 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004403 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004404 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004405 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004406}
4407
Bob Wilson626613d2010-11-23 19:38:38 +00004408/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4409/// element has been zero/sign-extended, depending on the isSigned parameter,
4410/// from an integer type half its size.
4411static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4412 bool isSigned) {
4413 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4414 EVT VT = N->getValueType(0);
4415 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4416 SDNode *BVN = N->getOperand(0).getNode();
4417 if (BVN->getValueType(0) != MVT::v4i32 ||
4418 BVN->getOpcode() != ISD::BUILD_VECTOR)
4419 return false;
4420 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4421 unsigned HiElt = 1 - LoElt;
4422 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4423 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4424 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4425 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4426 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4427 return false;
4428 if (isSigned) {
4429 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4430 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4431 return true;
4432 } else {
4433 if (Hi0->isNullValue() && Hi1->isNullValue())
4434 return true;
4435 }
4436 return false;
4437 }
4438
4439 if (N->getOpcode() != ISD::BUILD_VECTOR)
4440 return false;
4441
4442 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4443 SDNode *Elt = N->getOperand(i).getNode();
4444 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4445 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4446 unsigned HalfSize = EltSize / 2;
4447 if (isSigned) {
4448 int64_t SExtVal = C->getSExtValue();
4449 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4450 return false;
4451 } else {
4452 if ((C->getZExtValue() >> HalfSize) != 0)
4453 return false;
4454 }
4455 continue;
4456 }
4457 return false;
4458 }
4459
4460 return true;
4461}
4462
4463/// isSignExtended - Check if a node is a vector value that is sign-extended
4464/// or a constant BUILD_VECTOR with sign-extended elements.
4465static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4466 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4467 return true;
4468 if (isExtendedBUILD_VECTOR(N, DAG, true))
4469 return true;
4470 return false;
4471}
4472
4473/// isZeroExtended - Check if a node is a vector value that is zero-extended
4474/// or a constant BUILD_VECTOR with zero-extended elements.
4475static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4476 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4477 return true;
4478 if (isExtendedBUILD_VECTOR(N, DAG, false))
4479 return true;
4480 return false;
4481}
4482
4483/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4484/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004485static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4486 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4487 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004488 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4489 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4490 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4491 LD->isNonTemporal(), LD->getAlignment());
4492 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4493 // have been legalized as a BITCAST from v4i32.
4494 if (N->getOpcode() == ISD::BITCAST) {
4495 SDNode *BVN = N->getOperand(0).getNode();
4496 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4497 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4498 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4499 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4500 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4501 }
4502 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4503 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4504 EVT VT = N->getValueType(0);
4505 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4506 unsigned NumElts = VT.getVectorNumElements();
4507 MVT TruncVT = MVT::getIntegerVT(EltSize);
4508 SmallVector<SDValue, 8> Ops;
4509 for (unsigned i = 0; i != NumElts; ++i) {
4510 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4511 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004512 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004513 }
4514 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4515 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004516}
4517
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004518static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4519 unsigned Opcode = N->getOpcode();
4520 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4521 SDNode *N0 = N->getOperand(0).getNode();
4522 SDNode *N1 = N->getOperand(1).getNode();
4523 return N0->hasOneUse() && N1->hasOneUse() &&
4524 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4525 }
4526 return false;
4527}
4528
4529static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4530 unsigned Opcode = N->getOpcode();
4531 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4532 SDNode *N0 = N->getOperand(0).getNode();
4533 SDNode *N1 = N->getOperand(1).getNode();
4534 return N0->hasOneUse() && N1->hasOneUse() &&
4535 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4536 }
4537 return false;
4538}
4539
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004540static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4541 // Multiplications are only custom-lowered for 128-bit vectors so that
4542 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4543 EVT VT = Op.getValueType();
4544 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4545 SDNode *N0 = Op.getOperand(0).getNode();
4546 SDNode *N1 = Op.getOperand(1).getNode();
4547 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004548 bool isMLA = false;
4549 bool isN0SExt = isSignExtended(N0, DAG);
4550 bool isN1SExt = isSignExtended(N1, DAG);
4551 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004552 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004553 else {
4554 bool isN0ZExt = isZeroExtended(N0, DAG);
4555 bool isN1ZExt = isZeroExtended(N1, DAG);
4556 if (isN0ZExt && isN1ZExt)
4557 NewOpc = ARMISD::VMULLu;
4558 else if (isN1SExt || isN1ZExt) {
4559 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4560 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4561 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4562 NewOpc = ARMISD::VMULLs;
4563 isMLA = true;
4564 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4565 NewOpc = ARMISD::VMULLu;
4566 isMLA = true;
4567 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4568 std::swap(N0, N1);
4569 NewOpc = ARMISD::VMULLu;
4570 isMLA = true;
4571 }
4572 }
4573
4574 if (!NewOpc) {
4575 if (VT == MVT::v2i64)
4576 // Fall through to expand this. It is not legal.
4577 return SDValue();
4578 else
4579 // Other vector multiplications are legal.
4580 return Op;
4581 }
4582 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004583
4584 // Legalize to a VMULL instruction.
4585 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004586 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004587 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004588 if (!isMLA) {
4589 Op0 = SkipExtension(N0, DAG);
4590 assert(Op0.getValueType().is64BitVector() &&
4591 Op1.getValueType().is64BitVector() &&
4592 "unexpected types for extended operands to VMULL");
4593 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4594 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004595
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004596 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4597 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4598 // vmull q0, d4, d6
4599 // vmlal q0, d5, d6
4600 // is faster than
4601 // vaddl q0, d4, d5
4602 // vmovl q1, d6
4603 // vmul q0, q0, q1
4604 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4605 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4606 EVT Op1VT = Op1.getValueType();
4607 return DAG.getNode(N0->getOpcode(), DL, VT,
4608 DAG.getNode(NewOpc, DL, VT,
4609 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4610 DAG.getNode(NewOpc, DL, VT,
4611 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004612}
4613
Owen Anderson76706012011-04-05 21:48:57 +00004614static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004615LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4616 // Convert to float
4617 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4618 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4619 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4620 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4621 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4622 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4623 // Get reciprocal estimate.
4624 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004625 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004626 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4627 // Because char has a smaller range than uchar, we can actually get away
4628 // without any newton steps. This requires that we use a weird bias
4629 // of 0xb000, however (again, this has been exhaustively tested).
4630 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4631 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4632 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4633 Y = DAG.getConstant(0xb000, MVT::i32);
4634 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4635 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4636 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4637 // Convert back to short.
4638 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4639 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4640 return X;
4641}
4642
Owen Anderson76706012011-04-05 21:48:57 +00004643static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004644LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4645 SDValue N2;
4646 // Convert to float.
4647 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4648 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4649 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4650 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4651 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4652 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004653
Nate Begeman7973f352011-02-11 20:53:29 +00004654 // Use reciprocal estimate and one refinement step.
4655 // float4 recip = vrecpeq_f32(yf);
4656 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004657 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004658 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004659 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004660 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4661 N1, N2);
4662 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4663 // Because short has a smaller range than ushort, we can actually get away
4664 // with only a single newton step. This requires that we use a weird bias
4665 // of 89, however (again, this has been exhaustively tested).
4666 // float4 result = as_float4(as_int4(xf*recip) + 89);
4667 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4668 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4669 N1 = DAG.getConstant(89, MVT::i32);
4670 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4671 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4672 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4673 // Convert back to integer and return.
4674 // return vmovn_s32(vcvt_s32_f32(result));
4675 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4676 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4677 return N0;
4678}
4679
4680static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4681 EVT VT = Op.getValueType();
4682 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4683 "unexpected type for custom-lowering ISD::SDIV");
4684
4685 DebugLoc dl = Op.getDebugLoc();
4686 SDValue N0 = Op.getOperand(0);
4687 SDValue N1 = Op.getOperand(1);
4688 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004689
Nate Begeman7973f352011-02-11 20:53:29 +00004690 if (VT == MVT::v8i8) {
4691 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4692 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004693
Nate Begeman7973f352011-02-11 20:53:29 +00004694 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4695 DAG.getIntPtrConstant(4));
4696 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004697 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004698 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4699 DAG.getIntPtrConstant(0));
4700 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4701 DAG.getIntPtrConstant(0));
4702
4703 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4704 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4705
4706 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4707 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004708
Nate Begeman7973f352011-02-11 20:53:29 +00004709 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4710 return N0;
4711 }
4712 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4713}
4714
4715static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4716 EVT VT = Op.getValueType();
4717 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4718 "unexpected type for custom-lowering ISD::UDIV");
4719
4720 DebugLoc dl = Op.getDebugLoc();
4721 SDValue N0 = Op.getOperand(0);
4722 SDValue N1 = Op.getOperand(1);
4723 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004724
Nate Begeman7973f352011-02-11 20:53:29 +00004725 if (VT == MVT::v8i8) {
4726 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4727 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004728
Nate Begeman7973f352011-02-11 20:53:29 +00004729 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4730 DAG.getIntPtrConstant(4));
4731 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004732 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004733 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4734 DAG.getIntPtrConstant(0));
4735 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4736 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004737
Nate Begeman7973f352011-02-11 20:53:29 +00004738 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4739 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004740
Nate Begeman7973f352011-02-11 20:53:29 +00004741 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4742 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004743
4744 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004745 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4746 N0);
4747 return N0;
4748 }
Owen Anderson76706012011-04-05 21:48:57 +00004749
Nate Begeman7973f352011-02-11 20:53:29 +00004750 // v4i16 sdiv ... Convert to float.
4751 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4752 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4753 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4754 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4755 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4756 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
4757
4758 // Use reciprocal estimate and two refinement steps.
4759 // float4 recip = vrecpeq_f32(yf);
4760 // recip *= vrecpsq_f32(yf, recip);
4761 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004762 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004763 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004764 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004765 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4766 N1, N2);
4767 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004768 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004769 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4770 N1, N2);
4771 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4772 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4773 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4774 // and that it will never cause us to return an answer too large).
4775 // float4 result = as_float4(as_int4(xf*recip) + 89);
4776 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4777 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4778 N1 = DAG.getConstant(2, MVT::i32);
4779 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4780 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4781 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4782 // Convert back to integer and return.
4783 // return vmovn_u32(vcvt_s32_f32(result));
4784 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4785 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4786 return N0;
4787}
4788
Dan Gohmand858e902010-04-17 15:26:15 +00004789SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004790 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004791 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004792 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004793 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004794 case ISD::GlobalAddress:
4795 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4796 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004797 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004798 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004799 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4800 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004801 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004802 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004803 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004804 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004805 case ISD::SINT_TO_FP:
4806 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4807 case ISD::FP_TO_SINT:
4808 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004809 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004810 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004811 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004812 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004813 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004814 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004815 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004816 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4817 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00004818 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004819 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004820 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004821 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004822 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004823 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004824 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004825 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004826 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004827 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004828 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004829 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004830 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004831 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004832 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00004833 case ISD::SDIV: return LowerSDIV(Op, DAG);
4834 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004835 }
Dan Gohman475871a2008-07-27 21:46:04 +00004836 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004837}
4838
Duncan Sands1607f052008-12-01 11:39:25 +00004839/// ReplaceNodeResults - Replace the results of node with an illegal result
4840/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004841void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4842 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004843 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004844 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004845 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004846 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004847 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004848 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004849 case ISD::BITCAST:
4850 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004851 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004852 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004853 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004854 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004855 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004856 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004857 if (Res.getNode())
4858 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004859}
Chris Lattner27a6c732007-11-24 07:07:01 +00004860
Evan Chenga8e29892007-01-19 07:51:42 +00004861//===----------------------------------------------------------------------===//
4862// ARM Scheduler Hooks
4863//===----------------------------------------------------------------------===//
4864
4865MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004866ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4867 MachineBasicBlock *BB,
4868 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004869 unsigned dest = MI->getOperand(0).getReg();
4870 unsigned ptr = MI->getOperand(1).getReg();
4871 unsigned oldval = MI->getOperand(2).getReg();
4872 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004873 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4874 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004875 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004876
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004877 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
4878 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00004879 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004880 : ARM::GPRRegisterClass);
4881
4882 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00004883 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
4884 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
4885 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004886 }
4887
Jim Grosbach5278eb82009-12-11 01:42:04 +00004888 unsigned ldrOpc, strOpc;
4889 switch (Size) {
4890 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004891 case 1:
4892 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00004893 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004894 break;
4895 case 2:
4896 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4897 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4898 break;
4899 case 4:
4900 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4901 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4902 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004903 }
4904
4905 MachineFunction *MF = BB->getParent();
4906 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4907 MachineFunction::iterator It = BB;
4908 ++It; // insert the new blocks after the current block
4909
4910 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4911 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4912 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4913 MF->insert(It, loop1MBB);
4914 MF->insert(It, loop2MBB);
4915 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004916
4917 // Transfer the remainder of BB and its successor edges to exitMBB.
4918 exitMBB->splice(exitMBB->begin(), BB,
4919 llvm::next(MachineBasicBlock::iterator(MI)),
4920 BB->end());
4921 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004922
4923 // thisMBB:
4924 // ...
4925 // fallthrough --> loop1MBB
4926 BB->addSuccessor(loop1MBB);
4927
4928 // loop1MBB:
4929 // ldrex dest, [ptr]
4930 // cmp dest, oldval
4931 // bne exitMBB
4932 BB = loop1MBB;
4933 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004934 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004935 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004936 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4937 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004938 BB->addSuccessor(loop2MBB);
4939 BB->addSuccessor(exitMBB);
4940
4941 // loop2MBB:
4942 // strex scratch, newval, [ptr]
4943 // cmp scratch, #0
4944 // bne loop1MBB
4945 BB = loop2MBB;
4946 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
4947 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004948 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004949 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004950 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4951 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004952 BB->addSuccessor(loop1MBB);
4953 BB->addSuccessor(exitMBB);
4954
4955 // exitMBB:
4956 // ...
4957 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00004958
Dan Gohman14152b42010-07-06 20:24:04 +00004959 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00004960
Jim Grosbach5278eb82009-12-11 01:42:04 +00004961 return BB;
4962}
4963
4964MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004965ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
4966 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00004967 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
4968 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4969
4970 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004971 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004972 MachineFunction::iterator It = BB;
4973 ++It;
4974
4975 unsigned dest = MI->getOperand(0).getReg();
4976 unsigned ptr = MI->getOperand(1).getReg();
4977 unsigned incr = MI->getOperand(2).getReg();
4978 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00004979
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004980 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004981 unsigned ldrOpc, strOpc;
4982 switch (Size) {
4983 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004984 case 1:
4985 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00004986 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004987 break;
4988 case 2:
4989 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4990 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4991 break;
4992 case 4:
4993 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4994 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4995 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00004996 }
4997
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004998 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4999 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5000 MF->insert(It, loopMBB);
5001 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005002
5003 // Transfer the remainder of BB and its successor edges to exitMBB.
5004 exitMBB->splice(exitMBB->begin(), BB,
5005 llvm::next(MachineBasicBlock::iterator(MI)),
5006 BB->end());
5007 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005008
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005009 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005010 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
5011 unsigned scratch2 = (!BinOpcode) ? incr :
5012 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
5013
5014 // thisMBB:
5015 // ...
5016 // fallthrough --> loopMBB
5017 BB->addSuccessor(loopMBB);
5018
5019 // loopMBB:
5020 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005021 // <binop> scratch2, dest, incr
5022 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005023 // cmp scratch, #0
5024 // bne- loopMBB
5025 // fallthrough --> exitMBB
5026 BB = loopMBB;
5027 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00005028 if (BinOpcode) {
5029 // operand order needs to go the other way for NAND
5030 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5031 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5032 addReg(incr).addReg(dest)).addReg(0);
5033 else
5034 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5035 addReg(dest).addReg(incr)).addReg(0);
5036 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005037
5038 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5039 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005040 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005041 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005042 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5043 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005044
5045 BB->addSuccessor(loopMBB);
5046 BB->addSuccessor(exitMBB);
5047
5048 // exitMBB:
5049 // ...
5050 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005051
Dan Gohman14152b42010-07-06 20:24:04 +00005052 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005053
Jim Grosbachc3c23542009-12-14 04:22:04 +00005054 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005055}
5056
Jim Grosbachf7da8822011-04-26 19:44:18 +00005057MachineBasicBlock *
5058ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5059 MachineBasicBlock *BB,
5060 unsigned Size,
5061 bool signExtend,
5062 ARMCC::CondCodes Cond) const {
5063 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5064
5065 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5066 MachineFunction *MF = BB->getParent();
5067 MachineFunction::iterator It = BB;
5068 ++It;
5069
5070 unsigned dest = MI->getOperand(0).getReg();
5071 unsigned ptr = MI->getOperand(1).getReg();
5072 unsigned incr = MI->getOperand(2).getReg();
5073 unsigned oldval = dest;
5074 DebugLoc dl = MI->getDebugLoc();
5075
5076 bool isThumb2 = Subtarget->isThumb2();
5077 unsigned ldrOpc, strOpc, extendOpc;
5078 switch (Size) {
5079 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5080 case 1:
5081 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5082 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
5083 extendOpc = isThumb2 ? ARM::t2SXTBr : ARM::SXTBr;
5084 break;
5085 case 2:
5086 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5087 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5088 extendOpc = isThumb2 ? ARM::t2SXTHr : ARM::SXTHr;
5089 break;
5090 case 4:
5091 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5092 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5093 extendOpc = 0;
5094 break;
5095 }
5096
5097 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5098 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5099 MF->insert(It, loopMBB);
5100 MF->insert(It, exitMBB);
5101
5102 // Transfer the remainder of BB and its successor edges to exitMBB.
5103 exitMBB->splice(exitMBB->begin(), BB,
5104 llvm::next(MachineBasicBlock::iterator(MI)),
5105 BB->end());
5106 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5107
5108 MachineRegisterInfo &RegInfo = MF->getRegInfo();
5109 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
5110 unsigned scratch2 = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
5111
5112 // thisMBB:
5113 // ...
5114 // fallthrough --> loopMBB
5115 BB->addSuccessor(loopMBB);
5116
5117 // loopMBB:
5118 // ldrex dest, ptr
5119 // (sign extend dest, if required)
5120 // cmp dest, incr
5121 // cmov.cond scratch2, dest, incr
5122 // strex scratch, scratch2, ptr
5123 // cmp scratch, #0
5124 // bne- loopMBB
5125 // fallthrough --> exitMBB
5126 BB = loopMBB;
5127 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
5128
5129 // Sign extend the value, if necessary.
5130 if (signExtend && extendOpc) {
5131 oldval = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
5132 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval).addReg(dest));
5133 }
5134
5135 // Build compare and cmov instructions.
5136 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5137 .addReg(oldval).addReg(incr));
5138 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5139 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5140
5141 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5142 .addReg(ptr));
5143 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5144 .addReg(scratch).addImm(0));
5145 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5146 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5147
5148 BB->addSuccessor(loopMBB);
5149 BB->addSuccessor(exitMBB);
5150
5151 // exitMBB:
5152 // ...
5153 BB = exitMBB;
5154
5155 MI->eraseFromParent(); // The instruction is gone now.
5156
5157 return BB;
5158}
5159
Evan Cheng218977b2010-07-13 19:27:42 +00005160static
5161MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
5162 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
5163 E = MBB->succ_end(); I != E; ++I)
5164 if (*I != Succ)
5165 return *I;
5166 llvm_unreachable("Expecting a BB with two successors!");
5167}
5168
Andrew Trick1c3af772011-04-23 03:55:32 +00005169// FIXME: This opcode table should obviously be expressed in the target
5170// description. We probably just need a "machine opcode" value in the pseudo
5171// instruction. But the ideal solution maybe to simply remove the "S" version
5172// of the opcode altogether.
5173struct AddSubFlagsOpcodePair {
5174 unsigned PseudoOpc;
5175 unsigned MachineOpc;
5176};
5177
5178static AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
5179 {ARM::ADCSri, ARM::ADCri},
5180 {ARM::ADCSrr, ARM::ADCrr},
5181 {ARM::ADCSrs, ARM::ADCrs},
5182 {ARM::SBCSri, ARM::SBCri},
5183 {ARM::SBCSrr, ARM::SBCrr},
5184 {ARM::SBCSrs, ARM::SBCrs},
5185 {ARM::RSBSri, ARM::RSBri},
5186 {ARM::RSBSrr, ARM::RSBrr},
5187 {ARM::RSBSrs, ARM::RSBrs},
5188 {ARM::RSCSri, ARM::RSCri},
5189 {ARM::RSCSrs, ARM::RSCrs},
5190 {ARM::t2ADCSri, ARM::t2ADCri},
5191 {ARM::t2ADCSrr, ARM::t2ADCrr},
5192 {ARM::t2ADCSrs, ARM::t2ADCrs},
5193 {ARM::t2SBCSri, ARM::t2SBCri},
5194 {ARM::t2SBCSrr, ARM::t2SBCrr},
5195 {ARM::t2SBCSrs, ARM::t2SBCrs},
5196 {ARM::t2RSBSri, ARM::t2RSBri},
5197 {ARM::t2RSBSrs, ARM::t2RSBrs},
5198};
5199
5200// Convert and Add or Subtract with Carry and Flags to a generic opcode with
5201// CPSR<def> operand. e.g. ADCS (...) -> ADC (... CPSR<def>).
5202//
5203// FIXME: Somewhere we should assert that CPSR<def> is in the correct
5204// position to be recognized by the target descrition as the 'S' bit.
5205bool ARMTargetLowering::RemapAddSubWithFlags(MachineInstr *MI,
5206 MachineBasicBlock *BB) const {
5207 unsigned OldOpc = MI->getOpcode();
5208 unsigned NewOpc = 0;
5209
5210 // This is only called for instructions that need remapping, so iterating over
5211 // the tiny opcode table is not costly.
5212 static const int NPairs =
5213 sizeof(AddSubFlagsOpcodeMap) / sizeof(AddSubFlagsOpcodePair);
5214 for (AddSubFlagsOpcodePair *Pair = &AddSubFlagsOpcodeMap[0],
5215 *End = &AddSubFlagsOpcodeMap[NPairs]; Pair != End; ++Pair) {
5216 if (OldOpc == Pair->PseudoOpc) {
5217 NewOpc = Pair->MachineOpc;
5218 break;
5219 }
5220 }
5221 if (!NewOpc)
5222 return false;
5223
5224 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5225 DebugLoc dl = MI->getDebugLoc();
5226 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
5227 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
5228 MIB.addOperand(MI->getOperand(i));
5229 AddDefaultPred(MIB);
5230 MIB.addReg(ARM::CPSR, RegState::Define); // S bit
5231 MI->eraseFromParent();
5232 return true;
5233}
5234
Jim Grosbache801dc42009-12-12 01:40:06 +00005235MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00005236ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00005237 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005238 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00005239 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005240 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00005241 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00005242 default: {
5243 if (RemapAddSubWithFlags(MI, BB))
5244 return BB;
5245
Jim Grosbach5278eb82009-12-11 01:42:04 +00005246 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00005247 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00005248 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005249 case ARM::ATOMIC_LOAD_ADD_I8:
5250 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5251 case ARM::ATOMIC_LOAD_ADD_I16:
5252 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5253 case ARM::ATOMIC_LOAD_ADD_I32:
5254 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005255
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005256 case ARM::ATOMIC_LOAD_AND_I8:
5257 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5258 case ARM::ATOMIC_LOAD_AND_I16:
5259 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5260 case ARM::ATOMIC_LOAD_AND_I32:
5261 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005262
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005263 case ARM::ATOMIC_LOAD_OR_I8:
5264 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5265 case ARM::ATOMIC_LOAD_OR_I16:
5266 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5267 case ARM::ATOMIC_LOAD_OR_I32:
5268 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005269
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005270 case ARM::ATOMIC_LOAD_XOR_I8:
5271 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5272 case ARM::ATOMIC_LOAD_XOR_I16:
5273 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5274 case ARM::ATOMIC_LOAD_XOR_I32:
5275 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005276
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005277 case ARM::ATOMIC_LOAD_NAND_I8:
5278 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5279 case ARM::ATOMIC_LOAD_NAND_I16:
5280 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5281 case ARM::ATOMIC_LOAD_NAND_I32:
5282 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005283
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005284 case ARM::ATOMIC_LOAD_SUB_I8:
5285 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5286 case ARM::ATOMIC_LOAD_SUB_I16:
5287 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5288 case ARM::ATOMIC_LOAD_SUB_I32:
5289 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005290
Jim Grosbachf7da8822011-04-26 19:44:18 +00005291 case ARM::ATOMIC_LOAD_MIN_I8:
5292 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
5293 case ARM::ATOMIC_LOAD_MIN_I16:
5294 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
5295 case ARM::ATOMIC_LOAD_MIN_I32:
5296 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
5297
5298 case ARM::ATOMIC_LOAD_MAX_I8:
5299 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
5300 case ARM::ATOMIC_LOAD_MAX_I16:
5301 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
5302 case ARM::ATOMIC_LOAD_MAX_I32:
5303 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
5304
5305 case ARM::ATOMIC_LOAD_UMIN_I8:
5306 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
5307 case ARM::ATOMIC_LOAD_UMIN_I16:
5308 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
5309 case ARM::ATOMIC_LOAD_UMIN_I32:
5310 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
5311
5312 case ARM::ATOMIC_LOAD_UMAX_I8:
5313 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
5314 case ARM::ATOMIC_LOAD_UMAX_I16:
5315 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
5316 case ARM::ATOMIC_LOAD_UMAX_I32:
5317 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
5318
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005319 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
5320 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
5321 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00005322
5323 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
5324 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
5325 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005326
Evan Cheng007ea272009-08-12 05:17:19 +00005327 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00005328 // To "insert" a SELECT_CC instruction, we actually have to insert the
5329 // diamond control-flow pattern. The incoming instruction knows the
5330 // destination vreg to set, the condition code register to branch on, the
5331 // true/false values to select between, and a branch opcode to use.
5332 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005333 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00005334 ++It;
5335
5336 // thisMBB:
5337 // ...
5338 // TrueVal = ...
5339 // cmpTY ccX, r1, r2
5340 // bCC copy1MBB
5341 // fallthrough --> copy0MBB
5342 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005343 MachineFunction *F = BB->getParent();
5344 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
5345 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00005346 F->insert(It, copy0MBB);
5347 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005348
5349 // Transfer the remainder of BB and its successor edges to sinkMBB.
5350 sinkMBB->splice(sinkMBB->begin(), BB,
5351 llvm::next(MachineBasicBlock::iterator(MI)),
5352 BB->end());
5353 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
5354
Dan Gohman258c58c2010-07-06 15:49:48 +00005355 BB->addSuccessor(copy0MBB);
5356 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00005357
Dan Gohman14152b42010-07-06 20:24:04 +00005358 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
5359 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
5360
Evan Chenga8e29892007-01-19 07:51:42 +00005361 // copy0MBB:
5362 // %FalseValue = ...
5363 // # fallthrough to sinkMBB
5364 BB = copy0MBB;
5365
5366 // Update machine-CFG edges
5367 BB->addSuccessor(sinkMBB);
5368
5369 // sinkMBB:
5370 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5371 // ...
5372 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00005373 BuildMI(*BB, BB->begin(), dl,
5374 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00005375 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5376 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5377
Dan Gohman14152b42010-07-06 20:24:04 +00005378 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00005379 return BB;
5380 }
Evan Cheng86198642009-08-07 00:34:42 +00005381
Evan Cheng218977b2010-07-13 19:27:42 +00005382 case ARM::BCCi64:
5383 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00005384 // If there is an unconditional branch to the other successor, remove it.
5385 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00005386
Evan Cheng218977b2010-07-13 19:27:42 +00005387 // Compare both parts that make up the double comparison separately for
5388 // equality.
5389 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
5390
5391 unsigned LHS1 = MI->getOperand(1).getReg();
5392 unsigned LHS2 = MI->getOperand(2).getReg();
5393 if (RHSisZero) {
5394 AddDefaultPred(BuildMI(BB, dl,
5395 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5396 .addReg(LHS1).addImm(0));
5397 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5398 .addReg(LHS2).addImm(0)
5399 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5400 } else {
5401 unsigned RHS1 = MI->getOperand(3).getReg();
5402 unsigned RHS2 = MI->getOperand(4).getReg();
5403 AddDefaultPred(BuildMI(BB, dl,
5404 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5405 .addReg(LHS1).addReg(RHS1));
5406 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5407 .addReg(LHS2).addReg(RHS2)
5408 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5409 }
5410
5411 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
5412 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
5413 if (MI->getOperand(0).getImm() == ARMCC::NE)
5414 std::swap(destMBB, exitMBB);
5415
5416 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5417 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
5418 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
5419 .addMBB(exitMBB);
5420
5421 MI->eraseFromParent(); // The pseudo instruction is gone now.
5422 return BB;
5423 }
Evan Chenga8e29892007-01-19 07:51:42 +00005424 }
5425}
5426
5427//===----------------------------------------------------------------------===//
5428// ARM Optimization Hooks
5429//===----------------------------------------------------------------------===//
5430
Chris Lattnerd1980a52009-03-12 06:52:53 +00005431static
5432SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
5433 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005434 SelectionDAG &DAG = DCI.DAG;
5435 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00005436 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00005437 unsigned Opc = N->getOpcode();
5438 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
5439 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
5440 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
5441 ISD::CondCode CC = ISD::SETCC_INVALID;
5442
5443 if (isSlctCC) {
5444 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
5445 } else {
5446 SDValue CCOp = Slct.getOperand(0);
5447 if (CCOp.getOpcode() == ISD::SETCC)
5448 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
5449 }
5450
5451 bool DoXform = false;
5452 bool InvCC = false;
5453 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
5454 "Bad input!");
5455
5456 if (LHS.getOpcode() == ISD::Constant &&
5457 cast<ConstantSDNode>(LHS)->isNullValue()) {
5458 DoXform = true;
5459 } else if (CC != ISD::SETCC_INVALID &&
5460 RHS.getOpcode() == ISD::Constant &&
5461 cast<ConstantSDNode>(RHS)->isNullValue()) {
5462 std::swap(LHS, RHS);
5463 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005464 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00005465 Op0.getOperand(0).getValueType();
5466 bool isInt = OpVT.isInteger();
5467 CC = ISD::getSetCCInverse(CC, isInt);
5468
5469 if (!TLI.isCondCodeLegal(CC, OpVT))
5470 return SDValue(); // Inverse operator isn't legal.
5471
5472 DoXform = true;
5473 InvCC = true;
5474 }
5475
5476 if (DoXform) {
5477 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
5478 if (isSlctCC)
5479 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
5480 Slct.getOperand(0), Slct.getOperand(1), CC);
5481 SDValue CCOp = Slct.getOperand(0);
5482 if (InvCC)
5483 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
5484 CCOp.getOperand(0), CCOp.getOperand(1), CC);
5485 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
5486 CCOp, OtherOp, Result);
5487 }
5488 return SDValue();
5489}
5490
Bob Wilson3d5792a2010-07-29 20:34:14 +00005491/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
5492/// operands N0 and N1. This is a helper for PerformADDCombine that is
5493/// called with the default operands, and if that fails, with commuted
5494/// operands.
5495static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
5496 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005497 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
5498 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
5499 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
5500 if (Result.getNode()) return Result;
5501 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00005502 return SDValue();
5503}
5504
Bob Wilson3d5792a2010-07-29 20:34:14 +00005505/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
5506///
5507static SDValue PerformADDCombine(SDNode *N,
5508 TargetLowering::DAGCombinerInfo &DCI) {
5509 SDValue N0 = N->getOperand(0);
5510 SDValue N1 = N->getOperand(1);
5511
5512 // First try with the default operand order.
5513 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI);
5514 if (Result.getNode())
5515 return Result;
5516
5517 // If that didn't work, try again with the operands commuted.
5518 return PerformADDCombineWithOperands(N, N1, N0, DCI);
5519}
5520
Chris Lattnerd1980a52009-03-12 06:52:53 +00005521/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00005522///
Chris Lattnerd1980a52009-03-12 06:52:53 +00005523static SDValue PerformSUBCombine(SDNode *N,
5524 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005525 SDValue N0 = N->getOperand(0);
5526 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00005527
Chris Lattnerd1980a52009-03-12 06:52:53 +00005528 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
5529 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
5530 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
5531 if (Result.getNode()) return Result;
5532 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00005533
Chris Lattnerd1980a52009-03-12 06:52:53 +00005534 return SDValue();
5535}
5536
Evan Cheng463d3582011-03-31 19:38:48 +00005537/// PerformVMULCombine
5538/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
5539/// special multiplier accumulator forwarding.
5540/// vmul d3, d0, d2
5541/// vmla d3, d1, d2
5542/// is faster than
5543/// vadd d3, d0, d1
5544/// vmul d3, d3, d2
5545static SDValue PerformVMULCombine(SDNode *N,
5546 TargetLowering::DAGCombinerInfo &DCI,
5547 const ARMSubtarget *Subtarget) {
5548 if (!Subtarget->hasVMLxForwarding())
5549 return SDValue();
5550
5551 SelectionDAG &DAG = DCI.DAG;
5552 SDValue N0 = N->getOperand(0);
5553 SDValue N1 = N->getOperand(1);
5554 unsigned Opcode = N0.getOpcode();
5555 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5556 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
5557 Opcode = N0.getOpcode();
5558 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5559 Opcode != ISD::FADD && Opcode != ISD::FSUB)
5560 return SDValue();
5561 std::swap(N0, N1);
5562 }
5563
5564 EVT VT = N->getValueType(0);
5565 DebugLoc DL = N->getDebugLoc();
5566 SDValue N00 = N0->getOperand(0);
5567 SDValue N01 = N0->getOperand(1);
5568 return DAG.getNode(Opcode, DL, VT,
5569 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
5570 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
5571}
5572
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005573static SDValue PerformMULCombine(SDNode *N,
5574 TargetLowering::DAGCombinerInfo &DCI,
5575 const ARMSubtarget *Subtarget) {
5576 SelectionDAG &DAG = DCI.DAG;
5577
5578 if (Subtarget->isThumb1Only())
5579 return SDValue();
5580
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005581 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5582 return SDValue();
5583
5584 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00005585 if (VT.is64BitVector() || VT.is128BitVector())
5586 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005587 if (VT != MVT::i32)
5588 return SDValue();
5589
5590 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
5591 if (!C)
5592 return SDValue();
5593
5594 uint64_t MulAmt = C->getZExtValue();
5595 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
5596 ShiftAmt = ShiftAmt & (32 - 1);
5597 SDValue V = N->getOperand(0);
5598 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005599
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005600 SDValue Res;
5601 MulAmt >>= ShiftAmt;
5602 if (isPowerOf2_32(MulAmt - 1)) {
5603 // (mul x, 2^N + 1) => (add (shl x, N), x)
5604 Res = DAG.getNode(ISD::ADD, DL, VT,
5605 V, DAG.getNode(ISD::SHL, DL, VT,
5606 V, DAG.getConstant(Log2_32(MulAmt-1),
5607 MVT::i32)));
5608 } else if (isPowerOf2_32(MulAmt + 1)) {
5609 // (mul x, 2^N - 1) => (sub (shl x, N), x)
5610 Res = DAG.getNode(ISD::SUB, DL, VT,
5611 DAG.getNode(ISD::SHL, DL, VT,
5612 V, DAG.getConstant(Log2_32(MulAmt+1),
5613 MVT::i32)),
5614 V);
5615 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005616 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005617
5618 if (ShiftAmt != 0)
5619 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
5620 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005621
5622 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005623 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005624 return SDValue();
5625}
5626
Owen Anderson080c0922010-11-05 19:27:46 +00005627static SDValue PerformANDCombine(SDNode *N,
5628 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00005629
Owen Anderson080c0922010-11-05 19:27:46 +00005630 // Attempt to use immediate-form VBIC
5631 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5632 DebugLoc dl = N->getDebugLoc();
5633 EVT VT = N->getValueType(0);
5634 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005635
Tanya Lattner0433b212011-04-07 15:24:20 +00005636 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5637 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005638
Owen Anderson080c0922010-11-05 19:27:46 +00005639 APInt SplatBits, SplatUndef;
5640 unsigned SplatBitSize;
5641 bool HasAnyUndefs;
5642 if (BVN &&
5643 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5644 if (SplatBitSize <= 64) {
5645 EVT VbicVT;
5646 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
5647 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005648 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005649 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00005650 if (Val.getNode()) {
5651 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005652 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00005653 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005654 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00005655 }
5656 }
5657 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005658
Owen Anderson080c0922010-11-05 19:27:46 +00005659 return SDValue();
5660}
5661
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005662/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
5663static SDValue PerformORCombine(SDNode *N,
5664 TargetLowering::DAGCombinerInfo &DCI,
5665 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00005666 // Attempt to use immediate-form VORR
5667 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5668 DebugLoc dl = N->getDebugLoc();
5669 EVT VT = N->getValueType(0);
5670 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005671
Tanya Lattner0433b212011-04-07 15:24:20 +00005672 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5673 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005674
Owen Anderson60f48702010-11-03 23:15:26 +00005675 APInt SplatBits, SplatUndef;
5676 unsigned SplatBitSize;
5677 bool HasAnyUndefs;
5678 if (BVN && Subtarget->hasNEON() &&
5679 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5680 if (SplatBitSize <= 64) {
5681 EVT VorrVT;
5682 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
5683 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005684 DAG, VorrVT, VT.is128BitVector(),
5685 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00005686 if (Val.getNode()) {
5687 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005688 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00005689 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005690 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00005691 }
5692 }
5693 }
5694
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005695 SDValue N0 = N->getOperand(0);
5696 if (N0.getOpcode() != ISD::AND)
5697 return SDValue();
5698 SDValue N1 = N->getOperand(1);
5699
5700 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
5701 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
5702 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
5703 APInt SplatUndef;
5704 unsigned SplatBitSize;
5705 bool HasAnyUndefs;
5706
5707 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
5708 APInt SplatBits0;
5709 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
5710 HasAnyUndefs) && !HasAnyUndefs) {
5711 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
5712 APInt SplatBits1;
5713 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
5714 HasAnyUndefs) && !HasAnyUndefs &&
5715 SplatBits0 == ~SplatBits1) {
5716 // Canonicalize the vector type to make instruction selection simpler.
5717 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
5718 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
5719 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00005720 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005721 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
5722 }
5723 }
5724 }
5725
Jim Grosbach54238562010-07-17 03:30:54 +00005726 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
5727 // reasonable.
5728
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005729 // BFI is only available on V6T2+
5730 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
5731 return SDValue();
5732
Jim Grosbach54238562010-07-17 03:30:54 +00005733 DebugLoc DL = N->getDebugLoc();
5734 // 1) or (and A, mask), val => ARMbfi A, val, mask
5735 // iff (val & mask) == val
5736 //
5737 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
5738 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005739 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005740 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005741 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005742 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005743
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005744 if (VT != MVT::i32)
5745 return SDValue();
5746
Evan Cheng30fb13f2010-12-13 20:32:54 +00005747 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00005748
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005749 // The value and the mask need to be constants so we can verify this is
5750 // actually a bitfield set. If the mask is 0xffff, we can do better
5751 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00005752 SDValue MaskOp = N0.getOperand(1);
5753 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
5754 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005755 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005756 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005757 if (Mask == 0xffff)
5758 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005759 SDValue Res;
5760 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005761 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
5762 if (N1C) {
5763 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005764 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00005765 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005766
Evan Chenga9688c42010-12-11 04:11:38 +00005767 if (ARM::isBitFieldInvertedMask(Mask)) {
5768 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005769
Evan Cheng30fb13f2010-12-13 20:32:54 +00005770 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00005771 DAG.getConstant(Val, MVT::i32),
5772 DAG.getConstant(Mask, MVT::i32));
5773
5774 // Do not add new nodes to DAG combiner worklist.
5775 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005776 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005777 }
Jim Grosbach54238562010-07-17 03:30:54 +00005778 } else if (N1.getOpcode() == ISD::AND) {
5779 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005780 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5781 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00005782 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005783 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005784
Eric Christopher29aeed12011-03-26 01:21:03 +00005785 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
5786 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00005787 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005788 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005789 // The pack halfword instruction works better for masks that fit it,
5790 // so use that when it's available.
5791 if (Subtarget->hasT2ExtractPack() &&
5792 (Mask == 0xffff || Mask == 0xffff0000))
5793 return SDValue();
5794 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00005795 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00005796 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00005797 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00005798 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00005799 DAG.getConstant(Mask, MVT::i32));
5800 // Do not add new nodes to DAG combiner worklist.
5801 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005802 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005803 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005804 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005805 // The pack halfword instruction works better for masks that fit it,
5806 // so use that when it's available.
5807 if (Subtarget->hasT2ExtractPack() &&
5808 (Mask2 == 0xffff || Mask2 == 0xffff0000))
5809 return SDValue();
5810 // 2b
5811 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005812 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00005813 DAG.getConstant(lsb, MVT::i32));
5814 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00005815 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00005816 // Do not add new nodes to DAG combiner worklist.
5817 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005818 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005819 }
5820 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005821
Evan Cheng30fb13f2010-12-13 20:32:54 +00005822 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
5823 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
5824 ARM::isBitFieldInvertedMask(~Mask)) {
5825 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
5826 // where lsb(mask) == #shamt and masked bits of B are known zero.
5827 SDValue ShAmt = N00.getOperand(1);
5828 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
5829 unsigned LSB = CountTrailingZeros_32(Mask);
5830 if (ShAmtC != LSB)
5831 return SDValue();
5832
5833 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
5834 DAG.getConstant(~Mask, MVT::i32));
5835
5836 // Do not add new nodes to DAG combiner worklist.
5837 DCI.CombineTo(N, Res, false);
5838 }
5839
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005840 return SDValue();
5841}
5842
Evan Cheng0c1aec12010-12-14 03:22:07 +00005843/// PerformBFICombine - (bfi A, (and B, C1), C2) -> (bfi A, B, C2) iff
5844/// C1 & C2 == C1.
5845static SDValue PerformBFICombine(SDNode *N,
5846 TargetLowering::DAGCombinerInfo &DCI) {
5847 SDValue N1 = N->getOperand(1);
5848 if (N1.getOpcode() == ISD::AND) {
5849 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5850 if (!N11C)
5851 return SDValue();
5852 unsigned Mask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
5853 unsigned Mask2 = N11C->getZExtValue();
5854 if ((Mask & Mask2) == Mask2)
5855 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
5856 N->getOperand(0), N1.getOperand(0),
5857 N->getOperand(2));
5858 }
5859 return SDValue();
5860}
5861
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005862/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
5863/// ARMISD::VMOVRRD.
5864static SDValue PerformVMOVRRDCombine(SDNode *N,
5865 TargetLowering::DAGCombinerInfo &DCI) {
5866 // vmovrrd(vmovdrr x, y) -> x,y
5867 SDValue InDouble = N->getOperand(0);
5868 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
5869 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00005870
5871 // vmovrrd(load f64) -> (load i32), (load i32)
5872 SDNode *InNode = InDouble.getNode();
5873 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
5874 InNode->getValueType(0) == MVT::f64 &&
5875 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
5876 !cast<LoadSDNode>(InNode)->isVolatile()) {
5877 // TODO: Should this be done for non-FrameIndex operands?
5878 LoadSDNode *LD = cast<LoadSDNode>(InNode);
5879
5880 SelectionDAG &DAG = DCI.DAG;
5881 DebugLoc DL = LD->getDebugLoc();
5882 SDValue BasePtr = LD->getBasePtr();
5883 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
5884 LD->getPointerInfo(), LD->isVolatile(),
5885 LD->isNonTemporal(), LD->getAlignment());
5886
5887 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
5888 DAG.getConstant(4, MVT::i32));
5889 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
5890 LD->getPointerInfo(), LD->isVolatile(),
5891 LD->isNonTemporal(),
5892 std::min(4U, LD->getAlignment() / 2));
5893
5894 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
5895 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
5896 DCI.RemoveFromWorklist(LD);
5897 DAG.DeleteNode(LD);
5898 return Result;
5899 }
5900
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005901 return SDValue();
5902}
5903
5904/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
5905/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
5906static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
5907 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
5908 SDValue Op0 = N->getOperand(0);
5909 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005910 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005911 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005912 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005913 Op1 = Op1.getOperand(0);
5914 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
5915 Op0.getNode() == Op1.getNode() &&
5916 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005917 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005918 N->getValueType(0), Op0.getOperand(0));
5919 return SDValue();
5920}
5921
Bob Wilson31600902010-12-21 06:43:19 +00005922/// PerformSTORECombine - Target-specific dag combine xforms for
5923/// ISD::STORE.
5924static SDValue PerformSTORECombine(SDNode *N,
5925 TargetLowering::DAGCombinerInfo &DCI) {
5926 // Bitcast an i64 store extracted from a vector to f64.
5927 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5928 StoreSDNode *St = cast<StoreSDNode>(N);
5929 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00005930 if (!ISD::isNormalStore(St) || St->isVolatile())
5931 return SDValue();
5932
5933 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
5934 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
5935 SelectionDAG &DAG = DCI.DAG;
5936 DebugLoc DL = St->getDebugLoc();
5937 SDValue BasePtr = St->getBasePtr();
5938 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
5939 StVal.getNode()->getOperand(0), BasePtr,
5940 St->getPointerInfo(), St->isVolatile(),
5941 St->isNonTemporal(), St->getAlignment());
5942
5943 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
5944 DAG.getConstant(4, MVT::i32));
5945 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
5946 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
5947 St->isNonTemporal(),
5948 std::min(4U, St->getAlignment() / 2));
5949 }
5950
5951 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00005952 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5953 return SDValue();
5954
5955 SelectionDAG &DAG = DCI.DAG;
5956 DebugLoc dl = StVal.getDebugLoc();
5957 SDValue IntVec = StVal.getOperand(0);
5958 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5959 IntVec.getValueType().getVectorNumElements());
5960 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
5961 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5962 Vec, StVal.getOperand(1));
5963 dl = N->getDebugLoc();
5964 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
5965 // Make the DAGCombiner fold the bitcasts.
5966 DCI.AddToWorklist(Vec.getNode());
5967 DCI.AddToWorklist(ExtElt.getNode());
5968 DCI.AddToWorklist(V.getNode());
5969 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
5970 St->getPointerInfo(), St->isVolatile(),
5971 St->isNonTemporal(), St->getAlignment(),
5972 St->getTBAAInfo());
5973}
5974
5975/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
5976/// are normal, non-volatile loads. If so, it is profitable to bitcast an
5977/// i64 vector to have f64 elements, since the value can then be loaded
5978/// directly into a VFP register.
5979static bool hasNormalLoadOperand(SDNode *N) {
5980 unsigned NumElts = N->getValueType(0).getVectorNumElements();
5981 for (unsigned i = 0; i < NumElts; ++i) {
5982 SDNode *Elt = N->getOperand(i).getNode();
5983 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
5984 return true;
5985 }
5986 return false;
5987}
5988
Bob Wilson75f02882010-09-17 22:59:05 +00005989/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
5990/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00005991static SDValue PerformBUILD_VECTORCombine(SDNode *N,
5992 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00005993 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
5994 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
5995 // into a pair of GPRs, which is fine when the value is used as a scalar,
5996 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00005997 SelectionDAG &DAG = DCI.DAG;
5998 if (N->getNumOperands() == 2) {
5999 SDValue RV = PerformVMOVDRRCombine(N, DAG);
6000 if (RV.getNode())
6001 return RV;
6002 }
Bob Wilson75f02882010-09-17 22:59:05 +00006003
Bob Wilson31600902010-12-21 06:43:19 +00006004 // Load i64 elements as f64 values so that type legalization does not split
6005 // them up into i32 values.
6006 EVT VT = N->getValueType(0);
6007 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
6008 return SDValue();
6009 DebugLoc dl = N->getDebugLoc();
6010 SmallVector<SDValue, 8> Ops;
6011 unsigned NumElts = VT.getVectorNumElements();
6012 for (unsigned i = 0; i < NumElts; ++i) {
6013 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
6014 Ops.push_back(V);
6015 // Make the DAGCombiner fold the bitcast.
6016 DCI.AddToWorklist(V.getNode());
6017 }
6018 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
6019 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
6020 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
6021}
6022
6023/// PerformInsertEltCombine - Target-specific dag combine xforms for
6024/// ISD::INSERT_VECTOR_ELT.
6025static SDValue PerformInsertEltCombine(SDNode *N,
6026 TargetLowering::DAGCombinerInfo &DCI) {
6027 // Bitcast an i64 load inserted into a vector to f64.
6028 // Otherwise, the i64 value will be legalized to a pair of i32 values.
6029 EVT VT = N->getValueType(0);
6030 SDNode *Elt = N->getOperand(1).getNode();
6031 if (VT.getVectorElementType() != MVT::i64 ||
6032 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
6033 return SDValue();
6034
6035 SelectionDAG &DAG = DCI.DAG;
6036 DebugLoc dl = N->getDebugLoc();
6037 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
6038 VT.getVectorNumElements());
6039 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
6040 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
6041 // Make the DAGCombiner fold the bitcasts.
6042 DCI.AddToWorklist(Vec.getNode());
6043 DCI.AddToWorklist(V.getNode());
6044 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
6045 Vec, V, N->getOperand(2));
6046 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00006047}
6048
Bob Wilsonf20700c2010-10-27 20:38:28 +00006049/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
6050/// ISD::VECTOR_SHUFFLE.
6051static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
6052 // The LLVM shufflevector instruction does not require the shuffle mask
6053 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
6054 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
6055 // operands do not match the mask length, they are extended by concatenating
6056 // them with undef vectors. That is probably the right thing for other
6057 // targets, but for NEON it is better to concatenate two double-register
6058 // size vector operands into a single quad-register size vector. Do that
6059 // transformation here:
6060 // shuffle(concat(v1, undef), concat(v2, undef)) ->
6061 // shuffle(concat(v1, v2), undef)
6062 SDValue Op0 = N->getOperand(0);
6063 SDValue Op1 = N->getOperand(1);
6064 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
6065 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
6066 Op0.getNumOperands() != 2 ||
6067 Op1.getNumOperands() != 2)
6068 return SDValue();
6069 SDValue Concat0Op1 = Op0.getOperand(1);
6070 SDValue Concat1Op1 = Op1.getOperand(1);
6071 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
6072 Concat1Op1.getOpcode() != ISD::UNDEF)
6073 return SDValue();
6074 // Skip the transformation if any of the types are illegal.
6075 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6076 EVT VT = N->getValueType(0);
6077 if (!TLI.isTypeLegal(VT) ||
6078 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
6079 !TLI.isTypeLegal(Concat1Op1.getValueType()))
6080 return SDValue();
6081
6082 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
6083 Op0.getOperand(0), Op1.getOperand(0));
6084 // Translate the shuffle mask.
6085 SmallVector<int, 16> NewMask;
6086 unsigned NumElts = VT.getVectorNumElements();
6087 unsigned HalfElts = NumElts/2;
6088 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
6089 for (unsigned n = 0; n < NumElts; ++n) {
6090 int MaskElt = SVN->getMaskElt(n);
6091 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006092 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00006093 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006094 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00006095 NewElt = HalfElts + MaskElt - NumElts;
6096 NewMask.push_back(NewElt);
6097 }
6098 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
6099 DAG.getUNDEF(VT), NewMask.data());
6100}
6101
Bob Wilson1c3ef902011-02-07 17:43:21 +00006102/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
6103/// NEON load/store intrinsics to merge base address updates.
6104static SDValue CombineBaseUpdate(SDNode *N,
6105 TargetLowering::DAGCombinerInfo &DCI) {
6106 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6107 return SDValue();
6108
6109 SelectionDAG &DAG = DCI.DAG;
6110 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
6111 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
6112 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
6113 SDValue Addr = N->getOperand(AddrOpIdx);
6114
6115 // Search for a use of the address operand that is an increment.
6116 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
6117 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
6118 SDNode *User = *UI;
6119 if (User->getOpcode() != ISD::ADD ||
6120 UI.getUse().getResNo() != Addr.getResNo())
6121 continue;
6122
6123 // Check that the add is independent of the load/store. Otherwise, folding
6124 // it would create a cycle.
6125 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
6126 continue;
6127
6128 // Find the new opcode for the updating load/store.
6129 bool isLoad = true;
6130 bool isLaneOp = false;
6131 unsigned NewOpc = 0;
6132 unsigned NumVecs = 0;
6133 if (isIntrinsic) {
6134 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
6135 switch (IntNo) {
6136 default: assert(0 && "unexpected intrinsic for Neon base update");
6137 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
6138 NumVecs = 1; break;
6139 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
6140 NumVecs = 2; break;
6141 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
6142 NumVecs = 3; break;
6143 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
6144 NumVecs = 4; break;
6145 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
6146 NumVecs = 2; isLaneOp = true; break;
6147 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
6148 NumVecs = 3; isLaneOp = true; break;
6149 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
6150 NumVecs = 4; isLaneOp = true; break;
6151 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
6152 NumVecs = 1; isLoad = false; break;
6153 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
6154 NumVecs = 2; isLoad = false; break;
6155 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
6156 NumVecs = 3; isLoad = false; break;
6157 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
6158 NumVecs = 4; isLoad = false; break;
6159 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
6160 NumVecs = 2; isLoad = false; isLaneOp = true; break;
6161 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
6162 NumVecs = 3; isLoad = false; isLaneOp = true; break;
6163 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
6164 NumVecs = 4; isLoad = false; isLaneOp = true; break;
6165 }
6166 } else {
6167 isLaneOp = true;
6168 switch (N->getOpcode()) {
6169 default: assert(0 && "unexpected opcode for Neon base update");
6170 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
6171 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
6172 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
6173 }
6174 }
6175
6176 // Find the size of memory referenced by the load/store.
6177 EVT VecTy;
6178 if (isLoad)
6179 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00006180 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00006181 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
6182 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
6183 if (isLaneOp)
6184 NumBytes /= VecTy.getVectorNumElements();
6185
6186 // If the increment is a constant, it must match the memory ref size.
6187 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
6188 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
6189 uint64_t IncVal = CInc->getZExtValue();
6190 if (IncVal != NumBytes)
6191 continue;
6192 } else if (NumBytes >= 3 * 16) {
6193 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
6194 // separate instructions that make it harder to use a non-constant update.
6195 continue;
6196 }
6197
6198 // Create the new updating load/store node.
6199 EVT Tys[6];
6200 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
6201 unsigned n;
6202 for (n = 0; n < NumResultVecs; ++n)
6203 Tys[n] = VecTy;
6204 Tys[n++] = MVT::i32;
6205 Tys[n] = MVT::Other;
6206 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
6207 SmallVector<SDValue, 8> Ops;
6208 Ops.push_back(N->getOperand(0)); // incoming chain
6209 Ops.push_back(N->getOperand(AddrOpIdx));
6210 Ops.push_back(Inc);
6211 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
6212 Ops.push_back(N->getOperand(i));
6213 }
6214 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
6215 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
6216 Ops.data(), Ops.size(),
6217 MemInt->getMemoryVT(),
6218 MemInt->getMemOperand());
6219
6220 // Update the uses.
6221 std::vector<SDValue> NewResults;
6222 for (unsigned i = 0; i < NumResultVecs; ++i) {
6223 NewResults.push_back(SDValue(UpdN.getNode(), i));
6224 }
6225 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
6226 DCI.CombineTo(N, NewResults);
6227 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
6228
6229 break;
Owen Anderson76706012011-04-05 21:48:57 +00006230 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00006231 return SDValue();
6232}
6233
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006234/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
6235/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
6236/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
6237/// return true.
6238static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
6239 SelectionDAG &DAG = DCI.DAG;
6240 EVT VT = N->getValueType(0);
6241 // vldN-dup instructions only support 64-bit vectors for N > 1.
6242 if (!VT.is64BitVector())
6243 return false;
6244
6245 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
6246 SDNode *VLD = N->getOperand(0).getNode();
6247 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
6248 return false;
6249 unsigned NumVecs = 0;
6250 unsigned NewOpc = 0;
6251 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
6252 if (IntNo == Intrinsic::arm_neon_vld2lane) {
6253 NumVecs = 2;
6254 NewOpc = ARMISD::VLD2DUP;
6255 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
6256 NumVecs = 3;
6257 NewOpc = ARMISD::VLD3DUP;
6258 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
6259 NumVecs = 4;
6260 NewOpc = ARMISD::VLD4DUP;
6261 } else {
6262 return false;
6263 }
6264
6265 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
6266 // numbers match the load.
6267 unsigned VLDLaneNo =
6268 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
6269 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6270 UI != UE; ++UI) {
6271 // Ignore uses of the chain result.
6272 if (UI.getUse().getResNo() == NumVecs)
6273 continue;
6274 SDNode *User = *UI;
6275 if (User->getOpcode() != ARMISD::VDUPLANE ||
6276 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
6277 return false;
6278 }
6279
6280 // Create the vldN-dup node.
6281 EVT Tys[5];
6282 unsigned n;
6283 for (n = 0; n < NumVecs; ++n)
6284 Tys[n] = VT;
6285 Tys[n] = MVT::Other;
6286 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
6287 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
6288 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
6289 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
6290 Ops, 2, VLDMemInt->getMemoryVT(),
6291 VLDMemInt->getMemOperand());
6292
6293 // Update the uses.
6294 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6295 UI != UE; ++UI) {
6296 unsigned ResNo = UI.getUse().getResNo();
6297 // Ignore uses of the chain result.
6298 if (ResNo == NumVecs)
6299 continue;
6300 SDNode *User = *UI;
6301 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
6302 }
6303
6304 // Now the vldN-lane intrinsic is dead except for its chain result.
6305 // Update uses of the chain.
6306 std::vector<SDValue> VLDDupResults;
6307 for (unsigned n = 0; n < NumVecs; ++n)
6308 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
6309 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
6310 DCI.CombineTo(VLD, VLDDupResults);
6311
6312 return true;
6313}
6314
Bob Wilson9e82bf12010-07-14 01:22:12 +00006315/// PerformVDUPLANECombine - Target-specific dag combine xforms for
6316/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006317static SDValue PerformVDUPLANECombine(SDNode *N,
6318 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00006319 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006320
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006321 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
6322 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
6323 if (CombineVLDDUP(N, DCI))
6324 return SDValue(N, 0);
6325
6326 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
6327 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006328 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006329 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00006330 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006331 return SDValue();
6332
6333 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
6334 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
6335 // The canonical VMOV for a zero vector uses a 32-bit element size.
6336 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6337 unsigned EltBits;
6338 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
6339 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006340 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006341 if (EltSize > VT.getVectorElementType().getSizeInBits())
6342 return SDValue();
6343
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006344 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006345}
6346
Bob Wilson5bafff32009-06-22 23:27:02 +00006347/// getVShiftImm - Check if this is a valid build_vector for the immediate
6348/// operand of a vector shift operation, where all the elements of the
6349/// build_vector must have the same constant integer value.
6350static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
6351 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006352 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00006353 Op = Op.getOperand(0);
6354 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
6355 APInt SplatBits, SplatUndef;
6356 unsigned SplatBitSize;
6357 bool HasAnyUndefs;
6358 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
6359 HasAnyUndefs, ElementBits) ||
6360 SplatBitSize > ElementBits)
6361 return false;
6362 Cnt = SplatBits.getSExtValue();
6363 return true;
6364}
6365
6366/// isVShiftLImm - Check if this is a valid build_vector for the immediate
6367/// operand of a vector shift left operation. That value must be in the range:
6368/// 0 <= Value < ElementBits for a left shift; or
6369/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006370static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006371 assert(VT.isVector() && "vector shift count is not a vector type");
6372 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6373 if (! getVShiftImm(Op, ElementBits, Cnt))
6374 return false;
6375 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
6376}
6377
6378/// isVShiftRImm - Check if this is a valid build_vector for the immediate
6379/// operand of a vector shift right operation. For a shift opcode, the value
6380/// is positive, but for an intrinsic the value count must be negative. The
6381/// absolute value must be in the range:
6382/// 1 <= |Value| <= ElementBits for a right shift; or
6383/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006384static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00006385 int64_t &Cnt) {
6386 assert(VT.isVector() && "vector shift count is not a vector type");
6387 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6388 if (! getVShiftImm(Op, ElementBits, Cnt))
6389 return false;
6390 if (isIntrinsic)
6391 Cnt = -Cnt;
6392 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
6393}
6394
6395/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
6396static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
6397 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
6398 switch (IntNo) {
6399 default:
6400 // Don't do anything for most intrinsics.
6401 break;
6402
6403 // Vector shifts: check for immediate versions and lower them.
6404 // Note: This is done during DAG combining instead of DAG legalizing because
6405 // the build_vectors for 64-bit vector element shift counts are generally
6406 // not legal, and it is hard to see their values after they get legalized to
6407 // loads from a constant pool.
6408 case Intrinsic::arm_neon_vshifts:
6409 case Intrinsic::arm_neon_vshiftu:
6410 case Intrinsic::arm_neon_vshiftls:
6411 case Intrinsic::arm_neon_vshiftlu:
6412 case Intrinsic::arm_neon_vshiftn:
6413 case Intrinsic::arm_neon_vrshifts:
6414 case Intrinsic::arm_neon_vrshiftu:
6415 case Intrinsic::arm_neon_vrshiftn:
6416 case Intrinsic::arm_neon_vqshifts:
6417 case Intrinsic::arm_neon_vqshiftu:
6418 case Intrinsic::arm_neon_vqshiftsu:
6419 case Intrinsic::arm_neon_vqshiftns:
6420 case Intrinsic::arm_neon_vqshiftnu:
6421 case Intrinsic::arm_neon_vqshiftnsu:
6422 case Intrinsic::arm_neon_vqrshiftns:
6423 case Intrinsic::arm_neon_vqrshiftnu:
6424 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00006425 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006426 int64_t Cnt;
6427 unsigned VShiftOpc = 0;
6428
6429 switch (IntNo) {
6430 case Intrinsic::arm_neon_vshifts:
6431 case Intrinsic::arm_neon_vshiftu:
6432 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
6433 VShiftOpc = ARMISD::VSHL;
6434 break;
6435 }
6436 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
6437 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
6438 ARMISD::VSHRs : ARMISD::VSHRu);
6439 break;
6440 }
6441 return SDValue();
6442
6443 case Intrinsic::arm_neon_vshiftls:
6444 case Intrinsic::arm_neon_vshiftlu:
6445 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
6446 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006447 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006448
6449 case Intrinsic::arm_neon_vrshifts:
6450 case Intrinsic::arm_neon_vrshiftu:
6451 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
6452 break;
6453 return SDValue();
6454
6455 case Intrinsic::arm_neon_vqshifts:
6456 case Intrinsic::arm_neon_vqshiftu:
6457 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6458 break;
6459 return SDValue();
6460
6461 case Intrinsic::arm_neon_vqshiftsu:
6462 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6463 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006464 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006465
6466 case Intrinsic::arm_neon_vshiftn:
6467 case Intrinsic::arm_neon_vrshiftn:
6468 case Intrinsic::arm_neon_vqshiftns:
6469 case Intrinsic::arm_neon_vqshiftnu:
6470 case Intrinsic::arm_neon_vqshiftnsu:
6471 case Intrinsic::arm_neon_vqrshiftns:
6472 case Intrinsic::arm_neon_vqrshiftnu:
6473 case Intrinsic::arm_neon_vqrshiftnsu:
6474 // Narrowing shifts require an immediate right shift.
6475 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
6476 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00006477 llvm_unreachable("invalid shift count for narrowing vector shift "
6478 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006479
6480 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006481 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00006482 }
6483
6484 switch (IntNo) {
6485 case Intrinsic::arm_neon_vshifts:
6486 case Intrinsic::arm_neon_vshiftu:
6487 // Opcode already set above.
6488 break;
6489 case Intrinsic::arm_neon_vshiftls:
6490 case Intrinsic::arm_neon_vshiftlu:
6491 if (Cnt == VT.getVectorElementType().getSizeInBits())
6492 VShiftOpc = ARMISD::VSHLLi;
6493 else
6494 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
6495 ARMISD::VSHLLs : ARMISD::VSHLLu);
6496 break;
6497 case Intrinsic::arm_neon_vshiftn:
6498 VShiftOpc = ARMISD::VSHRN; break;
6499 case Intrinsic::arm_neon_vrshifts:
6500 VShiftOpc = ARMISD::VRSHRs; break;
6501 case Intrinsic::arm_neon_vrshiftu:
6502 VShiftOpc = ARMISD::VRSHRu; break;
6503 case Intrinsic::arm_neon_vrshiftn:
6504 VShiftOpc = ARMISD::VRSHRN; break;
6505 case Intrinsic::arm_neon_vqshifts:
6506 VShiftOpc = ARMISD::VQSHLs; break;
6507 case Intrinsic::arm_neon_vqshiftu:
6508 VShiftOpc = ARMISD::VQSHLu; break;
6509 case Intrinsic::arm_neon_vqshiftsu:
6510 VShiftOpc = ARMISD::VQSHLsu; break;
6511 case Intrinsic::arm_neon_vqshiftns:
6512 VShiftOpc = ARMISD::VQSHRNs; break;
6513 case Intrinsic::arm_neon_vqshiftnu:
6514 VShiftOpc = ARMISD::VQSHRNu; break;
6515 case Intrinsic::arm_neon_vqshiftnsu:
6516 VShiftOpc = ARMISD::VQSHRNsu; break;
6517 case Intrinsic::arm_neon_vqrshiftns:
6518 VShiftOpc = ARMISD::VQRSHRNs; break;
6519 case Intrinsic::arm_neon_vqrshiftnu:
6520 VShiftOpc = ARMISD::VQRSHRNu; break;
6521 case Intrinsic::arm_neon_vqrshiftnsu:
6522 VShiftOpc = ARMISD::VQRSHRNsu; break;
6523 }
6524
6525 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006526 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006527 }
6528
6529 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00006530 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006531 int64_t Cnt;
6532 unsigned VShiftOpc = 0;
6533
6534 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
6535 VShiftOpc = ARMISD::VSLI;
6536 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
6537 VShiftOpc = ARMISD::VSRI;
6538 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006539 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006540 }
6541
6542 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
6543 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00006544 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006545 }
6546
6547 case Intrinsic::arm_neon_vqrshifts:
6548 case Intrinsic::arm_neon_vqrshiftu:
6549 // No immediate versions of these to check for.
6550 break;
6551 }
6552
6553 return SDValue();
6554}
6555
6556/// PerformShiftCombine - Checks for immediate versions of vector shifts and
6557/// lowers them. As with the vector shift intrinsics, this is done during DAG
6558/// combining instead of DAG legalizing because the build_vectors for 64-bit
6559/// vector element shift counts are generally not legal, and it is hard to see
6560/// their values after they get legalized to loads from a constant pool.
6561static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
6562 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00006563 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00006564
6565 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00006566 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6567 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00006568 return SDValue();
6569
6570 assert(ST->hasNEON() && "unexpected vector shift");
6571 int64_t Cnt;
6572
6573 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006574 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006575
6576 case ISD::SHL:
6577 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
6578 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006579 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006580 break;
6581
6582 case ISD::SRA:
6583 case ISD::SRL:
6584 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
6585 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
6586 ARMISD::VSHRs : ARMISD::VSHRu);
6587 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006588 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006589 }
6590 }
6591 return SDValue();
6592}
6593
6594/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
6595/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
6596static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
6597 const ARMSubtarget *ST) {
6598 SDValue N0 = N->getOperand(0);
6599
6600 // Check for sign- and zero-extensions of vector extract operations of 8-
6601 // and 16-bit vector elements. NEON supports these directly. They are
6602 // handled during DAG combining because type legalization will promote them
6603 // to 32-bit types and it is messy to recognize the operations after that.
6604 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
6605 SDValue Vec = N0.getOperand(0);
6606 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006607 EVT VT = N->getValueType(0);
6608 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006609 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6610
Owen Anderson825b72b2009-08-11 20:47:22 +00006611 if (VT == MVT::i32 &&
6612 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00006613 TLI.isTypeLegal(Vec.getValueType()) &&
6614 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006615
6616 unsigned Opc = 0;
6617 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006618 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006619 case ISD::SIGN_EXTEND:
6620 Opc = ARMISD::VGETLANEs;
6621 break;
6622 case ISD::ZERO_EXTEND:
6623 case ISD::ANY_EXTEND:
6624 Opc = ARMISD::VGETLANEu;
6625 break;
6626 }
6627 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
6628 }
6629 }
6630
6631 return SDValue();
6632}
6633
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006634/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
6635/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
6636static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
6637 const ARMSubtarget *ST) {
6638 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00006639 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006640 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
6641 // a NaN; only do the transformation when it matches that behavior.
6642
6643 // For now only do this when using NEON for FP operations; if using VFP, it
6644 // is not obvious that the benefit outweighs the cost of switching to the
6645 // NEON pipeline.
6646 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
6647 N->getValueType(0) != MVT::f32)
6648 return SDValue();
6649
6650 SDValue CondLHS = N->getOperand(0);
6651 SDValue CondRHS = N->getOperand(1);
6652 SDValue LHS = N->getOperand(2);
6653 SDValue RHS = N->getOperand(3);
6654 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
6655
6656 unsigned Opcode = 0;
6657 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00006658 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006659 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00006660 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006661 IsReversed = true ; // x CC y ? y : x
6662 } else {
6663 return SDValue();
6664 }
6665
Bob Wilsone742bb52010-02-24 22:15:53 +00006666 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006667 switch (CC) {
6668 default: break;
6669 case ISD::SETOLT:
6670 case ISD::SETOLE:
6671 case ISD::SETLT:
6672 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006673 case ISD::SETULT:
6674 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006675 // If LHS is NaN, an ordered comparison will be false and the result will
6676 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
6677 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6678 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
6679 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6680 break;
6681 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
6682 // will return -0, so vmin can only be used for unsafe math or if one of
6683 // the operands is known to be nonzero.
6684 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
6685 !UnsafeFPMath &&
6686 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6687 break;
6688 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006689 break;
6690
6691 case ISD::SETOGT:
6692 case ISD::SETOGE:
6693 case ISD::SETGT:
6694 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006695 case ISD::SETUGT:
6696 case ISD::SETUGE:
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 vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
6699 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6700 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
6701 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6702 break;
6703 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
6704 // will return +0, so vmax can only be used for unsafe math or if one of
6705 // the operands is known to be nonzero.
6706 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
6707 !UnsafeFPMath &&
6708 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6709 break;
6710 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006711 break;
6712 }
6713
6714 if (!Opcode)
6715 return SDValue();
6716 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
6717}
6718
Dan Gohman475871a2008-07-27 21:46:04 +00006719SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006720 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006721 switch (N->getOpcode()) {
6722 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006723 case ISD::ADD: return PerformADDCombine(N, DCI);
6724 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006725 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006726 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00006727 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00006728 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00006729 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006730 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00006731 case ISD::STORE: return PerformSTORECombine(N, DCI);
6732 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
6733 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00006734 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006735 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006736 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00006737 case ISD::SHL:
6738 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006739 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00006740 case ISD::SIGN_EXTEND:
6741 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006742 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
6743 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Bob Wilson1c3ef902011-02-07 17:43:21 +00006744 case ARMISD::VLD2DUP:
6745 case ARMISD::VLD3DUP:
6746 case ARMISD::VLD4DUP:
6747 return CombineBaseUpdate(N, DCI);
6748 case ISD::INTRINSIC_VOID:
6749 case ISD::INTRINSIC_W_CHAIN:
6750 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
6751 case Intrinsic::arm_neon_vld1:
6752 case Intrinsic::arm_neon_vld2:
6753 case Intrinsic::arm_neon_vld3:
6754 case Intrinsic::arm_neon_vld4:
6755 case Intrinsic::arm_neon_vld2lane:
6756 case Intrinsic::arm_neon_vld3lane:
6757 case Intrinsic::arm_neon_vld4lane:
6758 case Intrinsic::arm_neon_vst1:
6759 case Intrinsic::arm_neon_vst2:
6760 case Intrinsic::arm_neon_vst3:
6761 case Intrinsic::arm_neon_vst4:
6762 case Intrinsic::arm_neon_vst2lane:
6763 case Intrinsic::arm_neon_vst3lane:
6764 case Intrinsic::arm_neon_vst4lane:
6765 return CombineBaseUpdate(N, DCI);
6766 default: break;
6767 }
6768 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006769 }
Dan Gohman475871a2008-07-27 21:46:04 +00006770 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006771}
6772
Evan Cheng31959b12011-02-02 01:06:55 +00006773bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
6774 EVT VT) const {
6775 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
6776}
6777
Bill Wendlingaf566342009-08-15 21:21:19 +00006778bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00006779 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00006780 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00006781
6782 switch (VT.getSimpleVT().SimpleTy) {
6783 default:
6784 return false;
6785 case MVT::i8:
6786 case MVT::i16:
6787 case MVT::i32:
6788 return true;
6789 // FIXME: VLD1 etc with standard alignment is legal.
6790 }
6791}
6792
Evan Chenge6c835f2009-08-14 20:09:37 +00006793static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
6794 if (V < 0)
6795 return false;
6796
6797 unsigned Scale = 1;
6798 switch (VT.getSimpleVT().SimpleTy) {
6799 default: return false;
6800 case MVT::i1:
6801 case MVT::i8:
6802 // Scale == 1;
6803 break;
6804 case MVT::i16:
6805 // Scale == 2;
6806 Scale = 2;
6807 break;
6808 case MVT::i32:
6809 // Scale == 4;
6810 Scale = 4;
6811 break;
6812 }
6813
6814 if ((V & (Scale - 1)) != 0)
6815 return false;
6816 V /= Scale;
6817 return V == (V & ((1LL << 5) - 1));
6818}
6819
6820static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
6821 const ARMSubtarget *Subtarget) {
6822 bool isNeg = false;
6823 if (V < 0) {
6824 isNeg = true;
6825 V = - V;
6826 }
6827
6828 switch (VT.getSimpleVT().SimpleTy) {
6829 default: return false;
6830 case MVT::i1:
6831 case MVT::i8:
6832 case MVT::i16:
6833 case MVT::i32:
6834 // + imm12 or - imm8
6835 if (isNeg)
6836 return V == (V & ((1LL << 8) - 1));
6837 return V == (V & ((1LL << 12) - 1));
6838 case MVT::f32:
6839 case MVT::f64:
6840 // Same as ARM mode. FIXME: NEON?
6841 if (!Subtarget->hasVFP2())
6842 return false;
6843 if ((V & 3) != 0)
6844 return false;
6845 V >>= 2;
6846 return V == (V & ((1LL << 8) - 1));
6847 }
6848}
6849
Evan Chengb01fad62007-03-12 23:30:29 +00006850/// isLegalAddressImmediate - Return true if the integer value can be used
6851/// as the offset of the target addressing mode for load / store of the
6852/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00006853static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006854 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00006855 if (V == 0)
6856 return true;
6857
Evan Cheng65011532009-03-09 19:15:00 +00006858 if (!VT.isSimple())
6859 return false;
6860
Evan Chenge6c835f2009-08-14 20:09:37 +00006861 if (Subtarget->isThumb1Only())
6862 return isLegalT1AddressImmediate(V, VT);
6863 else if (Subtarget->isThumb2())
6864 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00006865
Evan Chenge6c835f2009-08-14 20:09:37 +00006866 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00006867 if (V < 0)
6868 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00006869 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00006870 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006871 case MVT::i1:
6872 case MVT::i8:
6873 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00006874 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006875 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006876 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00006877 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006878 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006879 case MVT::f32:
6880 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00006881 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00006882 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00006883 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00006884 return false;
6885 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006886 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00006887 }
Evan Chenga8e29892007-01-19 07:51:42 +00006888}
6889
Evan Chenge6c835f2009-08-14 20:09:37 +00006890bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
6891 EVT VT) const {
6892 int Scale = AM.Scale;
6893 if (Scale < 0)
6894 return false;
6895
6896 switch (VT.getSimpleVT().SimpleTy) {
6897 default: return false;
6898 case MVT::i1:
6899 case MVT::i8:
6900 case MVT::i16:
6901 case MVT::i32:
6902 if (Scale == 1)
6903 return true;
6904 // r + r << imm
6905 Scale = Scale & ~1;
6906 return Scale == 2 || Scale == 4 || Scale == 8;
6907 case MVT::i64:
6908 // r + r
6909 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
6910 return true;
6911 return false;
6912 case MVT::isVoid:
6913 // Note, we allow "void" uses (basically, uses that aren't loads or
6914 // stores), because arm allows folding a scale into many arithmetic
6915 // operations. This should be made more precise and revisited later.
6916
6917 // Allow r << imm, but the imm has to be a multiple of two.
6918 if (Scale & 1) return false;
6919 return isPowerOf2_32(Scale);
6920 }
6921}
6922
Chris Lattner37caf8c2007-04-09 23:33:39 +00006923/// isLegalAddressingMode - Return true if the addressing mode represented
6924/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006925bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006926 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006927 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00006928 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00006929 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006930
Chris Lattner37caf8c2007-04-09 23:33:39 +00006931 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006932 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006933 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006934
Chris Lattner37caf8c2007-04-09 23:33:39 +00006935 switch (AM.Scale) {
6936 case 0: // no scale reg, must be "r+i" or "r", or "i".
6937 break;
6938 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00006939 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00006940 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006941 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00006942 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006943 // ARM doesn't support any R+R*scale+imm addr modes.
6944 if (AM.BaseOffs)
6945 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006946
Bob Wilson2c7dab12009-04-08 17:55:28 +00006947 if (!VT.isSimple())
6948 return false;
6949
Evan Chenge6c835f2009-08-14 20:09:37 +00006950 if (Subtarget->isThumb2())
6951 return isLegalT2ScaledAddressingMode(AM, VT);
6952
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006953 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00006954 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00006955 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006956 case MVT::i1:
6957 case MVT::i8:
6958 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006959 if (Scale < 0) Scale = -Scale;
6960 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006961 return true;
6962 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00006963 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006964 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00006965 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00006966 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006967 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006968 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00006969 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006970
Owen Anderson825b72b2009-08-11 20:47:22 +00006971 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00006972 // Note, we allow "void" uses (basically, uses that aren't loads or
6973 // stores), because arm allows folding a scale into many arithmetic
6974 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006975
Chris Lattner37caf8c2007-04-09 23:33:39 +00006976 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00006977 if (Scale & 1) return false;
6978 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00006979 }
6980 break;
Evan Chengb01fad62007-03-12 23:30:29 +00006981 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00006982 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00006983}
6984
Evan Cheng77e47512009-11-11 19:05:52 +00006985/// isLegalICmpImmediate - Return true if the specified immediate is legal
6986/// icmp immediate, that is the target has icmp instructions which can compare
6987/// a register against the immediate without having to materialize the
6988/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00006989bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00006990 if (!Subtarget->isThumb())
6991 return ARM_AM::getSOImmVal(Imm) != -1;
6992 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00006993 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00006994 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00006995}
6996
Dan Gohmancca82142011-05-03 00:46:49 +00006997/// isLegalAddImmediate - Return true if the specified immediate is legal
6998/// add immediate, that is the target has add instructions which can add
6999/// a register with the immediate without having to materialize the
7000/// immediate into a register.
7001bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
7002 return ARM_AM::getSOImmVal(Imm) != -1;
7003}
7004
Owen Andersone50ed302009-08-10 22:56:29 +00007005static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007006 bool isSEXTLoad, SDValue &Base,
7007 SDValue &Offset, bool &isInc,
7008 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00007009 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7010 return false;
7011
Owen Anderson825b72b2009-08-11 20:47:22 +00007012 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00007013 // AddressingMode 3
7014 Base = Ptr->getOperand(0);
7015 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007016 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007017 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007018 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007019 isInc = false;
7020 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7021 return true;
7022 }
7023 }
7024 isInc = (Ptr->getOpcode() == ISD::ADD);
7025 Offset = Ptr->getOperand(1);
7026 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00007027 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00007028 // AddressingMode 2
7029 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007030 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007031 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007032 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007033 isInc = false;
7034 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7035 Base = Ptr->getOperand(0);
7036 return true;
7037 }
7038 }
7039
7040 if (Ptr->getOpcode() == ISD::ADD) {
7041 isInc = true;
7042 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
7043 if (ShOpcVal != ARM_AM::no_shift) {
7044 Base = Ptr->getOperand(1);
7045 Offset = Ptr->getOperand(0);
7046 } else {
7047 Base = Ptr->getOperand(0);
7048 Offset = Ptr->getOperand(1);
7049 }
7050 return true;
7051 }
7052
7053 isInc = (Ptr->getOpcode() == ISD::ADD);
7054 Base = Ptr->getOperand(0);
7055 Offset = Ptr->getOperand(1);
7056 return true;
7057 }
7058
Jim Grosbache5165492009-11-09 00:11:35 +00007059 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00007060 return false;
7061}
7062
Owen Andersone50ed302009-08-10 22:56:29 +00007063static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007064 bool isSEXTLoad, SDValue &Base,
7065 SDValue &Offset, bool &isInc,
7066 SelectionDAG &DAG) {
7067 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7068 return false;
7069
7070 Base = Ptr->getOperand(0);
7071 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
7072 int RHSC = (int)RHS->getZExtValue();
7073 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
7074 assert(Ptr->getOpcode() == ISD::ADD);
7075 isInc = false;
7076 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7077 return true;
7078 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
7079 isInc = Ptr->getOpcode() == ISD::ADD;
7080 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
7081 return true;
7082 }
7083 }
7084
7085 return false;
7086}
7087
Evan Chenga8e29892007-01-19 07:51:42 +00007088/// getPreIndexedAddressParts - returns true by value, base pointer and
7089/// offset pointer and addressing mode by reference if the node's address
7090/// can be legally represented as pre-indexed load / store address.
7091bool
Dan Gohman475871a2008-07-27 21:46:04 +00007092ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
7093 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007094 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007095 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007096 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007097 return false;
7098
Owen Andersone50ed302009-08-10 22:56:29 +00007099 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007100 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007101 bool isSEXTLoad = false;
7102 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
7103 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007104 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007105 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7106 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
7107 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007108 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007109 } else
7110 return false;
7111
7112 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007113 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007114 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007115 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
7116 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007117 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007118 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00007119 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00007120 if (!isLegal)
7121 return false;
7122
7123 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
7124 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007125}
7126
7127/// getPostIndexedAddressParts - returns true by value, base pointer and
7128/// offset pointer and addressing mode by reference if this node can be
7129/// combined with a load / store to form a post-indexed load / store.
7130bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00007131 SDValue &Base,
7132 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007133 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007134 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007135 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007136 return false;
7137
Owen Andersone50ed302009-08-10 22:56:29 +00007138 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007139 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007140 bool isSEXTLoad = false;
7141 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007142 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007143 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007144 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7145 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007146 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007147 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007148 } else
7149 return false;
7150
7151 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007152 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007153 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007154 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00007155 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007156 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007157 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
7158 isInc, DAG);
7159 if (!isLegal)
7160 return false;
7161
Evan Cheng28dad2a2010-05-18 21:31:17 +00007162 if (Ptr != Base) {
7163 // Swap base ptr and offset to catch more post-index load / store when
7164 // it's legal. In Thumb2 mode, offset must be an immediate.
7165 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
7166 !Subtarget->isThumb2())
7167 std::swap(Base, Offset);
7168
7169 // Post-indexed load / store update the base pointer.
7170 if (Ptr != Base)
7171 return false;
7172 }
7173
Evan Chenge88d5ce2009-07-02 07:28:31 +00007174 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
7175 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007176}
7177
Dan Gohman475871a2008-07-27 21:46:04 +00007178void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007179 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00007180 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007181 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007182 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00007183 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007184 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007185 switch (Op.getOpcode()) {
7186 default: break;
7187 case ARMISD::CMOV: {
7188 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00007189 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007190 if (KnownZero == 0 && KnownOne == 0) return;
7191
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007192 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00007193 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
7194 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007195 KnownZero &= KnownZeroRHS;
7196 KnownOne &= KnownOneRHS;
7197 return;
7198 }
7199 }
7200}
7201
7202//===----------------------------------------------------------------------===//
7203// ARM Inline Assembly Support
7204//===----------------------------------------------------------------------===//
7205
Evan Cheng55d42002011-01-08 01:24:27 +00007206bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
7207 // Looking for "rev" which is V6+.
7208 if (!Subtarget->hasV6Ops())
7209 return false;
7210
7211 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
7212 std::string AsmStr = IA->getAsmString();
7213 SmallVector<StringRef, 4> AsmPieces;
7214 SplitString(AsmStr, AsmPieces, ";\n");
7215
7216 switch (AsmPieces.size()) {
7217 default: return false;
7218 case 1:
7219 AsmStr = AsmPieces[0];
7220 AsmPieces.clear();
7221 SplitString(AsmStr, AsmPieces, " \t,");
7222
7223 // rev $0, $1
7224 if (AsmPieces.size() == 3 &&
7225 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
7226 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
7227 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
7228 if (Ty && Ty->getBitWidth() == 32)
7229 return IntrinsicLowering::LowerToByteSwap(CI);
7230 }
7231 break;
7232 }
7233
7234 return false;
7235}
7236
Evan Chenga8e29892007-01-19 07:51:42 +00007237/// getConstraintType - Given a constraint letter, return the type of
7238/// constraint it is for this target.
7239ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00007240ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
7241 if (Constraint.size() == 1) {
7242 switch (Constraint[0]) {
7243 default: break;
7244 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007245 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00007246 }
Evan Chenga8e29892007-01-19 07:51:42 +00007247 }
Chris Lattner4234f572007-03-25 02:14:49 +00007248 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00007249}
7250
John Thompson44ab89e2010-10-29 17:29:13 +00007251/// Examine constraint type and operand type and determine a weight value.
7252/// This object must already have been set up with the operand type
7253/// and the current alternative constraint selected.
7254TargetLowering::ConstraintWeight
7255ARMTargetLowering::getSingleConstraintMatchWeight(
7256 AsmOperandInfo &info, const char *constraint) const {
7257 ConstraintWeight weight = CW_Invalid;
7258 Value *CallOperandVal = info.CallOperandVal;
7259 // If we don't have a value, we can't do a match,
7260 // but allow it at the lowest weight.
7261 if (CallOperandVal == NULL)
7262 return CW_Default;
7263 const Type *type = CallOperandVal->getType();
7264 // Look at the constraint type.
7265 switch (*constraint) {
7266 default:
7267 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
7268 break;
7269 case 'l':
7270 if (type->isIntegerTy()) {
7271 if (Subtarget->isThumb())
7272 weight = CW_SpecificReg;
7273 else
7274 weight = CW_Register;
7275 }
7276 break;
7277 case 'w':
7278 if (type->isFloatingPointTy())
7279 weight = CW_Register;
7280 break;
7281 }
7282 return weight;
7283}
7284
Bob Wilson2dc4f542009-03-20 22:42:55 +00007285std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00007286ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007287 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007288 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007289 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00007290 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007291 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007292 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007293 return std::make_pair(0U, ARM::tGPRRegisterClass);
7294 else
7295 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007296 case 'r':
7297 return std::make_pair(0U, ARM::GPRRegisterClass);
7298 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007299 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007300 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00007301 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007302 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00007303 if (VT.getSizeInBits() == 128)
7304 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007305 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007306 }
7307 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007308 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00007309 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007310
Evan Chenga8e29892007-01-19 07:51:42 +00007311 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7312}
7313
7314std::vector<unsigned> ARMTargetLowering::
7315getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007316 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007317 if (Constraint.size() != 1)
7318 return std::vector<unsigned>();
7319
7320 switch (Constraint[0]) { // GCC ARM Constraint Letters
7321 default: break;
7322 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007323 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
7324 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
7325 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007326 case 'r':
7327 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
7328 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
7329 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
7330 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007331 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007332 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007333 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
7334 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
7335 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
7336 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
7337 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
7338 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
7339 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
7340 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00007341 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007342 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
7343 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
7344 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
7345 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00007346 if (VT.getSizeInBits() == 128)
7347 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
7348 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007349 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007350 }
7351
7352 return std::vector<unsigned>();
7353}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007354
7355/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7356/// vector. If it is invalid, don't add anything to Ops.
7357void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
7358 char Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007359 std::vector<SDValue>&Ops,
7360 SelectionDAG &DAG) const {
7361 SDValue Result(0, 0);
7362
7363 switch (Constraint) {
7364 default: break;
7365 case 'I': case 'J': case 'K': case 'L':
7366 case 'M': case 'N': case 'O':
7367 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
7368 if (!C)
7369 return;
7370
7371 int64_t CVal64 = C->getSExtValue();
7372 int CVal = (int) CVal64;
7373 // None of these constraints allow values larger than 32 bits. Check
7374 // that the value fits in an int.
7375 if (CVal != CVal64)
7376 return;
7377
7378 switch (Constraint) {
7379 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007380 if (Subtarget->isThumb1Only()) {
7381 // This must be a constant between 0 and 255, for ADD
7382 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007383 if (CVal >= 0 && CVal <= 255)
7384 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007385 } else if (Subtarget->isThumb2()) {
7386 // A constant that can be used as an immediate value in a
7387 // data-processing instruction.
7388 if (ARM_AM::getT2SOImmVal(CVal) != -1)
7389 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007390 } else {
7391 // A constant that can be used as an immediate value in a
7392 // data-processing instruction.
7393 if (ARM_AM::getSOImmVal(CVal) != -1)
7394 break;
7395 }
7396 return;
7397
7398 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007399 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007400 // This must be a constant between -255 and -1, for negated ADD
7401 // immediates. This can be used in GCC with an "n" modifier that
7402 // prints the negated value, for use with SUB instructions. It is
7403 // not useful otherwise but is implemented for compatibility.
7404 if (CVal >= -255 && CVal <= -1)
7405 break;
7406 } else {
7407 // This must be a constant between -4095 and 4095. It is not clear
7408 // what this constraint is intended for. Implemented for
7409 // compatibility with GCC.
7410 if (CVal >= -4095 && CVal <= 4095)
7411 break;
7412 }
7413 return;
7414
7415 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007416 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007417 // A 32-bit value where only one byte has a nonzero value. Exclude
7418 // zero to match GCC. This constraint is used by GCC internally for
7419 // constants that can be loaded with a move/shift combination.
7420 // It is not useful otherwise but is implemented for compatibility.
7421 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
7422 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007423 } else if (Subtarget->isThumb2()) {
7424 // A constant whose bitwise inverse can be used as an immediate
7425 // value in a data-processing instruction. This can be used in GCC
7426 // with a "B" modifier that prints the inverted value, for use with
7427 // BIC and MVN instructions. It is not useful otherwise but is
7428 // implemented for compatibility.
7429 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
7430 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007431 } else {
7432 // A constant whose bitwise inverse can be used as an immediate
7433 // value in a data-processing instruction. This can be used in GCC
7434 // with a "B" modifier that prints the inverted value, for use with
7435 // BIC and MVN instructions. It is not useful otherwise but is
7436 // implemented for compatibility.
7437 if (ARM_AM::getSOImmVal(~CVal) != -1)
7438 break;
7439 }
7440 return;
7441
7442 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007443 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007444 // This must be a constant between -7 and 7,
7445 // for 3-operand ADD/SUB immediate instructions.
7446 if (CVal >= -7 && CVal < 7)
7447 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007448 } else if (Subtarget->isThumb2()) {
7449 // A constant whose negation can be used as an immediate value in a
7450 // data-processing instruction. This can be used in GCC with an "n"
7451 // modifier that prints the negated value, for use with SUB
7452 // instructions. It is not useful otherwise but is implemented for
7453 // compatibility.
7454 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
7455 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007456 } else {
7457 // A constant whose negation can be used as an immediate value in a
7458 // data-processing instruction. This can be used in GCC with an "n"
7459 // modifier that prints the negated value, for use with SUB
7460 // instructions. It is not useful otherwise but is implemented for
7461 // compatibility.
7462 if (ARM_AM::getSOImmVal(-CVal) != -1)
7463 break;
7464 }
7465 return;
7466
7467 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007468 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007469 // This must be a multiple of 4 between 0 and 1020, for
7470 // ADD sp + immediate.
7471 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
7472 break;
7473 } else {
7474 // A power of two or a constant between 0 and 32. This is used in
7475 // GCC for the shift amount on shifted register operands, but it is
7476 // useful in general for any shift amounts.
7477 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
7478 break;
7479 }
7480 return;
7481
7482 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007483 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007484 // This must be a constant between 0 and 31, for shift amounts.
7485 if (CVal >= 0 && CVal <= 31)
7486 break;
7487 }
7488 return;
7489
7490 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007491 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007492 // This must be a multiple of 4 between -508 and 508, for
7493 // ADD/SUB sp = sp + immediate.
7494 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
7495 break;
7496 }
7497 return;
7498 }
7499 Result = DAG.getTargetConstant(CVal, Op.getValueType());
7500 break;
7501 }
7502
7503 if (Result.getNode()) {
7504 Ops.push_back(Result);
7505 return;
7506 }
Dale Johannesen1784d162010-06-25 21:55:36 +00007507 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007508}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00007509
7510bool
7511ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7512 // The ARM target isn't yet aware of offsets.
7513 return false;
7514}
Evan Cheng39382422009-10-28 01:44:26 +00007515
7516int ARM::getVFPf32Imm(const APFloat &FPImm) {
7517 APInt Imm = FPImm.bitcastToAPInt();
7518 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
7519 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
7520 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
7521
7522 // We can handle 4 bits of mantissa.
7523 // mantissa = (16+UInt(e:f:g:h))/16.
7524 if (Mantissa & 0x7ffff)
7525 return -1;
7526 Mantissa >>= 19;
7527 if ((Mantissa & 0xf) != Mantissa)
7528 return -1;
7529
7530 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7531 if (Exp < -3 || Exp > 4)
7532 return -1;
7533 Exp = ((Exp+3) & 0x7) ^ 4;
7534
7535 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7536}
7537
7538int ARM::getVFPf64Imm(const APFloat &FPImm) {
7539 APInt Imm = FPImm.bitcastToAPInt();
7540 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
7541 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
7542 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
7543
7544 // We can handle 4 bits of mantissa.
7545 // mantissa = (16+UInt(e:f:g:h))/16.
7546 if (Mantissa & 0xffffffffffffLL)
7547 return -1;
7548 Mantissa >>= 48;
7549 if ((Mantissa & 0xf) != Mantissa)
7550 return -1;
7551
7552 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7553 if (Exp < -3 || Exp > 4)
7554 return -1;
7555 Exp = ((Exp+3) & 0x7) ^ 4;
7556
7557 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7558}
7559
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007560bool ARM::isBitFieldInvertedMask(unsigned v) {
7561 if (v == 0xffffffff)
7562 return 0;
7563 // there can be 1's on either or both "outsides", all the "inside"
7564 // bits must be 0's
7565 unsigned int lsb = 0, msb = 31;
7566 while (v & (1 << msb)) --msb;
7567 while (v & (1 << lsb)) ++lsb;
7568 for (unsigned int i = lsb; i <= msb; ++i) {
7569 if (v & (1 << i))
7570 return 0;
7571 }
7572 return 1;
7573}
7574
Evan Cheng39382422009-10-28 01:44:26 +00007575/// isFPImmLegal - Returns true if the target can instruction select the
7576/// specified FP immediate natively. If false, the legalizer will
7577/// materialize the FP immediate as a load from a constant pool.
7578bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
7579 if (!Subtarget->hasVFP3())
7580 return false;
7581 if (VT == MVT::f32)
7582 return ARM::getVFPf32Imm(Imm) != -1;
7583 if (VT == MVT::f64)
7584 return ARM::getVFPf64Imm(Imm) != -1;
7585 return false;
7586}
Bob Wilson65ffec42010-09-21 17:56:22 +00007587
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007588/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00007589/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
7590/// specified in the intrinsic calls.
7591bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
7592 const CallInst &I,
7593 unsigned Intrinsic) const {
7594 switch (Intrinsic) {
7595 case Intrinsic::arm_neon_vld1:
7596 case Intrinsic::arm_neon_vld2:
7597 case Intrinsic::arm_neon_vld3:
7598 case Intrinsic::arm_neon_vld4:
7599 case Intrinsic::arm_neon_vld2lane:
7600 case Intrinsic::arm_neon_vld3lane:
7601 case Intrinsic::arm_neon_vld4lane: {
7602 Info.opc = ISD::INTRINSIC_W_CHAIN;
7603 // Conservatively set memVT to the entire set of vectors loaded.
7604 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
7605 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7606 Info.ptrVal = I.getArgOperand(0);
7607 Info.offset = 0;
7608 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7609 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7610 Info.vol = false; // volatile loads with NEON intrinsics not supported
7611 Info.readMem = true;
7612 Info.writeMem = false;
7613 return true;
7614 }
7615 case Intrinsic::arm_neon_vst1:
7616 case Intrinsic::arm_neon_vst2:
7617 case Intrinsic::arm_neon_vst3:
7618 case Intrinsic::arm_neon_vst4:
7619 case Intrinsic::arm_neon_vst2lane:
7620 case Intrinsic::arm_neon_vst3lane:
7621 case Intrinsic::arm_neon_vst4lane: {
7622 Info.opc = ISD::INTRINSIC_VOID;
7623 // Conservatively set memVT to the entire set of vectors stored.
7624 unsigned NumElts = 0;
7625 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
7626 const Type *ArgTy = I.getArgOperand(ArgI)->getType();
7627 if (!ArgTy->isVectorTy())
7628 break;
7629 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
7630 }
7631 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7632 Info.ptrVal = I.getArgOperand(0);
7633 Info.offset = 0;
7634 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7635 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7636 Info.vol = false; // volatile stores with NEON intrinsics not supported
7637 Info.readMem = false;
7638 Info.writeMem = true;
7639 return true;
7640 }
7641 default:
7642 break;
7643 }
7644
7645 return false;
7646}