blob: 5502ca434cc93591b0c646b0dbe8f83a01554e42 [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
Evan Cheng8e23e812011-04-01 00:42:02 +000075static cl::opt<bool>
76UseDivMod("arm-divmod-libcall", cl::Hidden,
77 cl::desc("Use __{u}divmod libcalls for div / rem pairs"),
78 cl::init(false));
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
Evan Cheng8e23e812011-04-01 00:42:02 +0000401 if (UseDivMod) {
402 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
403 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
404 }
405
David Goodwinf1daf7d2009-07-08 23:10:31 +0000406 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000407 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000408 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000409 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000410 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000412 if (!Subtarget->isFPOnlySP())
413 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000414
Owen Anderson825b72b2009-08-11 20:47:22 +0000415 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000416 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000417
418 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 addDRTypeForNEON(MVT::v2f32);
420 addDRTypeForNEON(MVT::v8i8);
421 addDRTypeForNEON(MVT::v4i16);
422 addDRTypeForNEON(MVT::v2i32);
423 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000424
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 addQRTypeForNEON(MVT::v4f32);
426 addQRTypeForNEON(MVT::v2f64);
427 addQRTypeForNEON(MVT::v16i8);
428 addQRTypeForNEON(MVT::v8i16);
429 addQRTypeForNEON(MVT::v4i32);
430 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000431
Bob Wilson74dc72e2009-09-15 23:55:57 +0000432 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
433 // neither Neon nor VFP support any arithmetic operations on it.
434 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
435 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
436 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
437 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
438 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
439 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
440 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
441 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
442 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
443 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
444 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
445 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
446 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
447 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
448 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
449 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
450 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
451 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
452 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
453 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
454 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
455 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
456 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
457 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
458
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000459 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
460
Bob Wilson642b3292009-09-16 00:32:15 +0000461 // Neon does not support some operations on v1i64 and v2i64 types.
462 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000463 // Custom handling for some quad-vector types to detect VMULL.
464 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
465 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
466 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000467 // Custom handling for some vector types to avoid expensive expansions
468 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
469 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
470 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
471 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000472 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
473 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000474 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
475 // a destination type that is wider than the source.
476 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
477 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000478
Bob Wilson1c3ef902011-02-07 17:43:21 +0000479 setTargetDAGCombine(ISD::INTRINSIC_VOID);
480 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000481 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
482 setTargetDAGCombine(ISD::SHL);
483 setTargetDAGCombine(ISD::SRL);
484 setTargetDAGCombine(ISD::SRA);
485 setTargetDAGCombine(ISD::SIGN_EXTEND);
486 setTargetDAGCombine(ISD::ZERO_EXTEND);
487 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000488 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000489 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000490 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000491 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
492 setTargetDAGCombine(ISD::STORE);
Bob Wilson5bafff32009-06-22 23:27:02 +0000493 }
494
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000495 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000496
497 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000498 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000499
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000500 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000501 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000502
Evan Chenga8e29892007-01-19 07:51:42 +0000503 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000504 if (!Subtarget->isThumb1Only()) {
505 for (unsigned im = (unsigned)ISD::PRE_INC;
506 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000507 setIndexedLoadAction(im, MVT::i1, Legal);
508 setIndexedLoadAction(im, MVT::i8, Legal);
509 setIndexedLoadAction(im, MVT::i16, Legal);
510 setIndexedLoadAction(im, MVT::i32, Legal);
511 setIndexedStoreAction(im, MVT::i1, Legal);
512 setIndexedStoreAction(im, MVT::i8, Legal);
513 setIndexedStoreAction(im, MVT::i16, Legal);
514 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000515 }
Evan Chenga8e29892007-01-19 07:51:42 +0000516 }
517
518 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000519 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000520 setOperationAction(ISD::MUL, MVT::i64, Expand);
521 setOperationAction(ISD::MULHU, MVT::i32, Expand);
522 setOperationAction(ISD::MULHS, MVT::i32, Expand);
523 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
524 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000525 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000526 setOperationAction(ISD::MUL, MVT::i64, Expand);
527 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000528 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000529 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000530 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000531 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000532 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000533 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000534 setOperationAction(ISD::SRL, MVT::i64, Custom);
535 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000536
537 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000539 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000540 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000541 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000542 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000543
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000544 // Only ARMv6 has BSWAP.
545 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000546 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000547
Evan Chenga8e29892007-01-19 07:51:42 +0000548 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000549 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000550 // v7M has a hardware divider
551 setOperationAction(ISD::SDIV, MVT::i32, Expand);
552 setOperationAction(ISD::UDIV, MVT::i32, Expand);
553 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000554 setOperationAction(ISD::SREM, MVT::i32, Expand);
555 setOperationAction(ISD::UREM, MVT::i32, Expand);
556 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
557 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000558
Owen Anderson825b72b2009-08-11 20:47:22 +0000559 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
560 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
561 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
562 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000563 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000564
Evan Chengfb3611d2010-05-11 07:26:32 +0000565 setOperationAction(ISD::TRAP, MVT::Other, Legal);
566
Evan Chenga8e29892007-01-19 07:51:42 +0000567 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000568 setOperationAction(ISD::VASTART, MVT::Other, Custom);
569 setOperationAction(ISD::VAARG, MVT::Other, Expand);
570 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
571 setOperationAction(ISD::VAEND, MVT::Other, Expand);
572 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
573 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000574 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000575 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
576 setExceptionPointerRegister(ARM::R0);
577 setExceptionSelectorRegister(ARM::R1);
578
Evan Cheng3a1588a2010-04-15 22:20:34 +0000579 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000580 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
581 // the default expansion.
582 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000583 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000584 // membarrier needs custom lowering; the rest are legal and handled
585 // normally.
586 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
587 } else {
588 // Set them all for expansion, which will force libcalls.
589 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
590 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
591 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
592 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000593 setOperationAction(ISD::ATOMIC_SWAP, MVT::i8, Expand);
594 setOperationAction(ISD::ATOMIC_SWAP, MVT::i16, Expand);
595 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000596 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
597 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
598 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
599 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
600 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
601 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
602 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
603 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
604 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
605 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
606 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
607 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
608 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
609 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
610 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
611 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
612 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
613 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000614 // Since the libcalls include locking, fold in the fences
615 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000616 }
617 // 64-bit versions are always libcalls (for now)
618 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000619 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000620 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
621 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
622 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
623 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
624 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
625 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000626
Evan Cheng416941d2010-11-04 05:19:35 +0000627 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000628
Eli Friedmana2c6f452010-06-26 04:36:50 +0000629 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
630 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000631 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
632 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000633 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000635
Nate Begemand1fb5832010-08-03 21:31:55 +0000636 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000637 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
638 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000639 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000640 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
641 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000642
643 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000644 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000645 if (Subtarget->isTargetDarwin()) {
646 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
647 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000648 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000649 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000650
Owen Anderson825b72b2009-08-11 20:47:22 +0000651 setOperationAction(ISD::SETCC, MVT::i32, Expand);
652 setOperationAction(ISD::SETCC, MVT::f32, Expand);
653 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000654 setOperationAction(ISD::SELECT, MVT::i32, Custom);
655 setOperationAction(ISD::SELECT, MVT::f32, Custom);
656 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000657 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
658 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
659 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000660
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
662 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
663 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
664 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
665 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000666
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000667 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000668 setOperationAction(ISD::FSIN, MVT::f64, Expand);
669 setOperationAction(ISD::FSIN, MVT::f32, Expand);
670 setOperationAction(ISD::FCOS, MVT::f32, Expand);
671 setOperationAction(ISD::FCOS, MVT::f64, Expand);
672 setOperationAction(ISD::FREM, MVT::f64, Expand);
673 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000674 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000675 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
676 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000677 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000678 setOperationAction(ISD::FPOW, MVT::f64, Expand);
679 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000680
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000681 // Various VFP goodness
682 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000683 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
684 if (Subtarget->hasVFP2()) {
685 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
686 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
687 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
688 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
689 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000690 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000691 if (!Subtarget->hasFP16()) {
692 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
693 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000694 }
Evan Cheng110cf482008-04-01 01:50:16 +0000695 }
Evan Chenga8e29892007-01-19 07:51:42 +0000696
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000697 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000698 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000699 setTargetDAGCombine(ISD::ADD);
700 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000701 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000702
Owen Anderson080c0922010-11-05 19:27:46 +0000703 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000704 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000705 if (Subtarget->hasNEON())
706 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000707
Evan Chenga8e29892007-01-19 07:51:42 +0000708 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000709
Evan Chengf7d87ee2010-05-21 00:43:17 +0000710 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
711 setSchedulingPreference(Sched::RegPressure);
712 else
713 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000714
Evan Cheng05219282011-01-06 06:52:41 +0000715 //// temporary - rewrite interface to use type
716 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000717
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000718 // On ARM arguments smaller than 4 bytes are extended, so all arguments
719 // are at least 4 bytes aligned.
720 setMinStackArgumentAlignment(4);
721
Evan Chengfff606d2010-09-24 19:07:23 +0000722 benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000723}
724
Andrew Trick32cec0a2011-01-19 02:35:27 +0000725// FIXME: It might make sense to define the representative register class as the
726// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
727// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
728// SPR's representative would be DPR_VFP2. This should work well if register
729// pressure tracking were modified such that a register use would increment the
730// pressure of the register class's representative and all of it's super
731// classes' representatives transitively. We have not implemented this because
732// of the difficulty prior to coalescing of modeling operand register classes
733// due to the common occurence of cross class copies and subregister insertions
734// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000735std::pair<const TargetRegisterClass*, uint8_t>
736ARMTargetLowering::findRepresentativeClass(EVT VT) const{
737 const TargetRegisterClass *RRC = 0;
738 uint8_t Cost = 1;
739 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000740 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000741 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000742 // Use DPR as representative register class for all floating point
743 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
744 // the cost is 1 for both f32 and f64.
745 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000746 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000747 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000748 // When NEON is used for SP, only half of the register file is available
749 // because operations that define both SP and DP results will be constrained
750 // to the VFP2 class (D0-D15). We currently model this constraint prior to
751 // coalescing by double-counting the SP regs. See the FIXME above.
752 if (Subtarget->useNEONForSinglePrecisionFP())
753 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000754 break;
755 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
756 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000757 RRC = ARM::DPRRegisterClass;
758 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000759 break;
760 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000761 RRC = ARM::DPRRegisterClass;
762 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000763 break;
764 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000765 RRC = ARM::DPRRegisterClass;
766 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000767 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000768 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000769 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000770}
771
Evan Chenga8e29892007-01-19 07:51:42 +0000772const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
773 switch (Opcode) {
774 default: return 0;
775 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000776 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000777 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000778 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
779 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000780 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000781 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
782 case ARMISD::tCALL: return "ARMISD::tCALL";
783 case ARMISD::BRCOND: return "ARMISD::BRCOND";
784 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000785 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000786 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
787 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
788 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000789 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000790 case ARMISD::CMPFP: return "ARMISD::CMPFP";
791 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000792 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000793 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
794 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000795
Jim Grosbach3482c802010-01-18 19:58:49 +0000796 case ARMISD::RBIT: return "ARMISD::RBIT";
797
Bob Wilson76a312b2010-03-19 22:51:32 +0000798 case ARMISD::FTOSI: return "ARMISD::FTOSI";
799 case ARMISD::FTOUI: return "ARMISD::FTOUI";
800 case ARMISD::SITOF: return "ARMISD::SITOF";
801 case ARMISD::UITOF: return "ARMISD::UITOF";
802
Evan Chenga8e29892007-01-19 07:51:42 +0000803 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
804 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
805 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000806
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000807 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
808 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000809
Evan Chengc5942082009-10-28 06:55:03 +0000810 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
811 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000812 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000813
Dale Johannesen51e28e62010-06-03 21:09:53 +0000814 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000815
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000816 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000817
Evan Cheng86198642009-08-07 00:34:42 +0000818 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
819
Jim Grosbach3728e962009-12-10 00:11:09 +0000820 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000821 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000822
Evan Chengdfed19f2010-11-03 06:34:55 +0000823 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
824
Bob Wilson5bafff32009-06-22 23:27:02 +0000825 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000826 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000827 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000828 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
829 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000830 case ARMISD::VCGEU: return "ARMISD::VCGEU";
831 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000832 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
833 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000834 case ARMISD::VCGTU: return "ARMISD::VCGTU";
835 case ARMISD::VTST: return "ARMISD::VTST";
836
837 case ARMISD::VSHL: return "ARMISD::VSHL";
838 case ARMISD::VSHRs: return "ARMISD::VSHRs";
839 case ARMISD::VSHRu: return "ARMISD::VSHRu";
840 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
841 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
842 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
843 case ARMISD::VSHRN: return "ARMISD::VSHRN";
844 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
845 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
846 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
847 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
848 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
849 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
850 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
851 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
852 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
853 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
854 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
855 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
856 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
857 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000858 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000859 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000860 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000861 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000862 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000863 case ARMISD::VREV64: return "ARMISD::VREV64";
864 case ARMISD::VREV32: return "ARMISD::VREV32";
865 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000866 case ARMISD::VZIP: return "ARMISD::VZIP";
867 case ARMISD::VUZP: return "ARMISD::VUZP";
868 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000869 case ARMISD::VTBL1: return "ARMISD::VTBL1";
870 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000871 case ARMISD::VMULLs: return "ARMISD::VMULLs";
872 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000873 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000874 case ARMISD::FMAX: return "ARMISD::FMAX";
875 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000876 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000877 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
878 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000879 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000880 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
881 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
882 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000883 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
884 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
885 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
886 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
887 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
888 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
889 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
890 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
891 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
892 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
893 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
894 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
895 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
896 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
897 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
898 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
899 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000900 }
901}
902
Evan Cheng06b666c2010-05-15 02:18:07 +0000903/// getRegClassFor - Return the register class that should be used for the
904/// specified value type.
905TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
906 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
907 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
908 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000909 if (Subtarget->hasNEON()) {
910 if (VT == MVT::v4i64)
911 return ARM::QQPRRegisterClass;
912 else if (VT == MVT::v8i64)
913 return ARM::QQQQPRRegisterClass;
914 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000915 return TargetLowering::getRegClassFor(VT);
916}
917
Eric Christopherab695882010-07-21 22:26:11 +0000918// Create a fast isel object.
919FastISel *
920ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
921 return ARM::createFastISel(funcInfo);
922}
923
Bill Wendlingb4202b82009-07-01 18:50:55 +0000924/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000925unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Bob Wilsonb5b50572010-07-01 22:26:26 +0000926 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
Bill Wendling20c568f2009-06-30 22:38:32 +0000927}
928
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000929/// getMaximalGlobalOffset - Returns the maximal possible offset which can
930/// be used for loads / stores from the global.
931unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
932 return (Subtarget->isThumb1Only() ? 127 : 4095);
933}
934
Evan Cheng1cc39842010-05-20 23:26:43 +0000935Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000936 unsigned NumVals = N->getNumValues();
937 if (!NumVals)
938 return Sched::RegPressure;
939
940 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000941 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000942 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000943 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000944 if (VT.isFloatingPoint() || VT.isVector())
945 return Sched::Latency;
946 }
Evan Chengc10f5432010-05-28 23:25:23 +0000947
948 if (!N->isMachineOpcode())
949 return Sched::RegPressure;
950
951 // Load are scheduled for latency even if there instruction itinerary
952 // is not available.
953 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
954 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000955
956 if (TID.getNumDefs() == 0)
957 return Sched::RegPressure;
958 if (!Itins->isEmpty() &&
959 Itins->getOperandCycle(TID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000960 return Sched::Latency;
961
Evan Cheng1cc39842010-05-20 23:26:43 +0000962 return Sched::RegPressure;
963}
964
Evan Chenga8e29892007-01-19 07:51:42 +0000965//===----------------------------------------------------------------------===//
966// Lowering Code
967//===----------------------------------------------------------------------===//
968
Evan Chenga8e29892007-01-19 07:51:42 +0000969/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
970static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
971 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000972 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000973 case ISD::SETNE: return ARMCC::NE;
974 case ISD::SETEQ: return ARMCC::EQ;
975 case ISD::SETGT: return ARMCC::GT;
976 case ISD::SETGE: return ARMCC::GE;
977 case ISD::SETLT: return ARMCC::LT;
978 case ISD::SETLE: return ARMCC::LE;
979 case ISD::SETUGT: return ARMCC::HI;
980 case ISD::SETUGE: return ARMCC::HS;
981 case ISD::SETULT: return ARMCC::LO;
982 case ISD::SETULE: return ARMCC::LS;
983 }
984}
985
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000986/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
987static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000988 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000989 CondCode2 = ARMCC::AL;
990 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000991 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000992 case ISD::SETEQ:
993 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
994 case ISD::SETGT:
995 case ISD::SETOGT: CondCode = ARMCC::GT; break;
996 case ISD::SETGE:
997 case ISD::SETOGE: CondCode = ARMCC::GE; break;
998 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000999 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001000 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1001 case ISD::SETO: CondCode = ARMCC::VC; break;
1002 case ISD::SETUO: CondCode = ARMCC::VS; break;
1003 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1004 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1005 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1006 case ISD::SETLT:
1007 case ISD::SETULT: CondCode = ARMCC::LT; break;
1008 case ISD::SETLE:
1009 case ISD::SETULE: CondCode = ARMCC::LE; break;
1010 case ISD::SETNE:
1011 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1012 }
Evan Chenga8e29892007-01-19 07:51:42 +00001013}
1014
Bob Wilson1f595bb2009-04-17 19:07:39 +00001015//===----------------------------------------------------------------------===//
1016// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001017//===----------------------------------------------------------------------===//
1018
1019#include "ARMGenCallingConv.inc"
1020
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001021/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1022/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001023CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001024 bool Return,
1025 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001026 switch (CC) {
1027 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001028 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001029 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001030 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001031 if (!Subtarget->isAAPCS_ABI())
1032 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1033 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1034 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1035 }
1036 // Fallthrough
1037 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001038 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001039 if (!Subtarget->isAAPCS_ABI())
1040 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1041 else if (Subtarget->hasVFP2() &&
1042 FloatABIType == FloatABI::Hard && !isVarArg)
1043 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1044 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1045 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001046 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001047 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001048 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001049 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001050 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001051 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001052 }
1053}
1054
Dan Gohman98ca4f22009-08-05 01:29:28 +00001055/// LowerCallResult - Lower the result values of a call into the
1056/// appropriate copies out of appropriate physical registers.
1057SDValue
1058ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001059 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001060 const SmallVectorImpl<ISD::InputArg> &Ins,
1061 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001062 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001063
Bob Wilson1f595bb2009-04-17 19:07:39 +00001064 // Assign locations to each value returned by this call.
1065 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001066 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001067 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001068 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001069 CCAssignFnForNode(CallConv, /* Return*/ true,
1070 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001071
1072 // Copy all of the result registers out of their specified physreg.
1073 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1074 CCValAssign VA = RVLocs[i];
1075
Bob Wilson80915242009-04-25 00:33:20 +00001076 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001077 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001078 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001079 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001080 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001081 Chain = Lo.getValue(1);
1082 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001083 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001084 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001085 InFlag);
1086 Chain = Hi.getValue(1);
1087 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001088 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001089
Owen Anderson825b72b2009-08-11 20:47:22 +00001090 if (VA.getLocVT() == MVT::v2f64) {
1091 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1092 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1093 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001094
1095 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001096 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001097 Chain = Lo.getValue(1);
1098 InFlag = Lo.getValue(2);
1099 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001100 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001101 Chain = Hi.getValue(1);
1102 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001103 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001104 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1105 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001106 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001107 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001108 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1109 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001110 Chain = Val.getValue(1);
1111 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001112 }
Bob Wilson80915242009-04-25 00:33:20 +00001113
1114 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001115 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001116 case CCValAssign::Full: break;
1117 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001118 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001119 break;
1120 }
1121
Dan Gohman98ca4f22009-08-05 01:29:28 +00001122 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001123 }
1124
Dan Gohman98ca4f22009-08-05 01:29:28 +00001125 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001126}
1127
1128/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1129/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +00001130/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +00001131/// a byval function parameter.
1132/// Sometimes what we are copying is the end of a larger object, the part that
1133/// does not fit in registers.
1134static SDValue
1135CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
1136 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1137 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001138 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001139 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001140 /*isVolatile=*/false, /*AlwaysInline=*/false,
Chris Lattnere72f2022010-09-21 05:40:29 +00001141 MachinePointerInfo(0), MachinePointerInfo(0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001142}
1143
Bob Wilsondee46d72009-04-17 20:35:10 +00001144/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001145SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001146ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1147 SDValue StackPtr, SDValue Arg,
1148 DebugLoc dl, SelectionDAG &DAG,
1149 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001150 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001151 unsigned LocMemOffset = VA.getLocMemOffset();
1152 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1153 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001154 if (Flags.isByVal())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001155 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001156
Bob Wilson1f595bb2009-04-17 19:07:39 +00001157 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001158 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001159 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001160}
1161
Dan Gohman98ca4f22009-08-05 01:29:28 +00001162void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001163 SDValue Chain, SDValue &Arg,
1164 RegsToPassVector &RegsToPass,
1165 CCValAssign &VA, CCValAssign &NextVA,
1166 SDValue &StackPtr,
1167 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001168 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001169
Jim Grosbache5165492009-11-09 00:11:35 +00001170 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001171 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001172 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1173
1174 if (NextVA.isRegLoc())
1175 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1176 else {
1177 assert(NextVA.isMemLoc());
1178 if (StackPtr.getNode() == 0)
1179 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1180
Dan Gohman98ca4f22009-08-05 01:29:28 +00001181 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1182 dl, DAG, NextVA,
1183 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001184 }
1185}
1186
Dan Gohman98ca4f22009-08-05 01:29:28 +00001187/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001188/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1189/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001190SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001191ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001192 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001193 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001194 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001195 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001196 const SmallVectorImpl<ISD::InputArg> &Ins,
1197 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001198 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001199 MachineFunction &MF = DAG.getMachineFunction();
1200 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1201 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001202 // Temporarily disable tail calls so things don't break.
1203 if (!EnableARMTailCalls)
1204 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001205 if (isTailCall) {
1206 // Check if it's really possible to do a tail call.
1207 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1208 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001209 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001210 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1211 // detected sibcalls.
1212 if (isTailCall) {
1213 ++NumTailCalls;
1214 IsSibCall = true;
1215 }
1216 }
Evan Chenga8e29892007-01-19 07:51:42 +00001217
Bob Wilson1f595bb2009-04-17 19:07:39 +00001218 // Analyze operands of the call, assigning locations to each operand.
1219 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001220 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1221 *DAG.getContext());
1222 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001223 CCAssignFnForNode(CallConv, /* Return*/ false,
1224 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001225
Bob Wilson1f595bb2009-04-17 19:07:39 +00001226 // Get a count of how many bytes are to be pushed on the stack.
1227 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001228
Dale Johannesen51e28e62010-06-03 21:09:53 +00001229 // For tail calls, memory operands are available in our caller's stack.
1230 if (IsSibCall)
1231 NumBytes = 0;
1232
Evan Chenga8e29892007-01-19 07:51:42 +00001233 // Adjust the stack pointer for the new arguments...
1234 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001235 if (!IsSibCall)
1236 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001237
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001238 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001239
Bob Wilson5bafff32009-06-22 23:27:02 +00001240 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001241 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001242
Bob Wilson1f595bb2009-04-17 19:07:39 +00001243 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001244 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001245 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1246 i != e;
1247 ++i, ++realArgIdx) {
1248 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001249 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001250 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001251 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001252
Bob Wilson1f595bb2009-04-17 19:07:39 +00001253 // Promote the value if needed.
1254 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001255 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001256 case CCValAssign::Full: break;
1257 case CCValAssign::SExt:
1258 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1259 break;
1260 case CCValAssign::ZExt:
1261 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1262 break;
1263 case CCValAssign::AExt:
1264 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1265 break;
1266 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001267 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001268 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001269 }
1270
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001271 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001272 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001273 if (VA.getLocVT() == MVT::v2f64) {
1274 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1275 DAG.getConstant(0, MVT::i32));
1276 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1277 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001278
Dan Gohman98ca4f22009-08-05 01:29:28 +00001279 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001280 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1281
1282 VA = ArgLocs[++i]; // skip ahead to next loc
1283 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001284 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001285 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1286 } else {
1287 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001288
Dan Gohman98ca4f22009-08-05 01:29:28 +00001289 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1290 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001291 }
1292 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001293 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001294 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001295 }
1296 } else if (VA.isRegLoc()) {
1297 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsf222e592011-02-28 17:17:53 +00001298 } else if (!IsSibCall || isByVal) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001299 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001300
Dan Gohman98ca4f22009-08-05 01:29:28 +00001301 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1302 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001303 }
Evan Chenga8e29892007-01-19 07:51:42 +00001304 }
1305
1306 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001307 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001308 &MemOpChains[0], MemOpChains.size());
1309
1310 // Build a sequence of copy-to-reg nodes chained together with token chain
1311 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001312 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001313 // Tail call byval lowering might overwrite argument registers so in case of
1314 // tail call optimization the copies to registers are lowered later.
1315 if (!isTailCall)
1316 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1317 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1318 RegsToPass[i].second, InFlag);
1319 InFlag = Chain.getValue(1);
1320 }
Evan Chenga8e29892007-01-19 07:51:42 +00001321
Dale Johannesen51e28e62010-06-03 21:09:53 +00001322 // For tail calls lower the arguments to the 'real' stack slot.
1323 if (isTailCall) {
1324 // Force all the incoming stack arguments to be loaded from the stack
1325 // before any new outgoing arguments are stored to the stack, because the
1326 // outgoing stack slots may alias the incoming argument stack slots, and
1327 // the alias isn't otherwise explicit. This is slightly more conservative
1328 // than necessary, because it means that each store effectively depends
1329 // on every argument instead of just those arguments it would clobber.
1330
1331 // Do not flag preceeding copytoreg stuff together with the following stuff.
1332 InFlag = SDValue();
1333 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1334 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1335 RegsToPass[i].second, InFlag);
1336 InFlag = Chain.getValue(1);
1337 }
1338 InFlag =SDValue();
1339 }
1340
Bill Wendling056292f2008-09-16 21:48:12 +00001341 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1342 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1343 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001344 bool isDirect = false;
1345 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001346 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001347 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001348
1349 if (EnableARMLongCalls) {
1350 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1351 && "long-calls with non-static relocation model!");
1352 // Handle a global address or an external symbol. If it's not one of
1353 // those, the target's already in a register, so we don't need to do
1354 // anything extra.
1355 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001356 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001357 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001358 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001359 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1360 ARMPCLabelIndex,
1361 ARMCP::CPValue, 0);
1362 // Get the address of the callee into a register
1363 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1364 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1365 Callee = DAG.getLoad(getPointerTy(), dl,
1366 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001367 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001368 false, false, 0);
1369 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1370 const char *Sym = S->getSymbol();
1371
1372 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001373 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001374 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1375 Sym, ARMPCLabelIndex, 0);
1376 // Get the address of the callee into a register
1377 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1378 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1379 Callee = DAG.getLoad(getPointerTy(), dl,
1380 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001381 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001382 false, false, 0);
1383 }
1384 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001385 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001386 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001387 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001388 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001389 getTargetMachine().getRelocationModel() != Reloc::Static;
1390 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001391 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001392 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001393 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001394 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001395 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001396 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001397 ARMPCLabelIndex,
1398 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001399 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001400 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001401 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001402 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001403 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001404 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001405 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001406 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001407 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001408 } else {
1409 // On ELF targets for PIC code, direct calls should go through the PLT
1410 unsigned OpFlags = 0;
1411 if (Subtarget->isTargetELF() &&
1412 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1413 OpFlags = ARMII::MO_PLT;
1414 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1415 }
Bill Wendling056292f2008-09-16 21:48:12 +00001416 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001417 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001418 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001419 getTargetMachine().getRelocationModel() != Reloc::Static;
1420 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001421 // tBX takes a register source operand.
1422 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001423 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001424 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001425 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001426 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001427 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001428 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001429 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001430 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001431 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001432 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001433 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001434 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001435 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001436 } else {
1437 unsigned OpFlags = 0;
1438 // On ELF targets for PIC code, direct calls should go through the PLT
1439 if (Subtarget->isTargetELF() &&
1440 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1441 OpFlags = ARMII::MO_PLT;
1442 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1443 }
Evan Chenga8e29892007-01-19 07:51:42 +00001444 }
1445
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001446 // FIXME: handle tail calls differently.
1447 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001448 if (Subtarget->isThumb()) {
1449 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001450 CallOpc = ARMISD::CALL_NOLINK;
1451 else
1452 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1453 } else {
1454 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001455 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1456 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001457 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001458
Dan Gohman475871a2008-07-27 21:46:04 +00001459 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001460 Ops.push_back(Chain);
1461 Ops.push_back(Callee);
1462
1463 // Add argument registers to the end of the list so that they are known live
1464 // into the call.
1465 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1466 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1467 RegsToPass[i].second.getValueType()));
1468
Gabor Greifba36cb52008-08-28 21:40:38 +00001469 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001470 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001471
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001472 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001473 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001474 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001475
Duncan Sands4bdcb612008-07-02 17:40:58 +00001476 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001477 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001478 InFlag = Chain.getValue(1);
1479
Chris Lattnere563bbc2008-10-11 22:08:30 +00001480 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1481 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001482 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001483 InFlag = Chain.getValue(1);
1484
Bob Wilson1f595bb2009-04-17 19:07:39 +00001485 // Handle result values, copying them out of physregs into vregs that we
1486 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001487 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1488 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001489}
1490
Stuart Hastingsf222e592011-02-28 17:17:53 +00001491/// HandleByVal - Every parameter *after* a byval parameter is passed
1492/// on the stack. Confiscate all the parameter registers to insure
1493/// this.
1494void
1495llvm::ARMTargetLowering::HandleByVal(CCState *State) const {
1496 static const unsigned RegList1[] = {
1497 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1498 };
1499 do {} while (State->AllocateReg(RegList1, 4));
1500}
1501
Dale Johannesen51e28e62010-06-03 21:09:53 +00001502/// MatchingStackOffset - Return true if the given stack call argument is
1503/// already available in the same position (relatively) of the caller's
1504/// incoming argument stack.
1505static
1506bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1507 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1508 const ARMInstrInfo *TII) {
1509 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1510 int FI = INT_MAX;
1511 if (Arg.getOpcode() == ISD::CopyFromReg) {
1512 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001513 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001514 return false;
1515 MachineInstr *Def = MRI->getVRegDef(VR);
1516 if (!Def)
1517 return false;
1518 if (!Flags.isByVal()) {
1519 if (!TII->isLoadFromStackSlot(Def, FI))
1520 return false;
1521 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001522 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001523 }
1524 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1525 if (Flags.isByVal())
1526 // ByVal argument is passed in as a pointer but it's now being
1527 // dereferenced. e.g.
1528 // define @foo(%struct.X* %A) {
1529 // tail call @bar(%struct.X* byval %A)
1530 // }
1531 return false;
1532 SDValue Ptr = Ld->getBasePtr();
1533 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1534 if (!FINode)
1535 return false;
1536 FI = FINode->getIndex();
1537 } else
1538 return false;
1539
1540 assert(FI != INT_MAX);
1541 if (!MFI->isFixedObjectIndex(FI))
1542 return false;
1543 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1544}
1545
1546/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1547/// for tail call optimization. Targets which want to do tail call
1548/// optimization should implement this function.
1549bool
1550ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1551 CallingConv::ID CalleeCC,
1552 bool isVarArg,
1553 bool isCalleeStructRet,
1554 bool isCallerStructRet,
1555 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001556 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001557 const SmallVectorImpl<ISD::InputArg> &Ins,
1558 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001559 const Function *CallerF = DAG.getMachineFunction().getFunction();
1560 CallingConv::ID CallerCC = CallerF->getCallingConv();
1561 bool CCMatch = CallerCC == CalleeCC;
1562
1563 // Look for obvious safe cases to perform tail call optimization that do not
1564 // require ABI changes. This is what gcc calls sibcall.
1565
Jim Grosbach7616b642010-06-16 23:45:49 +00001566 // Do not sibcall optimize vararg calls unless the call site is not passing
1567 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001568 if (isVarArg && !Outs.empty())
1569 return false;
1570
1571 // Also avoid sibcall optimization if either caller or callee uses struct
1572 // return semantics.
1573 if (isCalleeStructRet || isCallerStructRet)
1574 return false;
1575
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001576 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Evan Cheng0110ac62010-06-19 01:01:32 +00001577 // emitEpilogue is not ready for them.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001578 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1579 // LR. This means if we need to reload LR, it takes an extra instructions,
1580 // which outweighs the value of the tail call; but here we don't know yet
1581 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001582 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001583 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001584
1585 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1586 // but we need to make sure there are enough registers; the only valid
1587 // registers are the 4 used for parameters. We don't currently do this
1588 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001589 if (Subtarget->isThumb1Only())
1590 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001591
Dale Johannesen51e28e62010-06-03 21:09:53 +00001592 // If the calling conventions do not match, then we'd better make sure the
1593 // results are returned in the same way as what the caller expects.
1594 if (!CCMatch) {
1595 SmallVector<CCValAssign, 16> RVLocs1;
1596 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
1597 RVLocs1, *DAG.getContext());
1598 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1599
1600 SmallVector<CCValAssign, 16> RVLocs2;
1601 CCState CCInfo2(CallerCC, false, getTargetMachine(),
1602 RVLocs2, *DAG.getContext());
1603 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1604
1605 if (RVLocs1.size() != RVLocs2.size())
1606 return false;
1607 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1608 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1609 return false;
1610 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1611 return false;
1612 if (RVLocs1[i].isRegLoc()) {
1613 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1614 return false;
1615 } else {
1616 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1617 return false;
1618 }
1619 }
1620 }
1621
1622 // If the callee takes no arguments then go on to check the results of the
1623 // call.
1624 if (!Outs.empty()) {
1625 // Check if stack adjustment is needed. For now, do not do this if any
1626 // argument is passed on the stack.
1627 SmallVector<CCValAssign, 16> ArgLocs;
1628 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
1629 ArgLocs, *DAG.getContext());
1630 CCInfo.AnalyzeCallOperands(Outs,
1631 CCAssignFnForNode(CalleeCC, false, isVarArg));
1632 if (CCInfo.getNextStackOffset()) {
1633 MachineFunction &MF = DAG.getMachineFunction();
1634
1635 // Check if the arguments are already laid out in the right way as
1636 // the caller's fixed stack objects.
1637 MachineFrameInfo *MFI = MF.getFrameInfo();
1638 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1639 const ARMInstrInfo *TII =
1640 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001641 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1642 i != e;
1643 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001644 CCValAssign &VA = ArgLocs[i];
1645 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001646 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001647 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001648 if (VA.getLocInfo() == CCValAssign::Indirect)
1649 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001650 if (VA.needsCustom()) {
1651 // f64 and vector types are split into multiple registers or
1652 // register/stack-slot combinations. The types will not match
1653 // the registers; give up on memory f64 refs until we figure
1654 // out what to do about this.
1655 if (!VA.isRegLoc())
1656 return false;
1657 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001658 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001659 if (RegVT == MVT::v2f64) {
1660 if (!ArgLocs[++i].isRegLoc())
1661 return false;
1662 if (!ArgLocs[++i].isRegLoc())
1663 return false;
1664 }
1665 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001666 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1667 MFI, MRI, TII))
1668 return false;
1669 }
1670 }
1671 }
1672 }
1673
1674 return true;
1675}
1676
Dan Gohman98ca4f22009-08-05 01:29:28 +00001677SDValue
1678ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001679 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001680 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001681 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001682 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001683
Bob Wilsondee46d72009-04-17 20:35:10 +00001684 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001685 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001686
Bob Wilsondee46d72009-04-17 20:35:10 +00001687 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001688 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1689 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001690
Dan Gohman98ca4f22009-08-05 01:29:28 +00001691 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001692 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1693 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001694
1695 // If this is the first return lowered for this function, add
1696 // the regs to the liveout set for the function.
1697 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1698 for (unsigned i = 0; i != RVLocs.size(); ++i)
1699 if (RVLocs[i].isRegLoc())
1700 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001701 }
1702
Bob Wilson1f595bb2009-04-17 19:07:39 +00001703 SDValue Flag;
1704
1705 // Copy the result values into the output registers.
1706 for (unsigned i = 0, realRVLocIdx = 0;
1707 i != RVLocs.size();
1708 ++i, ++realRVLocIdx) {
1709 CCValAssign &VA = RVLocs[i];
1710 assert(VA.isRegLoc() && "Can only return in registers!");
1711
Dan Gohmanc9403652010-07-07 15:54:55 +00001712 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001713
1714 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001715 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001716 case CCValAssign::Full: break;
1717 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001718 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001719 break;
1720 }
1721
Bob Wilson1f595bb2009-04-17 19:07:39 +00001722 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001723 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001724 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001725 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1726 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001727 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001728 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001729
1730 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1731 Flag = Chain.getValue(1);
1732 VA = RVLocs[++i]; // skip ahead to next loc
1733 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1734 HalfGPRs.getValue(1), Flag);
1735 Flag = Chain.getValue(1);
1736 VA = RVLocs[++i]; // skip ahead to next loc
1737
1738 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001739 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1740 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001741 }
1742 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1743 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001744 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001745 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001746 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001747 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001748 VA = RVLocs[++i]; // skip ahead to next loc
1749 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1750 Flag);
1751 } else
1752 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1753
Bob Wilsondee46d72009-04-17 20:35:10 +00001754 // Guarantee that all emitted copies are
1755 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001756 Flag = Chain.getValue(1);
1757 }
1758
1759 SDValue result;
1760 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001761 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001762 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001763 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001764
1765 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001766}
1767
Evan Cheng3d2125c2010-11-30 23:55:39 +00001768bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1769 if (N->getNumValues() != 1)
1770 return false;
1771 if (!N->hasNUsesOfValue(1, 0))
1772 return false;
1773
1774 unsigned NumCopies = 0;
1775 SDNode* Copies[2];
1776 SDNode *Use = *N->use_begin();
1777 if (Use->getOpcode() == ISD::CopyToReg) {
1778 Copies[NumCopies++] = Use;
1779 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1780 // f64 returned in a pair of GPRs.
1781 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1782 UI != UE; ++UI) {
1783 if (UI->getOpcode() != ISD::CopyToReg)
1784 return false;
1785 Copies[UI.getUse().getResNo()] = *UI;
1786 ++NumCopies;
1787 }
1788 } else if (Use->getOpcode() == ISD::BITCAST) {
1789 // f32 returned in a single GPR.
1790 if (!Use->hasNUsesOfValue(1, 0))
1791 return false;
1792 Use = *Use->use_begin();
1793 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1794 return false;
1795 Copies[NumCopies++] = Use;
1796 } else {
1797 return false;
1798 }
1799
1800 if (NumCopies != 1 && NumCopies != 2)
1801 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001802
1803 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001804 for (unsigned i = 0; i < NumCopies; ++i) {
1805 SDNode *Copy = Copies[i];
1806 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1807 UI != UE; ++UI) {
1808 if (UI->getOpcode() == ISD::CopyToReg) {
1809 SDNode *Use = *UI;
1810 if (Use == Copies[0] || Use == Copies[1])
1811 continue;
1812 return false;
1813 }
1814 if (UI->getOpcode() != ARMISD::RET_FLAG)
1815 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001816 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001817 }
1818 }
1819
Evan Cheng1bf891a2010-12-01 22:59:46 +00001820 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001821}
1822
Evan Cheng485fafc2011-03-21 01:19:09 +00001823bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1824 if (!EnableARMTailCalls)
1825 return false;
1826
1827 if (!CI->isTailCall())
1828 return false;
1829
1830 return !Subtarget->isThumb1Only();
1831}
1832
Bob Wilsonb62d2572009-11-03 00:02:05 +00001833// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1834// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1835// one of the above mentioned nodes. It has to be wrapped because otherwise
1836// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1837// be used to form addressing mode. These wrapped nodes will be selected
1838// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001839static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001840 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001841 // FIXME there is no actual debug info here
1842 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001843 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001844 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001845 if (CP->isMachineConstantPoolEntry())
1846 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1847 CP->getAlignment());
1848 else
1849 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1850 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001851 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001852}
1853
Jim Grosbache1102ca2010-07-19 17:20:38 +00001854unsigned ARMTargetLowering::getJumpTableEncoding() const {
1855 return MachineJumpTableInfo::EK_Inline;
1856}
1857
Dan Gohmand858e902010-04-17 15:26:15 +00001858SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1859 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001860 MachineFunction &MF = DAG.getMachineFunction();
1861 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1862 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001863 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001864 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001865 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001866 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1867 SDValue CPAddr;
1868 if (RelocM == Reloc::Static) {
1869 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1870 } else {
1871 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001872 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001873 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1874 ARMCP::CPBlockAddress,
1875 PCAdj);
1876 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1877 }
1878 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1879 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001880 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001881 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001882 if (RelocM == Reloc::Static)
1883 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001884 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001885 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001886}
1887
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001888// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001889SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001890ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001891 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001892 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001893 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001894 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001895 MachineFunction &MF = DAG.getMachineFunction();
1896 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001897 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001898 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001899 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001900 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001901 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001902 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001903 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001904 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001905 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001906 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001907
Evan Chenge7e0d622009-11-06 22:24:13 +00001908 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001909 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001910
1911 // call __tls_get_addr.
1912 ArgListTy Args;
1913 ArgListEntry Entry;
1914 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001915 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001916 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001917 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001918 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001919 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1920 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001921 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001922 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001923 return CallResult.first;
1924}
1925
1926// Lower ISD::GlobalTLSAddress using the "initial exec" or
1927// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001928SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001929ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001930 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001931 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001932 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001933 SDValue Offset;
1934 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001935 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001936 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001937 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001938
Chris Lattner4fb63d02009-07-15 04:12:33 +00001939 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001940 MachineFunction &MF = DAG.getMachineFunction();
1941 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001942 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00001943 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001944 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1945 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001946 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001947 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001948 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001949 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001950 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001951 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001952 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001953 Chain = Offset.getValue(1);
1954
Evan Chenge7e0d622009-11-06 22:24:13 +00001955 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001956 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001957
Evan Cheng9eda6892009-10-31 03:39:36 +00001958 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001959 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001960 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001961 } else {
1962 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001963 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001964 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001965 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001966 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001967 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001968 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001969 }
1970
1971 // The address of the thread local variable is the add of the thread
1972 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001973 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001974}
1975
Dan Gohman475871a2008-07-27 21:46:04 +00001976SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001977ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001978 // TODO: implement the "local dynamic" model
1979 assert(Subtarget->isTargetELF() &&
1980 "TLS not implemented for non-ELF targets");
1981 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1982 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1983 // otherwise use the "Local Exec" TLS Model
1984 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1985 return LowerToTLSGeneralDynamicModel(GA, DAG);
1986 else
1987 return LowerToTLSExecModels(GA, DAG);
1988}
1989
Dan Gohman475871a2008-07-27 21:46:04 +00001990SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001991 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001992 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001993 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001994 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001995 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1996 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001997 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001998 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001999 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002000 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002001 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002002 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002003 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002004 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002005 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002006 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002007 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002008 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002009 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002010 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002011 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002012 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002013 }
2014
2015 // If we have T2 ops, we can materialize the address directly via movt/movw
2016 // pair. This is always cheaper.
2017 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002018 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002019 // FIXME: Once remat is capable of dealing with instructions with register
2020 // operands, expand this into two nodes.
2021 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2022 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002023 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002024 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2025 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2026 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2027 MachinePointerInfo::getConstantPool(),
2028 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002029 }
2030}
2031
Dan Gohman475871a2008-07-27 21:46:04 +00002032SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002033 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002034 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002035 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002036 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002037 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002038 MachineFunction &MF = DAG.getMachineFunction();
2039 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2040
2041 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002042 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002043 // FIXME: Once remat is capable of dealing with instructions with register
2044 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002045 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002046 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2047 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2048
Evan Cheng53519f02011-01-21 18:55:51 +00002049 unsigned Wrapper = (RelocM == Reloc::PIC_)
2050 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2051 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002052 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002053 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2054 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2055 MachinePointerInfo::getGOT(), false, false, 0);
2056 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002057 }
2058
2059 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002060 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002061 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002062 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002063 } else {
2064 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002065 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2066 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002067 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002068 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002069 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002070 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002071
Evan Cheng9eda6892009-10-31 03:39:36 +00002072 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002073 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002074 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002075 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002076
2077 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002078 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002079 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002080 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002081
Evan Cheng63476a82009-09-03 07:04:02 +00002082 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002083 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002084 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002085
2086 return Result;
2087}
2088
Dan Gohman475871a2008-07-27 21:46:04 +00002089SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002090 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002091 assert(Subtarget->isTargetELF() &&
2092 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002093 MachineFunction &MF = DAG.getMachineFunction();
2094 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002095 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002096 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002097 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002098 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002099 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2100 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002101 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002102 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002103 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002104 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002105 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002106 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002107 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002108 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002109}
2110
Jim Grosbach0e0da732009-05-12 23:59:14 +00002111SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002112ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2113 const {
2114 DebugLoc dl = Op.getDebugLoc();
2115 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
2116 Op.getOperand(0), Op.getOperand(1));
2117}
2118
2119SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002120ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2121 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002122 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002123 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2124 Op.getOperand(1), Val);
2125}
2126
2127SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002128ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2129 DebugLoc dl = Op.getDebugLoc();
2130 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2131 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2132}
2133
2134SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002135ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002136 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002137 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002138 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002139 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002140 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002141 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002142 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002143 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2144 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002145 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002146 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002147 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002148 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002149 EVT PtrVT = getPointerTy();
2150 DebugLoc dl = Op.getDebugLoc();
2151 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2152 SDValue CPAddr;
2153 unsigned PCAdj = (RelocM != Reloc::PIC_)
2154 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002155 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002156 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2157 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002158 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002159 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002160 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002161 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002162 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002163 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002164
2165 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002166 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002167 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2168 }
2169 return Result;
2170 }
Evan Cheng92e39162011-03-29 23:06:19 +00002171 case Intrinsic::arm_neon_vmulls:
2172 case Intrinsic::arm_neon_vmullu: {
2173 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2174 ? ARMISD::VMULLs : ARMISD::VMULLu;
2175 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2176 Op.getOperand(1), Op.getOperand(2));
2177 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002178 }
2179}
2180
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002181static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002182 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002183 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002184 if (!Subtarget->hasDataBarrier()) {
2185 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2186 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2187 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002188 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002189 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002190 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002191 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002192 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002193
2194 SDValue Op5 = Op.getOperand(5);
2195 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2196 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2197 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2198 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2199
2200 ARM_MB::MemBOpt DMBOpt;
2201 if (isDeviceBarrier)
2202 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2203 else
2204 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2205 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2206 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002207}
2208
Evan Chengdfed19f2010-11-03 06:34:55 +00002209static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2210 const ARMSubtarget *Subtarget) {
2211 // ARM pre v5TE and Thumb1 does not have preload instructions.
2212 if (!(Subtarget->isThumb2() ||
2213 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2214 // Just preserve the chain.
2215 return Op.getOperand(0);
2216
2217 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002218 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2219 if (!isRead &&
2220 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2221 // ARMv7 with MP extension has PLDW.
2222 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002223
2224 if (Subtarget->isThumb())
2225 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002226 isRead = ~isRead & 1;
2227 unsigned isData = Subtarget->isThumb() ? 0 : 1;
Evan Chengdfed19f2010-11-03 06:34:55 +00002228
Evan Cheng416941d2010-11-04 05:19:35 +00002229 // Currently there is no intrinsic that matches pli.
Evan Chengdfed19f2010-11-03 06:34:55 +00002230 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002231 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2232 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002233}
2234
Dan Gohman1e93df62010-04-17 14:41:14 +00002235static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2236 MachineFunction &MF = DAG.getMachineFunction();
2237 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2238
Evan Chenga8e29892007-01-19 07:51:42 +00002239 // vastart just stores the address of the VarArgsFrameIndex slot into the
2240 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002241 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002242 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002243 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002244 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002245 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2246 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002247}
2248
Dan Gohman475871a2008-07-27 21:46:04 +00002249SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002250ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2251 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002252 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002253 MachineFunction &MF = DAG.getMachineFunction();
2254 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2255
2256 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002257 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002258 RC = ARM::tGPRRegisterClass;
2259 else
2260 RC = ARM::GPRRegisterClass;
2261
2262 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002263 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002264 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002265
2266 SDValue ArgValue2;
2267 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002268 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002269 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002270
2271 // Create load node to retrieve arguments from the stack.
2272 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002273 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002274 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002275 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002276 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002277 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002278 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002279 }
2280
Jim Grosbache5165492009-11-09 00:11:35 +00002281 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002282}
2283
2284SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002285ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002286 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002287 const SmallVectorImpl<ISD::InputArg>
2288 &Ins,
2289 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002290 SmallVectorImpl<SDValue> &InVals)
2291 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002292
Bob Wilson1f595bb2009-04-17 19:07:39 +00002293 MachineFunction &MF = DAG.getMachineFunction();
2294 MachineFrameInfo *MFI = MF.getFrameInfo();
2295
Bob Wilson1f595bb2009-04-17 19:07:39 +00002296 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2297
2298 // Assign locations to all of the incoming arguments.
2299 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002300 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
2301 *DAG.getContext());
2302 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002303 CCAssignFnForNode(CallConv, /* Return*/ false,
2304 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002305
2306 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002307 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002308
Stuart Hastingsf222e592011-02-28 17:17:53 +00002309 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002310 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2311 CCValAssign &VA = ArgLocs[i];
2312
Bob Wilsondee46d72009-04-17 20:35:10 +00002313 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002314 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002315 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002316
Bob Wilson1f595bb2009-04-17 19:07:39 +00002317 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002318 // f64 and vector types are split up into multiple registers or
2319 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002320 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002321 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002322 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002323 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002324 SDValue ArgValue2;
2325 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002326 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002327 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2328 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002329 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002330 false, false, 0);
2331 } else {
2332 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2333 Chain, DAG, dl);
2334 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002335 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2336 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002337 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002338 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002339 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2340 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002341 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002342
Bob Wilson5bafff32009-06-22 23:27:02 +00002343 } else {
2344 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002345
Owen Anderson825b72b2009-08-11 20:47:22 +00002346 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002347 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002348 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002349 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002350 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002351 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002352 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002353 RC = (AFI->isThumb1OnlyFunction() ?
2354 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002355 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002356 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002357
2358 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002359 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002360 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002361 }
2362
2363 // If this is an 8 or 16-bit value, it is really passed promoted
2364 // to 32 bits. Insert an assert[sz]ext to capture this, then
2365 // truncate to the right size.
2366 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002367 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002368 case CCValAssign::Full: break;
2369 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002370 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002371 break;
2372 case CCValAssign::SExt:
2373 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2374 DAG.getValueType(VA.getValVT()));
2375 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2376 break;
2377 case CCValAssign::ZExt:
2378 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2379 DAG.getValueType(VA.getValVT()));
2380 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2381 break;
2382 }
2383
Dan Gohman98ca4f22009-08-05 01:29:28 +00002384 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002385
2386 } else { // VA.isRegLoc()
2387
2388 // sanity check
2389 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002390 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002391
Stuart Hastingsf222e592011-02-28 17:17:53 +00002392 int index = ArgLocs[i].getValNo();
2393
2394 // Some Ins[] entries become multiple ArgLoc[] entries.
2395 // Process them only once.
2396 if (index != lastInsIndex)
2397 {
2398 ISD::ArgFlagsTy Flags = Ins[index].Flags;
2399 // FIXME: For now, all byval parameter objects are marked mutable. This can be
2400 // changed with more analysis.
2401 // In case of tail call optimization mark all arguments mutable. Since they
2402 // could be overwritten by lowering of arguments in case of a tail call.
2403 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00002404 unsigned Bytes = Flags.getByValSize();
2405 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
2406 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002407 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2408 } else {
2409 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2410 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002411
Stuart Hastingsf222e592011-02-28 17:17:53 +00002412 // Create load nodes to retrieve arguments from the stack.
2413 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2414 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2415 MachinePointerInfo::getFixedStack(FI),
2416 false, false, 0));
2417 }
2418 lastInsIndex = index;
2419 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002420 }
2421 }
2422
2423 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00002424 if (isVarArg) {
2425 static const unsigned GPRArgRegs[] = {
2426 ARM::R0, ARM::R1, ARM::R2, ARM::R3
2427 };
2428
Bob Wilsondee46d72009-04-17 20:35:10 +00002429 unsigned NumGPRs = CCInfo.getFirstUnallocated
2430 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002431
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002432 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00002433 unsigned VARegSize = (4 - NumGPRs) * 4;
2434 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00002435 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00002436 if (VARegSaveSize) {
2437 // If this function is vararg, store any remaining integer argument regs
2438 // to their spots on the stack so that they may be loaded by deferencing
2439 // the result of va_next.
2440 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00002441 AFI->setVarArgsFrameIndex(
2442 MFI->CreateFixedObject(VARegSaveSize,
2443 ArgOffset + VARegSaveSize - VARegSize,
Jim Grosbachfd529062010-10-15 18:34:47 +00002444 false));
Dan Gohman1e93df62010-04-17 14:41:14 +00002445 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2446 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00002447
Dan Gohman475871a2008-07-27 21:46:04 +00002448 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00002449 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002450 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002451 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00002452 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002453 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00002454 RC = ARM::GPRRegisterClass;
2455
Devang Patel68e6bee2011-02-21 23:21:26 +00002456 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002457 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00002458 SDValue Store =
2459 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002460 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
2461 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002462 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002463 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00002464 DAG.getConstant(4, getPointerTy()));
2465 }
2466 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002467 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002468 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00002469 } else
2470 // This will point to the next argument passed via stack.
Evan Chenged2ae132010-07-03 00:40:23 +00002471 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
Evan Chenga8e29892007-01-19 07:51:42 +00002472 }
2473
Dan Gohman98ca4f22009-08-05 01:29:28 +00002474 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002475}
2476
2477/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002478static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002479 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002480 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002481 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002482 // Maybe this has already been legalized into the constant pool?
2483 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002484 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002485 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002486 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002487 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002488 }
2489 }
2490 return false;
2491}
2492
Evan Chenga8e29892007-01-19 07:51:42 +00002493/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2494/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002495SDValue
2496ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002497 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002498 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002499 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002500 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002501 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002502 // Constant does not fit, try adjusting it by one?
2503 switch (CC) {
2504 default: break;
2505 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002506 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002507 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002508 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002509 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002510 }
2511 break;
2512 case ISD::SETULT:
2513 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002514 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002515 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002516 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002517 }
2518 break;
2519 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002520 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002521 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002522 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002523 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002524 }
2525 break;
2526 case ISD::SETULE:
2527 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002528 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002529 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002530 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002531 }
2532 break;
2533 }
2534 }
2535 }
2536
2537 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002538 ARMISD::NodeType CompareType;
2539 switch (CondCode) {
2540 default:
2541 CompareType = ARMISD::CMP;
2542 break;
2543 case ARMCC::EQ:
2544 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002545 // Uses only Z Flag
2546 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002547 break;
2548 }
Evan Cheng218977b2010-07-13 19:27:42 +00002549 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002550 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002551}
2552
2553/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002554SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002555ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002556 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002557 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002558 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002559 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002560 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002561 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2562 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002563}
2564
Bob Wilson79f56c92011-03-08 01:17:20 +00002565/// duplicateCmp - Glue values can have only one use, so this function
2566/// duplicates a comparison node.
2567SDValue
2568ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2569 unsigned Opc = Cmp.getOpcode();
2570 DebugLoc DL = Cmp.getDebugLoc();
2571 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2572 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2573
2574 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2575 Cmp = Cmp.getOperand(0);
2576 Opc = Cmp.getOpcode();
2577 if (Opc == ARMISD::CMPFP)
2578 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2579 else {
2580 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2581 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2582 }
2583 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2584}
2585
Bill Wendlingde2b1512010-08-11 08:43:16 +00002586SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2587 SDValue Cond = Op.getOperand(0);
2588 SDValue SelectTrue = Op.getOperand(1);
2589 SDValue SelectFalse = Op.getOperand(2);
2590 DebugLoc dl = Op.getDebugLoc();
2591
2592 // Convert:
2593 //
2594 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2595 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2596 //
2597 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2598 const ConstantSDNode *CMOVTrue =
2599 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2600 const ConstantSDNode *CMOVFalse =
2601 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2602
2603 if (CMOVTrue && CMOVFalse) {
2604 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2605 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2606
2607 SDValue True;
2608 SDValue False;
2609 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2610 True = SelectTrue;
2611 False = SelectFalse;
2612 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2613 True = SelectFalse;
2614 False = SelectTrue;
2615 }
2616
2617 if (True.getNode() && False.getNode()) {
2618 EVT VT = Cond.getValueType();
2619 SDValue ARMcc = Cond.getOperand(2);
2620 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002621 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002622 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
2623 }
2624 }
2625 }
2626
2627 return DAG.getSelectCC(dl, Cond,
2628 DAG.getConstant(0, Cond.getValueType()),
2629 SelectTrue, SelectFalse, ISD::SETNE);
2630}
2631
Dan Gohmand858e902010-04-17 15:26:15 +00002632SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002633 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002634 SDValue LHS = Op.getOperand(0);
2635 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002636 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002637 SDValue TrueVal = Op.getOperand(2);
2638 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002639 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002640
Owen Anderson825b72b2009-08-11 20:47:22 +00002641 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002642 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002643 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002644 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2645 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002646 }
2647
2648 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002649 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002650
Evan Cheng218977b2010-07-13 19:27:42 +00002651 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2652 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002653 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002654 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002655 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002656 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002657 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002658 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002659 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002660 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002661 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002662 }
2663 return Result;
2664}
2665
Evan Cheng218977b2010-07-13 19:27:42 +00002666/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2667/// to morph to an integer compare sequence.
2668static bool canChangeToInt(SDValue Op, bool &SeenZero,
2669 const ARMSubtarget *Subtarget) {
2670 SDNode *N = Op.getNode();
2671 if (!N->hasOneUse())
2672 // Otherwise it requires moving the value from fp to integer registers.
2673 return false;
2674 if (!N->getNumValues())
2675 return false;
2676 EVT VT = Op.getValueType();
2677 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2678 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2679 // vmrs are very slow, e.g. cortex-a8.
2680 return false;
2681
2682 if (isFloatingPointZero(Op)) {
2683 SeenZero = true;
2684 return true;
2685 }
2686 return ISD::isNormalLoad(N);
2687}
2688
2689static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2690 if (isFloatingPointZero(Op))
2691 return DAG.getConstant(0, MVT::i32);
2692
2693 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2694 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002695 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002696 Ld->isVolatile(), Ld->isNonTemporal(),
2697 Ld->getAlignment());
2698
2699 llvm_unreachable("Unknown VFP cmp argument!");
2700}
2701
2702static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2703 SDValue &RetVal1, SDValue &RetVal2) {
2704 if (isFloatingPointZero(Op)) {
2705 RetVal1 = DAG.getConstant(0, MVT::i32);
2706 RetVal2 = DAG.getConstant(0, MVT::i32);
2707 return;
2708 }
2709
2710 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2711 SDValue Ptr = Ld->getBasePtr();
2712 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2713 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002714 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002715 Ld->isVolatile(), Ld->isNonTemporal(),
2716 Ld->getAlignment());
2717
2718 EVT PtrType = Ptr.getValueType();
2719 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2720 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2721 PtrType, Ptr, DAG.getConstant(4, PtrType));
2722 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2723 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002724 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002725 Ld->isVolatile(), Ld->isNonTemporal(),
2726 NewAlign);
2727 return;
2728 }
2729
2730 llvm_unreachable("Unknown VFP cmp argument!");
2731}
2732
2733/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2734/// f32 and even f64 comparisons to integer ones.
2735SDValue
2736ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2737 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002738 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002739 SDValue LHS = Op.getOperand(2);
2740 SDValue RHS = Op.getOperand(3);
2741 SDValue Dest = Op.getOperand(4);
2742 DebugLoc dl = Op.getDebugLoc();
2743
2744 bool SeenZero = false;
2745 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2746 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002747 // If one of the operand is zero, it's safe to ignore the NaN case since
2748 // we only care about equality comparisons.
2749 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002750 // If unsafe fp math optimization is enabled and there are no other uses of
2751 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002752 // to an integer comparison.
2753 if (CC == ISD::SETOEQ)
2754 CC = ISD::SETEQ;
2755 else if (CC == ISD::SETUNE)
2756 CC = ISD::SETNE;
2757
2758 SDValue ARMcc;
2759 if (LHS.getValueType() == MVT::f32) {
2760 LHS = bitcastf32Toi32(LHS, DAG);
2761 RHS = bitcastf32Toi32(RHS, DAG);
2762 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2763 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2764 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2765 Chain, Dest, ARMcc, CCR, Cmp);
2766 }
2767
2768 SDValue LHS1, LHS2;
2769 SDValue RHS1, RHS2;
2770 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2771 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2772 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2773 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002774 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002775 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2776 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2777 }
2778
2779 return SDValue();
2780}
2781
2782SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2783 SDValue Chain = Op.getOperand(0);
2784 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2785 SDValue LHS = Op.getOperand(2);
2786 SDValue RHS = Op.getOperand(3);
2787 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002788 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002789
Owen Anderson825b72b2009-08-11 20:47:22 +00002790 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002791 SDValue ARMcc;
2792 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002793 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002794 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002795 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002796 }
2797
Owen Anderson825b72b2009-08-11 20:47:22 +00002798 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002799
2800 if (UnsafeFPMath &&
2801 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2802 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2803 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2804 if (Result.getNode())
2805 return Result;
2806 }
2807
Evan Chenga8e29892007-01-19 07:51:42 +00002808 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002809 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002810
Evan Cheng218977b2010-07-13 19:27:42 +00002811 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2812 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002813 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002814 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002815 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002816 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002817 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002818 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2819 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002820 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002821 }
2822 return Res;
2823}
2824
Dan Gohmand858e902010-04-17 15:26:15 +00002825SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002826 SDValue Chain = Op.getOperand(0);
2827 SDValue Table = Op.getOperand(1);
2828 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002829 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002830
Owen Andersone50ed302009-08-10 22:56:29 +00002831 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002832 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2833 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002834 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002835 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002836 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002837 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2838 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002839 if (Subtarget->isThumb2()) {
2840 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2841 // which does another jump to the destination. This also makes it easier
2842 // to translate it to TBB / TBH later.
2843 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002844 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002845 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002846 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002847 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002848 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002849 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002850 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002851 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002852 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002853 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002854 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002855 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002856 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002857 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002858 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002859 }
Evan Chenga8e29892007-01-19 07:51:42 +00002860}
2861
Bob Wilson76a312b2010-03-19 22:51:32 +00002862static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2863 DebugLoc dl = Op.getDebugLoc();
2864 unsigned Opc;
2865
2866 switch (Op.getOpcode()) {
2867 default:
2868 assert(0 && "Invalid opcode!");
2869 case ISD::FP_TO_SINT:
2870 Opc = ARMISD::FTOSI;
2871 break;
2872 case ISD::FP_TO_UINT:
2873 Opc = ARMISD::FTOUI;
2874 break;
2875 }
2876 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002877 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00002878}
2879
Cameron Zwarich3007d332011-03-29 21:41:55 +00002880static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2881 EVT VT = Op.getValueType();
2882 DebugLoc dl = Op.getDebugLoc();
2883
2884 EVT OperandVT = Op.getOperand(0).getValueType();
2885 assert(OperandVT == MVT::v4i16 && "Invalid type for custom lowering!");
2886 if (VT != MVT::v4f32)
2887 return DAG.UnrollVectorOp(Op.getNode());
2888
2889 unsigned CastOpc;
2890 unsigned Opc;
2891 switch (Op.getOpcode()) {
2892 default:
2893 assert(0 && "Invalid opcode!");
2894 case ISD::SINT_TO_FP:
2895 CastOpc = ISD::SIGN_EXTEND;
2896 Opc = ISD::SINT_TO_FP;
2897 break;
2898 case ISD::UINT_TO_FP:
2899 CastOpc = ISD::ZERO_EXTEND;
2900 Opc = ISD::UINT_TO_FP;
2901 break;
2902 }
2903
2904 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
2905 return DAG.getNode(Opc, dl, VT, Op);
2906}
2907
Bob Wilson76a312b2010-03-19 22:51:32 +00002908static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2909 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00002910 if (VT.isVector())
2911 return LowerVectorINT_TO_FP(Op, DAG);
2912
Bob Wilson76a312b2010-03-19 22:51:32 +00002913 DebugLoc dl = Op.getDebugLoc();
2914 unsigned Opc;
2915
2916 switch (Op.getOpcode()) {
2917 default:
2918 assert(0 && "Invalid opcode!");
2919 case ISD::SINT_TO_FP:
2920 Opc = ARMISD::SITOF;
2921 break;
2922 case ISD::UINT_TO_FP:
2923 Opc = ARMISD::UITOF;
2924 break;
2925 }
2926
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002927 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00002928 return DAG.getNode(Opc, dl, VT, Op);
2929}
2930
Evan Cheng515fe3a2010-07-08 02:08:50 +00002931SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002932 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002933 SDValue Tmp0 = Op.getOperand(0);
2934 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002935 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002936 EVT VT = Op.getValueType();
2937 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00002938 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
2939 Tmp0.getOpcode() == ARMISD::VMOVDRR;
2940 bool UseNEON = !InGPR && Subtarget->hasNEON();
2941
2942 if (UseNEON) {
2943 // Use VBSL to copy the sign bit.
2944 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
2945 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
2946 DAG.getTargetConstant(EncodedVal, MVT::i32));
2947 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
2948 if (VT == MVT::f64)
2949 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
2950 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
2951 DAG.getConstant(32, MVT::i32));
2952 else /*if (VT == MVT::f32)*/
2953 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
2954 if (SrcVT == MVT::f32) {
2955 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
2956 if (VT == MVT::f64)
2957 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
2958 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
2959 DAG.getConstant(32, MVT::i32));
2960 }
2961 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
2962 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
2963
2964 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
2965 MVT::i32);
2966 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
2967 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
2968 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
2969
2970 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
2971 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
2972 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00002973 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00002974 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
2975 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
2976 DAG.getConstant(0, MVT::i32));
2977 } else {
2978 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
2979 }
2980
2981 return Res;
2982 }
Evan Chengc143dd42011-02-11 02:28:55 +00002983
2984 // Bitcast operand 1 to i32.
2985 if (SrcVT == MVT::f64)
2986 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
2987 &Tmp1, 1).getValue(1);
2988 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
2989
Evan Chenge573fb32011-02-23 02:24:55 +00002990 // Or in the signbit with integer operations.
2991 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
2992 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
2993 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
2994 if (VT == MVT::f32) {
2995 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
2996 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
2997 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
2998 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00002999 }
3000
Evan Chenge573fb32011-02-23 02:24:55 +00003001 // f64: Or the high part with signbit and then combine two parts.
3002 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3003 &Tmp0, 1);
3004 SDValue Lo = Tmp0.getValue(0);
3005 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3006 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3007 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003008}
3009
Evan Cheng2457f2c2010-05-22 01:47:14 +00003010SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3011 MachineFunction &MF = DAG.getMachineFunction();
3012 MachineFrameInfo *MFI = MF.getFrameInfo();
3013 MFI->setReturnAddressIsTaken(true);
3014
3015 EVT VT = Op.getValueType();
3016 DebugLoc dl = Op.getDebugLoc();
3017 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3018 if (Depth) {
3019 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3020 SDValue Offset = DAG.getConstant(4, MVT::i32);
3021 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3022 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003023 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003024 }
3025
3026 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003027 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003028 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3029}
3030
Dan Gohmand858e902010-04-17 15:26:15 +00003031SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003032 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3033 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003034
Owen Andersone50ed302009-08-10 22:56:29 +00003035 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003036 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3037 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003038 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003039 ? ARM::R7 : ARM::R11;
3040 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3041 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003042 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3043 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00003044 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003045 return FrameAddr;
3046}
3047
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003048/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003049/// expand a bit convert where either the source or destination type is i64 to
3050/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3051/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3052/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003053static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003054 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3055 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003056 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003057
Bob Wilson9f3f0612010-04-17 05:30:19 +00003058 // This function is only supposed to be called for i64 types, either as the
3059 // source or destination of the bit convert.
3060 EVT SrcVT = Op.getValueType();
3061 EVT DstVT = N->getValueType(0);
3062 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003063 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003064
Bob Wilson9f3f0612010-04-17 05:30:19 +00003065 // Turn i64->f64 into VMOVDRR.
3066 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003067 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3068 DAG.getConstant(0, MVT::i32));
3069 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3070 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003071 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003072 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003073 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003074
Jim Grosbache5165492009-11-09 00:11:35 +00003075 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003076 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3077 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3078 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3079 // Merge the pieces into a single i64 value.
3080 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3081 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003082
Bob Wilson9f3f0612010-04-17 05:30:19 +00003083 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003084}
3085
Bob Wilson5bafff32009-06-22 23:27:02 +00003086/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003087/// Zero vectors are used to represent vector negation and in those cases
3088/// will be implemented with the NEON VNEG instruction. However, VNEG does
3089/// not support i64 elements, so sometimes the zero vectors will need to be
3090/// explicitly constructed. Regardless, use a canonical VMOV to create the
3091/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003092static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003093 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003094 // The canonical modified immediate encoding of a zero vector is....0!
3095 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3096 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3097 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003098 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003099}
3100
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003101/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3102/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003103SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3104 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003105 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3106 EVT VT = Op.getValueType();
3107 unsigned VTBits = VT.getSizeInBits();
3108 DebugLoc dl = Op.getDebugLoc();
3109 SDValue ShOpLo = Op.getOperand(0);
3110 SDValue ShOpHi = Op.getOperand(1);
3111 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003112 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003113 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003114
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003115 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3116
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003117 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3118 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3119 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3120 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3121 DAG.getConstant(VTBits, MVT::i32));
3122 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3123 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003124 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003125
3126 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3127 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003128 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003129 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003130 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003131 CCR, Cmp);
3132
3133 SDValue Ops[2] = { Lo, Hi };
3134 return DAG.getMergeValues(Ops, 2, dl);
3135}
3136
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003137/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3138/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003139SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3140 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003141 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3142 EVT VT = Op.getValueType();
3143 unsigned VTBits = VT.getSizeInBits();
3144 DebugLoc dl = Op.getDebugLoc();
3145 SDValue ShOpLo = Op.getOperand(0);
3146 SDValue ShOpHi = Op.getOperand(1);
3147 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003148 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003149
3150 assert(Op.getOpcode() == ISD::SHL_PARTS);
3151 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3152 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3153 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3154 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3155 DAG.getConstant(VTBits, MVT::i32));
3156 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3157 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3158
3159 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3160 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3161 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003162 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003163 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003164 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003165 CCR, Cmp);
3166
3167 SDValue Ops[2] = { Lo, Hi };
3168 return DAG.getMergeValues(Ops, 2, dl);
3169}
3170
Jim Grosbach4725ca72010-09-08 03:54:02 +00003171SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003172 SelectionDAG &DAG) const {
3173 // The rounding mode is in bits 23:22 of the FPSCR.
3174 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3175 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3176 // so that the shift + and get folded into a bitfield extract.
3177 DebugLoc dl = Op.getDebugLoc();
3178 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3179 DAG.getConstant(Intrinsic::arm_get_fpscr,
3180 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003181 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003182 DAG.getConstant(1U << 22, MVT::i32));
3183 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3184 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003185 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003186 DAG.getConstant(3, MVT::i32));
3187}
3188
Jim Grosbach3482c802010-01-18 19:58:49 +00003189static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3190 const ARMSubtarget *ST) {
3191 EVT VT = N->getValueType(0);
3192 DebugLoc dl = N->getDebugLoc();
3193
3194 if (!ST->hasV6T2Ops())
3195 return SDValue();
3196
3197 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3198 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3199}
3200
Bob Wilson5bafff32009-06-22 23:27:02 +00003201static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3202 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003203 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003204 DebugLoc dl = N->getDebugLoc();
3205
Bob Wilsond5448bb2010-11-18 21:16:28 +00003206 if (!VT.isVector())
3207 return SDValue();
3208
Bob Wilson5bafff32009-06-22 23:27:02 +00003209 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003210 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003211
Bob Wilsond5448bb2010-11-18 21:16:28 +00003212 // Left shifts translate directly to the vshiftu intrinsic.
3213 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003214 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003215 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3216 N->getOperand(0), N->getOperand(1));
3217
3218 assert((N->getOpcode() == ISD::SRA ||
3219 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3220
3221 // NEON uses the same intrinsics for both left and right shifts. For
3222 // right shifts, the shift amounts are negative, so negate the vector of
3223 // shift amounts.
3224 EVT ShiftVT = N->getOperand(1).getValueType();
3225 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3226 getZeroVector(ShiftVT, DAG, dl),
3227 N->getOperand(1));
3228 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3229 Intrinsic::arm_neon_vshifts :
3230 Intrinsic::arm_neon_vshiftu);
3231 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3232 DAG.getConstant(vshiftInt, MVT::i32),
3233 N->getOperand(0), NegatedCount);
3234}
3235
3236static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3237 const ARMSubtarget *ST) {
3238 EVT VT = N->getValueType(0);
3239 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003240
Eli Friedmance392eb2009-08-22 03:13:10 +00003241 // We can get here for a node like i32 = ISD::SHL i32, i64
3242 if (VT != MVT::i64)
3243 return SDValue();
3244
3245 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003246 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003247
Chris Lattner27a6c732007-11-24 07:07:01 +00003248 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3249 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003250 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003251 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003252
Chris Lattner27a6c732007-11-24 07:07:01 +00003253 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003254 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003255
Chris Lattner27a6c732007-11-24 07:07:01 +00003256 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003257 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003258 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003259 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003260 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003261
Chris Lattner27a6c732007-11-24 07:07:01 +00003262 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3263 // captures the result into a carry flag.
3264 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003265 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003266
Chris Lattner27a6c732007-11-24 07:07:01 +00003267 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003268 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003269
Chris Lattner27a6c732007-11-24 07:07:01 +00003270 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003271 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003272}
3273
Bob Wilson5bafff32009-06-22 23:27:02 +00003274static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3275 SDValue TmpOp0, TmpOp1;
3276 bool Invert = false;
3277 bool Swap = false;
3278 unsigned Opc = 0;
3279
3280 SDValue Op0 = Op.getOperand(0);
3281 SDValue Op1 = Op.getOperand(1);
3282 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003283 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003284 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3285 DebugLoc dl = Op.getDebugLoc();
3286
3287 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3288 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003289 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003290 case ISD::SETUNE:
3291 case ISD::SETNE: Invert = true; // Fallthrough
3292 case ISD::SETOEQ:
3293 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3294 case ISD::SETOLT:
3295 case ISD::SETLT: Swap = true; // Fallthrough
3296 case ISD::SETOGT:
3297 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3298 case ISD::SETOLE:
3299 case ISD::SETLE: Swap = true; // Fallthrough
3300 case ISD::SETOGE:
3301 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3302 case ISD::SETUGE: Swap = true; // Fallthrough
3303 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3304 case ISD::SETUGT: Swap = true; // Fallthrough
3305 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3306 case ISD::SETUEQ: Invert = true; // Fallthrough
3307 case ISD::SETONE:
3308 // Expand this to (OLT | OGT).
3309 TmpOp0 = Op0;
3310 TmpOp1 = Op1;
3311 Opc = ISD::OR;
3312 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3313 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3314 break;
3315 case ISD::SETUO: Invert = true; // Fallthrough
3316 case ISD::SETO:
3317 // Expand this to (OLT | OGE).
3318 TmpOp0 = Op0;
3319 TmpOp1 = Op1;
3320 Opc = ISD::OR;
3321 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3322 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3323 break;
3324 }
3325 } else {
3326 // Integer comparisons.
3327 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003328 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003329 case ISD::SETNE: Invert = true;
3330 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3331 case ISD::SETLT: Swap = true;
3332 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3333 case ISD::SETLE: Swap = true;
3334 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3335 case ISD::SETULT: Swap = true;
3336 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3337 case ISD::SETULE: Swap = true;
3338 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3339 }
3340
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003341 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003342 if (Opc == ARMISD::VCEQ) {
3343
3344 SDValue AndOp;
3345 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3346 AndOp = Op0;
3347 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3348 AndOp = Op1;
3349
3350 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003351 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003352 AndOp = AndOp.getOperand(0);
3353
3354 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3355 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003356 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3357 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003358 Invert = !Invert;
3359 }
3360 }
3361 }
3362
3363 if (Swap)
3364 std::swap(Op0, Op1);
3365
Owen Andersonc24cb352010-11-08 23:21:22 +00003366 // If one of the operands is a constant vector zero, attempt to fold the
3367 // comparison to a specialized compare-against-zero form.
3368 SDValue SingleOp;
3369 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3370 SingleOp = Op0;
3371 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3372 if (Opc == ARMISD::VCGE)
3373 Opc = ARMISD::VCLEZ;
3374 else if (Opc == ARMISD::VCGT)
3375 Opc = ARMISD::VCLTZ;
3376 SingleOp = Op1;
3377 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003378
Owen Andersonc24cb352010-11-08 23:21:22 +00003379 SDValue Result;
3380 if (SingleOp.getNode()) {
3381 switch (Opc) {
3382 case ARMISD::VCEQ:
3383 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3384 case ARMISD::VCGE:
3385 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3386 case ARMISD::VCLEZ:
3387 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3388 case ARMISD::VCGT:
3389 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3390 case ARMISD::VCLTZ:
3391 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3392 default:
3393 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3394 }
3395 } else {
3396 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3397 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003398
3399 if (Invert)
3400 Result = DAG.getNOT(dl, Result, VT);
3401
3402 return Result;
3403}
3404
Bob Wilsond3c42842010-06-14 22:19:57 +00003405/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3406/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003407/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003408static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3409 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003410 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003411 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003412
Bob Wilson827b2102010-06-15 19:05:35 +00003413 // SplatBitSize is set to the smallest size that splats the vector, so a
3414 // zero vector will always have SplatBitSize == 8. However, NEON modified
3415 // immediate instructions others than VMOV do not support the 8-bit encoding
3416 // of a zero vector, and the default encoding of zero is supposed to be the
3417 // 32-bit version.
3418 if (SplatBits == 0)
3419 SplatBitSize = 32;
3420
Bob Wilson5bafff32009-06-22 23:27:02 +00003421 switch (SplatBitSize) {
3422 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003423 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003424 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003425 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003426 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003427 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003428 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003429 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003430 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003431
3432 case 16:
3433 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003434 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003435 if ((SplatBits & ~0xff) == 0) {
3436 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003437 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003438 Imm = SplatBits;
3439 break;
3440 }
3441 if ((SplatBits & ~0xff00) == 0) {
3442 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003443 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003444 Imm = SplatBits >> 8;
3445 break;
3446 }
3447 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003448
3449 case 32:
3450 // NEON's 32-bit VMOV supports splat values where:
3451 // * only one byte is nonzero, or
3452 // * the least significant byte is 0xff and the second byte is nonzero, or
3453 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003454 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003455 if ((SplatBits & ~0xff) == 0) {
3456 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003457 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003458 Imm = SplatBits;
3459 break;
3460 }
3461 if ((SplatBits & ~0xff00) == 0) {
3462 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003463 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003464 Imm = SplatBits >> 8;
3465 break;
3466 }
3467 if ((SplatBits & ~0xff0000) == 0) {
3468 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003469 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003470 Imm = SplatBits >> 16;
3471 break;
3472 }
3473 if ((SplatBits & ~0xff000000) == 0) {
3474 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003475 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003476 Imm = SplatBits >> 24;
3477 break;
3478 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003479
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003480 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3481 if (type == OtherModImm) return SDValue();
3482
Bob Wilson5bafff32009-06-22 23:27:02 +00003483 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003484 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3485 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003486 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003487 Imm = SplatBits >> 8;
3488 SplatBits |= 0xff;
3489 break;
3490 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003491
3492 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003493 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3494 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003495 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003496 Imm = SplatBits >> 16;
3497 SplatBits |= 0xffff;
3498 break;
3499 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003500
3501 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3502 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3503 // VMOV.I32. A (very) minor optimization would be to replicate the value
3504 // and fall through here to test for a valid 64-bit splat. But, then the
3505 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003506 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003507
3508 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003509 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003510 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003511 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003512 uint64_t BitMask = 0xff;
3513 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003514 unsigned ImmMask = 1;
3515 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003516 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003517 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003518 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003519 Imm |= ImmMask;
3520 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003521 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003522 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003523 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003524 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003525 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003526 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003527 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003528 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003529 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003530 break;
3531 }
3532
Bob Wilson1a913ed2010-06-11 21:34:50 +00003533 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003534 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003535 return SDValue();
3536 }
3537
Bob Wilsoncba270d2010-07-13 21:16:48 +00003538 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3539 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003540}
3541
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003542static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3543 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003544 unsigned NumElts = VT.getVectorNumElements();
3545 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003546
3547 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3548 if (M[0] < 0)
3549 return false;
3550
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003551 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003552
3553 // If this is a VEXT shuffle, the immediate value is the index of the first
3554 // element. The other shuffle indices must be the successive elements after
3555 // the first one.
3556 unsigned ExpectedElt = Imm;
3557 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003558 // Increment the expected index. If it wraps around, it may still be
3559 // a VEXT but the source vectors must be swapped.
3560 ExpectedElt += 1;
3561 if (ExpectedElt == NumElts * 2) {
3562 ExpectedElt = 0;
3563 ReverseVEXT = true;
3564 }
3565
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003566 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003567 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003568 return false;
3569 }
3570
3571 // Adjust the index value if the source operands will be swapped.
3572 if (ReverseVEXT)
3573 Imm -= NumElts;
3574
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003575 return true;
3576}
3577
Bob Wilson8bb9e482009-07-26 00:39:34 +00003578/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3579/// instruction with the specified blocksize. (The order of the elements
3580/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003581static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3582 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003583 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3584 "Only possible block sizes for VREV are: 16, 32, 64");
3585
Bob Wilson8bb9e482009-07-26 00:39:34 +00003586 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003587 if (EltSz == 64)
3588 return false;
3589
3590 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003591 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003592 // If the first shuffle index is UNDEF, be optimistic.
3593 if (M[0] < 0)
3594 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003595
3596 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3597 return false;
3598
3599 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003600 if (M[i] < 0) continue; // ignore UNDEF indices
3601 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003602 return false;
3603 }
3604
3605 return true;
3606}
3607
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003608static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3609 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3610 // range, then 0 is placed into the resulting vector. So pretty much any mask
3611 // of 8 elements can work here.
3612 return VT == MVT::v8i8 && M.size() == 8;
3613}
3614
Bob Wilsonc692cb72009-08-21 20:54:19 +00003615static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3616 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003617 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3618 if (EltSz == 64)
3619 return false;
3620
Bob Wilsonc692cb72009-08-21 20:54:19 +00003621 unsigned NumElts = VT.getVectorNumElements();
3622 WhichResult = (M[0] == 0 ? 0 : 1);
3623 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003624 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3625 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003626 return false;
3627 }
3628 return true;
3629}
3630
Bob Wilson324f4f12009-12-03 06:40:55 +00003631/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3632/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3633/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3634static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3635 unsigned &WhichResult) {
3636 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3637 if (EltSz == 64)
3638 return false;
3639
3640 unsigned NumElts = VT.getVectorNumElements();
3641 WhichResult = (M[0] == 0 ? 0 : 1);
3642 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003643 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3644 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003645 return false;
3646 }
3647 return true;
3648}
3649
Bob Wilsonc692cb72009-08-21 20:54:19 +00003650static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3651 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003652 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3653 if (EltSz == 64)
3654 return false;
3655
Bob Wilsonc692cb72009-08-21 20:54:19 +00003656 unsigned NumElts = VT.getVectorNumElements();
3657 WhichResult = (M[0] == 0 ? 0 : 1);
3658 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003659 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003660 if ((unsigned) M[i] != 2 * i + WhichResult)
3661 return false;
3662 }
3663
3664 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003665 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003666 return false;
3667
3668 return true;
3669}
3670
Bob Wilson324f4f12009-12-03 06:40:55 +00003671/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3672/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3673/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3674static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3675 unsigned &WhichResult) {
3676 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3677 if (EltSz == 64)
3678 return false;
3679
3680 unsigned Half = VT.getVectorNumElements() / 2;
3681 WhichResult = (M[0] == 0 ? 0 : 1);
3682 for (unsigned j = 0; j != 2; ++j) {
3683 unsigned Idx = WhichResult;
3684 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003685 int MIdx = M[i + j * Half];
3686 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003687 return false;
3688 Idx += 2;
3689 }
3690 }
3691
3692 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3693 if (VT.is64BitVector() && EltSz == 32)
3694 return false;
3695
3696 return true;
3697}
3698
Bob Wilsonc692cb72009-08-21 20:54:19 +00003699static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3700 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003701 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3702 if (EltSz == 64)
3703 return false;
3704
Bob Wilsonc692cb72009-08-21 20:54:19 +00003705 unsigned NumElts = VT.getVectorNumElements();
3706 WhichResult = (M[0] == 0 ? 0 : 1);
3707 unsigned Idx = WhichResult * NumElts / 2;
3708 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003709 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3710 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003711 return false;
3712 Idx += 1;
3713 }
3714
3715 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003716 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003717 return false;
3718
3719 return true;
3720}
3721
Bob Wilson324f4f12009-12-03 06:40:55 +00003722/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3723/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3724/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3725static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3726 unsigned &WhichResult) {
3727 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3728 if (EltSz == 64)
3729 return false;
3730
3731 unsigned NumElts = VT.getVectorNumElements();
3732 WhichResult = (M[0] == 0 ? 0 : 1);
3733 unsigned Idx = WhichResult * NumElts / 2;
3734 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003735 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3736 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003737 return false;
3738 Idx += 1;
3739 }
3740
3741 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3742 if (VT.is64BitVector() && EltSz == 32)
3743 return false;
3744
3745 return true;
3746}
3747
Dale Johannesenf630c712010-07-29 20:10:08 +00003748// If N is an integer constant that can be moved into a register in one
3749// instruction, return an SDValue of such a constant (will become a MOV
3750// instruction). Otherwise return null.
3751static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3752 const ARMSubtarget *ST, DebugLoc dl) {
3753 uint64_t Val;
3754 if (!isa<ConstantSDNode>(N))
3755 return SDValue();
3756 Val = cast<ConstantSDNode>(N)->getZExtValue();
3757
3758 if (ST->isThumb1Only()) {
3759 if (Val <= 255 || ~Val <= 255)
3760 return DAG.getConstant(Val, MVT::i32);
3761 } else {
3762 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3763 return DAG.getConstant(Val, MVT::i32);
3764 }
3765 return SDValue();
3766}
3767
Bob Wilson5bafff32009-06-22 23:27:02 +00003768// If this is a case we can't handle, return null and let the default
3769// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003770SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3771 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003772 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003773 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003774 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003775
3776 APInt SplatBits, SplatUndef;
3777 unsigned SplatBitSize;
3778 bool HasAnyUndefs;
3779 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003780 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003781 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003782 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003783 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003784 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003785 DAG, VmovVT, VT.is128BitVector(),
3786 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003787 if (Val.getNode()) {
3788 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003789 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003790 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003791
3792 // Try an immediate VMVN.
3793 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3794 ((1LL << SplatBitSize) - 1));
3795 Val = isNEONModifiedImm(NegatedImm,
3796 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003797 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003798 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003799 if (Val.getNode()) {
3800 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003801 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003802 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003803 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003804 }
3805
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003806 // Scan through the operands to see if only one value is used.
3807 unsigned NumElts = VT.getVectorNumElements();
3808 bool isOnlyLowElement = true;
3809 bool usesOnlyOneValue = true;
3810 bool isConstant = true;
3811 SDValue Value;
3812 for (unsigned i = 0; i < NumElts; ++i) {
3813 SDValue V = Op.getOperand(i);
3814 if (V.getOpcode() == ISD::UNDEF)
3815 continue;
3816 if (i > 0)
3817 isOnlyLowElement = false;
3818 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3819 isConstant = false;
3820
3821 if (!Value.getNode())
3822 Value = V;
3823 else if (V != Value)
3824 usesOnlyOneValue = false;
3825 }
3826
3827 if (!Value.getNode())
3828 return DAG.getUNDEF(VT);
3829
3830 if (isOnlyLowElement)
3831 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3832
Dale Johannesenf630c712010-07-29 20:10:08 +00003833 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3834
Dale Johannesen575cd142010-10-19 20:00:17 +00003835 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3836 // i32 and try again.
3837 if (usesOnlyOneValue && EltSize <= 32) {
3838 if (!isConstant)
3839 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3840 if (VT.getVectorElementType().isFloatingPoint()) {
3841 SmallVector<SDValue, 8> Ops;
3842 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003843 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003844 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003845 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3846 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003847 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3848 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003849 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003850 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003851 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3852 if (Val.getNode())
3853 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003854 }
3855
3856 // If all elements are constants and the case above didn't get hit, fall back
3857 // to the default expansion, which will generate a load from the constant
3858 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003859 if (isConstant)
3860 return SDValue();
3861
Bob Wilson11a1dff2011-01-07 21:37:30 +00003862 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3863 if (NumElts >= 4) {
3864 SDValue shuffle = ReconstructShuffle(Op, DAG);
3865 if (shuffle != SDValue())
3866 return shuffle;
3867 }
3868
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003869 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003870 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3871 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003872 if (EltSize >= 32) {
3873 // Do the expansion with floating-point types, since that is what the VFP
3874 // registers are defined to use, and since i64 is not legal.
3875 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3876 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003877 SmallVector<SDValue, 8> Ops;
3878 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003879 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003880 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003881 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003882 }
3883
3884 return SDValue();
3885}
3886
Bob Wilson11a1dff2011-01-07 21:37:30 +00003887// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003888// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00003889SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
3890 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00003891 DebugLoc dl = Op.getDebugLoc();
3892 EVT VT = Op.getValueType();
3893 unsigned NumElts = VT.getVectorNumElements();
3894
3895 SmallVector<SDValue, 2> SourceVecs;
3896 SmallVector<unsigned, 2> MinElts;
3897 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003898
Bob Wilson11a1dff2011-01-07 21:37:30 +00003899 for (unsigned i = 0; i < NumElts; ++i) {
3900 SDValue V = Op.getOperand(i);
3901 if (V.getOpcode() == ISD::UNDEF)
3902 continue;
3903 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
3904 // A shuffle can only come from building a vector from various
3905 // elements of other vectors.
3906 return SDValue();
3907 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003908
Bob Wilson11a1dff2011-01-07 21:37:30 +00003909 // Record this extraction against the appropriate vector if possible...
3910 SDValue SourceVec = V.getOperand(0);
3911 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
3912 bool FoundSource = false;
3913 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
3914 if (SourceVecs[j] == SourceVec) {
3915 if (MinElts[j] > EltNo)
3916 MinElts[j] = EltNo;
3917 if (MaxElts[j] < EltNo)
3918 MaxElts[j] = EltNo;
3919 FoundSource = true;
3920 break;
3921 }
3922 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003923
Bob Wilson11a1dff2011-01-07 21:37:30 +00003924 // Or record a new source if not...
3925 if (!FoundSource) {
3926 SourceVecs.push_back(SourceVec);
3927 MinElts.push_back(EltNo);
3928 MaxElts.push_back(EltNo);
3929 }
3930 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003931
Bob Wilson11a1dff2011-01-07 21:37:30 +00003932 // Currently only do something sane when at most two source vectors
3933 // involved.
3934 if (SourceVecs.size() > 2)
3935 return SDValue();
3936
3937 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
3938 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003939
Bob Wilson11a1dff2011-01-07 21:37:30 +00003940 // This loop extracts the usage patterns of the source vectors
3941 // and prepares appropriate SDValues for a shuffle if possible.
3942 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
3943 if (SourceVecs[i].getValueType() == VT) {
3944 // No VEXT necessary
3945 ShuffleSrcs[i] = SourceVecs[i];
3946 VEXTOffsets[i] = 0;
3947 continue;
3948 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
3949 // It probably isn't worth padding out a smaller vector just to
3950 // break it down again in a shuffle.
3951 return SDValue();
3952 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003953
Bob Wilson11a1dff2011-01-07 21:37:30 +00003954 // Since only 64-bit and 128-bit vectors are legal on ARM and
3955 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00003956 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
3957 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003958
Bob Wilson11a1dff2011-01-07 21:37:30 +00003959 if (MaxElts[i] - MinElts[i] >= NumElts) {
3960 // Span too large for a VEXT to cope
3961 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003962 }
3963
Bob Wilson11a1dff2011-01-07 21:37:30 +00003964 if (MinElts[i] >= NumElts) {
3965 // The extraction can just take the second half
3966 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00003967 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3968 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003969 DAG.getIntPtrConstant(NumElts));
3970 } else if (MaxElts[i] < NumElts) {
3971 // The extraction can just take the first half
3972 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00003973 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3974 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003975 DAG.getIntPtrConstant(0));
3976 } else {
3977 // An actual VEXT is needed
3978 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00003979 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3980 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003981 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00003982 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3983 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003984 DAG.getIntPtrConstant(NumElts));
3985 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
3986 DAG.getConstant(VEXTOffsets[i], MVT::i32));
3987 }
3988 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003989
Bob Wilson11a1dff2011-01-07 21:37:30 +00003990 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003991
Bob Wilson11a1dff2011-01-07 21:37:30 +00003992 for (unsigned i = 0; i < NumElts; ++i) {
3993 SDValue Entry = Op.getOperand(i);
3994 if (Entry.getOpcode() == ISD::UNDEF) {
3995 Mask.push_back(-1);
3996 continue;
3997 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003998
Bob Wilson11a1dff2011-01-07 21:37:30 +00003999 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004000 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4001 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004002 if (ExtractVec == SourceVecs[0]) {
4003 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4004 } else {
4005 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4006 }
4007 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004008
Bob Wilson11a1dff2011-01-07 21:37:30 +00004009 // Final check before we try to produce nonsense...
4010 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004011 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4012 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004013
Bob Wilson11a1dff2011-01-07 21:37:30 +00004014 return SDValue();
4015}
4016
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004017/// isShuffleMaskLegal - Targets can use this to indicate that they only
4018/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4019/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4020/// are assumed to be legal.
4021bool
4022ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4023 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004024 if (VT.getVectorNumElements() == 4 &&
4025 (VT.is128BitVector() || VT.is64BitVector())) {
4026 unsigned PFIndexes[4];
4027 for (unsigned i = 0; i != 4; ++i) {
4028 if (M[i] < 0)
4029 PFIndexes[i] = 8;
4030 else
4031 PFIndexes[i] = M[i];
4032 }
4033
4034 // Compute the index in the perfect shuffle table.
4035 unsigned PFTableIndex =
4036 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4037 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4038 unsigned Cost = (PFEntry >> 30);
4039
4040 if (Cost <= 4)
4041 return true;
4042 }
4043
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004044 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004045 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004046
Bob Wilson53dd2452010-06-07 23:53:38 +00004047 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4048 return (EltSize >= 32 ||
4049 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004050 isVREVMask(M, VT, 64) ||
4051 isVREVMask(M, VT, 32) ||
4052 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004053 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004054 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004055 isVTRNMask(M, VT, WhichResult) ||
4056 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004057 isVZIPMask(M, VT, WhichResult) ||
4058 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4059 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4060 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004061}
4062
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004063/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4064/// the specified operations to build the shuffle.
4065static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4066 SDValue RHS, SelectionDAG &DAG,
4067 DebugLoc dl) {
4068 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4069 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4070 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4071
4072 enum {
4073 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4074 OP_VREV,
4075 OP_VDUP0,
4076 OP_VDUP1,
4077 OP_VDUP2,
4078 OP_VDUP3,
4079 OP_VEXT1,
4080 OP_VEXT2,
4081 OP_VEXT3,
4082 OP_VUZPL, // VUZP, left result
4083 OP_VUZPR, // VUZP, right result
4084 OP_VZIPL, // VZIP, left result
4085 OP_VZIPR, // VZIP, right result
4086 OP_VTRNL, // VTRN, left result
4087 OP_VTRNR // VTRN, right result
4088 };
4089
4090 if (OpNum == OP_COPY) {
4091 if (LHSID == (1*9+2)*9+3) return LHS;
4092 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4093 return RHS;
4094 }
4095
4096 SDValue OpLHS, OpRHS;
4097 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4098 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4099 EVT VT = OpLHS.getValueType();
4100
4101 switch (OpNum) {
4102 default: llvm_unreachable("Unknown shuffle opcode!");
4103 case OP_VREV:
4104 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4105 case OP_VDUP0:
4106 case OP_VDUP1:
4107 case OP_VDUP2:
4108 case OP_VDUP3:
4109 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004110 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004111 case OP_VEXT1:
4112 case OP_VEXT2:
4113 case OP_VEXT3:
4114 return DAG.getNode(ARMISD::VEXT, dl, VT,
4115 OpLHS, OpRHS,
4116 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4117 case OP_VUZPL:
4118 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004119 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004120 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4121 case OP_VZIPL:
4122 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004123 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004124 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4125 case OP_VTRNL:
4126 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004127 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4128 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004129 }
4130}
4131
Bill Wendling69a05a72011-03-14 23:02:38 +00004132static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4133 SmallVectorImpl<int> &ShuffleMask,
4134 SelectionDAG &DAG) {
4135 // Check to see if we can use the VTBL instruction.
4136 SDValue V1 = Op.getOperand(0);
4137 SDValue V2 = Op.getOperand(1);
4138 DebugLoc DL = Op.getDebugLoc();
4139
4140 SmallVector<SDValue, 8> VTBLMask;
4141 for (SmallVectorImpl<int>::iterator
4142 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4143 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4144
4145 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4146 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4147 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4148 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004149
4150 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
4151 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4152 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004153}
4154
Bob Wilson5bafff32009-06-22 23:27:02 +00004155static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004156 SDValue V1 = Op.getOperand(0);
4157 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004158 DebugLoc dl = Op.getDebugLoc();
4159 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004160 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004161 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004162
Bob Wilson28865062009-08-13 02:13:04 +00004163 // Convert shuffles that are directly supported on NEON to target-specific
4164 // DAG nodes, instead of keeping them as shuffles and matching them again
4165 // during code selection. This is more efficient and avoids the possibility
4166 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004167 // FIXME: floating-point vectors should be canonicalized to integer vectors
4168 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004169 SVN->getMask(ShuffleMask);
4170
Bob Wilson53dd2452010-06-07 23:53:38 +00004171 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4172 if (EltSize <= 32) {
4173 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4174 int Lane = SVN->getSplatIndex();
4175 // If this is undef splat, generate it via "just" vdup, if possible.
4176 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004177
Bob Wilson53dd2452010-06-07 23:53:38 +00004178 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4179 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4180 }
4181 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4182 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004183 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004184
4185 bool ReverseVEXT;
4186 unsigned Imm;
4187 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4188 if (ReverseVEXT)
4189 std::swap(V1, V2);
4190 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4191 DAG.getConstant(Imm, MVT::i32));
4192 }
4193
4194 if (isVREVMask(ShuffleMask, VT, 64))
4195 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4196 if (isVREVMask(ShuffleMask, VT, 32))
4197 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4198 if (isVREVMask(ShuffleMask, VT, 16))
4199 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4200
4201 // Check for Neon shuffles that modify both input vectors in place.
4202 // If both results are used, i.e., if there are two shuffles with the same
4203 // source operands and with masks corresponding to both results of one of
4204 // these operations, DAG memoization will ensure that a single node is
4205 // used for both shuffles.
4206 unsigned WhichResult;
4207 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4208 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4209 V1, V2).getValue(WhichResult);
4210 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4211 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4212 V1, V2).getValue(WhichResult);
4213 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4214 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4215 V1, V2).getValue(WhichResult);
4216
4217 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4218 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4219 V1, V1).getValue(WhichResult);
4220 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4221 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4222 V1, V1).getValue(WhichResult);
4223 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4224 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4225 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004226 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004227
Bob Wilsonc692cb72009-08-21 20:54:19 +00004228 // If the shuffle is not directly supported and it has 4 elements, use
4229 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004230 unsigned NumElts = VT.getVectorNumElements();
4231 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004232 unsigned PFIndexes[4];
4233 for (unsigned i = 0; i != 4; ++i) {
4234 if (ShuffleMask[i] < 0)
4235 PFIndexes[i] = 8;
4236 else
4237 PFIndexes[i] = ShuffleMask[i];
4238 }
4239
4240 // Compute the index in the perfect shuffle table.
4241 unsigned PFTableIndex =
4242 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004243 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4244 unsigned Cost = (PFEntry >> 30);
4245
4246 if (Cost <= 4)
4247 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4248 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004249
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004250 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004251 if (EltSize >= 32) {
4252 // Do the expansion with floating-point types, since that is what the VFP
4253 // registers are defined to use, and since i64 is not legal.
4254 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4255 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004256 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4257 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004258 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004259 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004260 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004261 Ops.push_back(DAG.getUNDEF(EltVT));
4262 else
4263 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4264 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4265 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4266 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004267 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004268 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004269 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004270 }
4271
Bill Wendling69a05a72011-03-14 23:02:38 +00004272 if (VT == MVT::v8i8) {
4273 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4274 if (NewOp.getNode())
4275 return NewOp;
4276 }
4277
Bob Wilson22cac0d2009-08-14 05:16:33 +00004278 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004279}
4280
Bob Wilson5bafff32009-06-22 23:27:02 +00004281static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004282 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004283 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004284 if (!isa<ConstantSDNode>(Lane))
4285 return SDValue();
4286
4287 SDValue Vec = Op.getOperand(0);
4288 if (Op.getValueType() == MVT::i32 &&
4289 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4290 DebugLoc dl = Op.getDebugLoc();
4291 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4292 }
4293
4294 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004295}
4296
Bob Wilsona6d65862009-08-03 20:36:38 +00004297static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4298 // The only time a CONCAT_VECTORS operation can have legal types is when
4299 // two 64-bit vectors are concatenated to a 128-bit vector.
4300 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4301 "unexpected CONCAT_VECTORS");
4302 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004303 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004304 SDValue Op0 = Op.getOperand(0);
4305 SDValue Op1 = Op.getOperand(1);
4306 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004307 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004308 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004309 DAG.getIntPtrConstant(0));
4310 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004311 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004312 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004313 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004314 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004315}
4316
Bob Wilson626613d2010-11-23 19:38:38 +00004317/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4318/// element has been zero/sign-extended, depending on the isSigned parameter,
4319/// from an integer type half its size.
4320static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4321 bool isSigned) {
4322 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4323 EVT VT = N->getValueType(0);
4324 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4325 SDNode *BVN = N->getOperand(0).getNode();
4326 if (BVN->getValueType(0) != MVT::v4i32 ||
4327 BVN->getOpcode() != ISD::BUILD_VECTOR)
4328 return false;
4329 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4330 unsigned HiElt = 1 - LoElt;
4331 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4332 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4333 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4334 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4335 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4336 return false;
4337 if (isSigned) {
4338 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4339 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4340 return true;
4341 } else {
4342 if (Hi0->isNullValue() && Hi1->isNullValue())
4343 return true;
4344 }
4345 return false;
4346 }
4347
4348 if (N->getOpcode() != ISD::BUILD_VECTOR)
4349 return false;
4350
4351 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4352 SDNode *Elt = N->getOperand(i).getNode();
4353 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4354 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4355 unsigned HalfSize = EltSize / 2;
4356 if (isSigned) {
4357 int64_t SExtVal = C->getSExtValue();
4358 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4359 return false;
4360 } else {
4361 if ((C->getZExtValue() >> HalfSize) != 0)
4362 return false;
4363 }
4364 continue;
4365 }
4366 return false;
4367 }
4368
4369 return true;
4370}
4371
4372/// isSignExtended - Check if a node is a vector value that is sign-extended
4373/// or a constant BUILD_VECTOR with sign-extended elements.
4374static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4375 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4376 return true;
4377 if (isExtendedBUILD_VECTOR(N, DAG, true))
4378 return true;
4379 return false;
4380}
4381
4382/// isZeroExtended - Check if a node is a vector value that is zero-extended
4383/// or a constant BUILD_VECTOR with zero-extended elements.
4384static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4385 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4386 return true;
4387 if (isExtendedBUILD_VECTOR(N, DAG, false))
4388 return true;
4389 return false;
4390}
4391
4392/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4393/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004394static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4395 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4396 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004397 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4398 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4399 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4400 LD->isNonTemporal(), LD->getAlignment());
4401 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4402 // have been legalized as a BITCAST from v4i32.
4403 if (N->getOpcode() == ISD::BITCAST) {
4404 SDNode *BVN = N->getOperand(0).getNode();
4405 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4406 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4407 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4408 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4409 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4410 }
4411 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4412 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4413 EVT VT = N->getValueType(0);
4414 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4415 unsigned NumElts = VT.getVectorNumElements();
4416 MVT TruncVT = MVT::getIntegerVT(EltSize);
4417 SmallVector<SDValue, 8> Ops;
4418 for (unsigned i = 0; i != NumElts; ++i) {
4419 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4420 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004421 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004422 }
4423 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4424 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004425}
4426
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004427static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4428 unsigned Opcode = N->getOpcode();
4429 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4430 SDNode *N0 = N->getOperand(0).getNode();
4431 SDNode *N1 = N->getOperand(1).getNode();
4432 return N0->hasOneUse() && N1->hasOneUse() &&
4433 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4434 }
4435 return false;
4436}
4437
4438static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4439 unsigned Opcode = N->getOpcode();
4440 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4441 SDNode *N0 = N->getOperand(0).getNode();
4442 SDNode *N1 = N->getOperand(1).getNode();
4443 return N0->hasOneUse() && N1->hasOneUse() &&
4444 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4445 }
4446 return false;
4447}
4448
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004449static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4450 // Multiplications are only custom-lowered for 128-bit vectors so that
4451 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4452 EVT VT = Op.getValueType();
4453 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4454 SDNode *N0 = Op.getOperand(0).getNode();
4455 SDNode *N1 = Op.getOperand(1).getNode();
4456 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004457 bool isMLA = false;
4458 bool isN0SExt = isSignExtended(N0, DAG);
4459 bool isN1SExt = isSignExtended(N1, DAG);
4460 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004461 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004462 else {
4463 bool isN0ZExt = isZeroExtended(N0, DAG);
4464 bool isN1ZExt = isZeroExtended(N1, DAG);
4465 if (isN0ZExt && isN1ZExt)
4466 NewOpc = ARMISD::VMULLu;
4467 else if (isN1SExt || isN1ZExt) {
4468 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4469 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4470 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4471 NewOpc = ARMISD::VMULLs;
4472 isMLA = true;
4473 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4474 NewOpc = ARMISD::VMULLu;
4475 isMLA = true;
4476 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4477 std::swap(N0, N1);
4478 NewOpc = ARMISD::VMULLu;
4479 isMLA = true;
4480 }
4481 }
4482
4483 if (!NewOpc) {
4484 if (VT == MVT::v2i64)
4485 // Fall through to expand this. It is not legal.
4486 return SDValue();
4487 else
4488 // Other vector multiplications are legal.
4489 return Op;
4490 }
4491 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004492
4493 // Legalize to a VMULL instruction.
4494 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004495 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004496 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004497 if (!isMLA) {
4498 Op0 = SkipExtension(N0, DAG);
4499 assert(Op0.getValueType().is64BitVector() &&
4500 Op1.getValueType().is64BitVector() &&
4501 "unexpected types for extended operands to VMULL");
4502 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4503 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004504
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004505 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4506 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4507 // vmull q0, d4, d6
4508 // vmlal q0, d5, d6
4509 // is faster than
4510 // vaddl q0, d4, d5
4511 // vmovl q1, d6
4512 // vmul q0, q0, q1
4513 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4514 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4515 EVT Op1VT = Op1.getValueType();
4516 return DAG.getNode(N0->getOpcode(), DL, VT,
4517 DAG.getNode(NewOpc, DL, VT,
4518 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4519 DAG.getNode(NewOpc, DL, VT,
4520 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004521}
4522
Nate Begeman7973f352011-02-11 20:53:29 +00004523static SDValue
4524LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4525 // Convert to float
4526 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4527 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4528 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4529 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4530 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4531 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4532 // Get reciprocal estimate.
4533 // float4 recip = vrecpeq_f32(yf);
4534 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
4535 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4536 // Because char has a smaller range than uchar, we can actually get away
4537 // without any newton steps. This requires that we use a weird bias
4538 // of 0xb000, however (again, this has been exhaustively tested).
4539 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4540 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4541 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4542 Y = DAG.getConstant(0xb000, MVT::i32);
4543 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4544 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4545 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4546 // Convert back to short.
4547 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4548 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4549 return X;
4550}
4551
4552static SDValue
4553LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4554 SDValue N2;
4555 // Convert to float.
4556 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4557 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4558 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4559 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4560 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4561 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
4562
4563 // Use reciprocal estimate and one refinement step.
4564 // float4 recip = vrecpeq_f32(yf);
4565 // recip *= vrecpsq_f32(yf, recip);
4566 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
4567 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
4568 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
4569 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4570 N1, N2);
4571 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4572 // Because short has a smaller range than ushort, we can actually get away
4573 // with only a single newton step. This requires that we use a weird bias
4574 // of 89, however (again, this has been exhaustively tested).
4575 // float4 result = as_float4(as_int4(xf*recip) + 89);
4576 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4577 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4578 N1 = DAG.getConstant(89, MVT::i32);
4579 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4580 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4581 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4582 // Convert back to integer and return.
4583 // return vmovn_s32(vcvt_s32_f32(result));
4584 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4585 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4586 return N0;
4587}
4588
4589static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4590 EVT VT = Op.getValueType();
4591 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4592 "unexpected type for custom-lowering ISD::SDIV");
4593
4594 DebugLoc dl = Op.getDebugLoc();
4595 SDValue N0 = Op.getOperand(0);
4596 SDValue N1 = Op.getOperand(1);
4597 SDValue N2, N3;
4598
4599 if (VT == MVT::v8i8) {
4600 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4601 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
4602
4603 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4604 DAG.getIntPtrConstant(4));
4605 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4606 DAG.getIntPtrConstant(4));
4607 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4608 DAG.getIntPtrConstant(0));
4609 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4610 DAG.getIntPtrConstant(0));
4611
4612 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4613 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4614
4615 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4616 N0 = LowerCONCAT_VECTORS(N0, DAG);
4617
4618 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4619 return N0;
4620 }
4621 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4622}
4623
4624static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4625 EVT VT = Op.getValueType();
4626 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4627 "unexpected type for custom-lowering ISD::UDIV");
4628
4629 DebugLoc dl = Op.getDebugLoc();
4630 SDValue N0 = Op.getOperand(0);
4631 SDValue N1 = Op.getOperand(1);
4632 SDValue N2, N3;
4633
4634 if (VT == MVT::v8i8) {
4635 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4636 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
4637
4638 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4639 DAG.getIntPtrConstant(4));
4640 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4641 DAG.getIntPtrConstant(4));
4642 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4643 DAG.getIntPtrConstant(0));
4644 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4645 DAG.getIntPtrConstant(0));
4646
4647 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4648 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
4649
4650 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4651 N0 = LowerCONCAT_VECTORS(N0, DAG);
4652
4653 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
4654 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4655 N0);
4656 return N0;
4657 }
4658
4659 // v4i16 sdiv ... Convert to float.
4660 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4661 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4662 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4663 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4664 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4665 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
4666
4667 // Use reciprocal estimate and two refinement steps.
4668 // float4 recip = vrecpeq_f32(yf);
4669 // recip *= vrecpsq_f32(yf, recip);
4670 // recip *= vrecpsq_f32(yf, recip);
4671 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
4672 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
4673 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
4674 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4675 N1, N2);
4676 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4677 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
4678 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4679 N1, N2);
4680 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4681 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4682 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4683 // and that it will never cause us to return an answer too large).
4684 // float4 result = as_float4(as_int4(xf*recip) + 89);
4685 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4686 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4687 N1 = DAG.getConstant(2, MVT::i32);
4688 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4689 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4690 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4691 // Convert back to integer and return.
4692 // return vmovn_u32(vcvt_s32_f32(result));
4693 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4694 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4695 return N0;
4696}
4697
Dan Gohmand858e902010-04-17 15:26:15 +00004698SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004699 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004700 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004701 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004702 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004703 case ISD::GlobalAddress:
4704 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4705 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004706 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004707 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004708 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4709 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004710 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004711 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004712 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004713 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004714 case ISD::SINT_TO_FP:
4715 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4716 case ISD::FP_TO_SINT:
4717 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004718 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004719 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004720 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004721 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004722 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004723 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004724 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004725 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4726 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00004727 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004728 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004729 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004730 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004731 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004732 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004733 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004734 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004735 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004736 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004737 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004738 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004739 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004740 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004741 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00004742 case ISD::SDIV: return LowerSDIV(Op, DAG);
4743 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004744 }
Dan Gohman475871a2008-07-27 21:46:04 +00004745 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004746}
4747
Duncan Sands1607f052008-12-01 11:39:25 +00004748/// ReplaceNodeResults - Replace the results of node with an illegal result
4749/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004750void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4751 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004752 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004753 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004754 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004755 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004756 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004757 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004758 case ISD::BITCAST:
4759 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004760 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004761 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004762 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004763 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004764 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004765 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004766 if (Res.getNode())
4767 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004768}
Chris Lattner27a6c732007-11-24 07:07:01 +00004769
Evan Chenga8e29892007-01-19 07:51:42 +00004770//===----------------------------------------------------------------------===//
4771// ARM Scheduler Hooks
4772//===----------------------------------------------------------------------===//
4773
4774MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004775ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4776 MachineBasicBlock *BB,
4777 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004778 unsigned dest = MI->getOperand(0).getReg();
4779 unsigned ptr = MI->getOperand(1).getReg();
4780 unsigned oldval = MI->getOperand(2).getReg();
4781 unsigned newval = MI->getOperand(3).getReg();
4782 unsigned scratch = BB->getParent()->getRegInfo()
4783 .createVirtualRegister(ARM::GPRRegisterClass);
4784 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4785 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004786 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004787
4788 unsigned ldrOpc, strOpc;
4789 switch (Size) {
4790 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004791 case 1:
4792 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00004793 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004794 break;
4795 case 2:
4796 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4797 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4798 break;
4799 case 4:
4800 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4801 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4802 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004803 }
4804
4805 MachineFunction *MF = BB->getParent();
4806 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4807 MachineFunction::iterator It = BB;
4808 ++It; // insert the new blocks after the current block
4809
4810 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4811 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4812 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4813 MF->insert(It, loop1MBB);
4814 MF->insert(It, loop2MBB);
4815 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004816
4817 // Transfer the remainder of BB and its successor edges to exitMBB.
4818 exitMBB->splice(exitMBB->begin(), BB,
4819 llvm::next(MachineBasicBlock::iterator(MI)),
4820 BB->end());
4821 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004822
4823 // thisMBB:
4824 // ...
4825 // fallthrough --> loop1MBB
4826 BB->addSuccessor(loop1MBB);
4827
4828 // loop1MBB:
4829 // ldrex dest, [ptr]
4830 // cmp dest, oldval
4831 // bne exitMBB
4832 BB = loop1MBB;
4833 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004834 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004835 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004836 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4837 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004838 BB->addSuccessor(loop2MBB);
4839 BB->addSuccessor(exitMBB);
4840
4841 // loop2MBB:
4842 // strex scratch, newval, [ptr]
4843 // cmp scratch, #0
4844 // bne loop1MBB
4845 BB = loop2MBB;
4846 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
4847 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004848 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004849 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004850 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4851 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004852 BB->addSuccessor(loop1MBB);
4853 BB->addSuccessor(exitMBB);
4854
4855 // exitMBB:
4856 // ...
4857 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00004858
Dan Gohman14152b42010-07-06 20:24:04 +00004859 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00004860
Jim Grosbach5278eb82009-12-11 01:42:04 +00004861 return BB;
4862}
4863
4864MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004865ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
4866 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00004867 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
4868 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4869
4870 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004871 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004872 MachineFunction::iterator It = BB;
4873 ++It;
4874
4875 unsigned dest = MI->getOperand(0).getReg();
4876 unsigned ptr = MI->getOperand(1).getReg();
4877 unsigned incr = MI->getOperand(2).getReg();
4878 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00004879
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004880 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004881 unsigned ldrOpc, strOpc;
4882 switch (Size) {
4883 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004884 case 1:
4885 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00004886 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004887 break;
4888 case 2:
4889 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4890 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4891 break;
4892 case 4:
4893 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4894 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4895 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00004896 }
4897
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004898 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4899 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4900 MF->insert(It, loopMBB);
4901 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004902
4903 // Transfer the remainder of BB and its successor edges to exitMBB.
4904 exitMBB->splice(exitMBB->begin(), BB,
4905 llvm::next(MachineBasicBlock::iterator(MI)),
4906 BB->end());
4907 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004908
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004909 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004910 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4911 unsigned scratch2 = (!BinOpcode) ? incr :
4912 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4913
4914 // thisMBB:
4915 // ...
4916 // fallthrough --> loopMBB
4917 BB->addSuccessor(loopMBB);
4918
4919 // loopMBB:
4920 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004921 // <binop> scratch2, dest, incr
4922 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00004923 // cmp scratch, #0
4924 // bne- loopMBB
4925 // fallthrough --> exitMBB
4926 BB = loopMBB;
4927 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00004928 if (BinOpcode) {
4929 // operand order needs to go the other way for NAND
4930 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
4931 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
4932 addReg(incr).addReg(dest)).addReg(0);
4933 else
4934 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
4935 addReg(dest).addReg(incr)).addReg(0);
4936 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00004937
4938 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
4939 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004940 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00004941 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004942 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4943 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004944
4945 BB->addSuccessor(loopMBB);
4946 BB->addSuccessor(exitMBB);
4947
4948 // exitMBB:
4949 // ...
4950 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00004951
Dan Gohman14152b42010-07-06 20:24:04 +00004952 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00004953
Jim Grosbachc3c23542009-12-14 04:22:04 +00004954 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00004955}
4956
Evan Cheng218977b2010-07-13 19:27:42 +00004957static
4958MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
4959 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
4960 E = MBB->succ_end(); I != E; ++I)
4961 if (*I != Succ)
4962 return *I;
4963 llvm_unreachable("Expecting a BB with two successors!");
4964}
4965
Jim Grosbache801dc42009-12-12 01:40:06 +00004966MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00004967ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00004968 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004969 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00004970 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004971 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00004972 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00004973 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00004974 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00004975 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00004976
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004977 case ARM::ATOMIC_LOAD_ADD_I8:
4978 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4979 case ARM::ATOMIC_LOAD_ADD_I16:
4980 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4981 case ARM::ATOMIC_LOAD_ADD_I32:
4982 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004983
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004984 case ARM::ATOMIC_LOAD_AND_I8:
4985 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4986 case ARM::ATOMIC_LOAD_AND_I16:
4987 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4988 case ARM::ATOMIC_LOAD_AND_I32:
4989 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004990
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004991 case ARM::ATOMIC_LOAD_OR_I8:
4992 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4993 case ARM::ATOMIC_LOAD_OR_I16:
4994 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4995 case ARM::ATOMIC_LOAD_OR_I32:
4996 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004997
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004998 case ARM::ATOMIC_LOAD_XOR_I8:
4999 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5000 case ARM::ATOMIC_LOAD_XOR_I16:
5001 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5002 case ARM::ATOMIC_LOAD_XOR_I32:
5003 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005004
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005005 case ARM::ATOMIC_LOAD_NAND_I8:
5006 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5007 case ARM::ATOMIC_LOAD_NAND_I16:
5008 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5009 case ARM::ATOMIC_LOAD_NAND_I32:
5010 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005011
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005012 case ARM::ATOMIC_LOAD_SUB_I8:
5013 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5014 case ARM::ATOMIC_LOAD_SUB_I16:
5015 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5016 case ARM::ATOMIC_LOAD_SUB_I32:
5017 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005018
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005019 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
5020 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
5021 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00005022
5023 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
5024 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
5025 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005026
Evan Cheng007ea272009-08-12 05:17:19 +00005027 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00005028 // To "insert" a SELECT_CC instruction, we actually have to insert the
5029 // diamond control-flow pattern. The incoming instruction knows the
5030 // destination vreg to set, the condition code register to branch on, the
5031 // true/false values to select between, and a branch opcode to use.
5032 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005033 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00005034 ++It;
5035
5036 // thisMBB:
5037 // ...
5038 // TrueVal = ...
5039 // cmpTY ccX, r1, r2
5040 // bCC copy1MBB
5041 // fallthrough --> copy0MBB
5042 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005043 MachineFunction *F = BB->getParent();
5044 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
5045 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00005046 F->insert(It, copy0MBB);
5047 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005048
5049 // Transfer the remainder of BB and its successor edges to sinkMBB.
5050 sinkMBB->splice(sinkMBB->begin(), BB,
5051 llvm::next(MachineBasicBlock::iterator(MI)),
5052 BB->end());
5053 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
5054
Dan Gohman258c58c2010-07-06 15:49:48 +00005055 BB->addSuccessor(copy0MBB);
5056 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00005057
Dan Gohman14152b42010-07-06 20:24:04 +00005058 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
5059 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
5060
Evan Chenga8e29892007-01-19 07:51:42 +00005061 // copy0MBB:
5062 // %FalseValue = ...
5063 // # fallthrough to sinkMBB
5064 BB = copy0MBB;
5065
5066 // Update machine-CFG edges
5067 BB->addSuccessor(sinkMBB);
5068
5069 // sinkMBB:
5070 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5071 // ...
5072 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00005073 BuildMI(*BB, BB->begin(), dl,
5074 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00005075 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5076 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5077
Dan Gohman14152b42010-07-06 20:24:04 +00005078 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00005079 return BB;
5080 }
Evan Cheng86198642009-08-07 00:34:42 +00005081
Evan Cheng218977b2010-07-13 19:27:42 +00005082 case ARM::BCCi64:
5083 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00005084 // If there is an unconditional branch to the other successor, remove it.
5085 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00005086
Evan Cheng218977b2010-07-13 19:27:42 +00005087 // Compare both parts that make up the double comparison separately for
5088 // equality.
5089 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
5090
5091 unsigned LHS1 = MI->getOperand(1).getReg();
5092 unsigned LHS2 = MI->getOperand(2).getReg();
5093 if (RHSisZero) {
5094 AddDefaultPred(BuildMI(BB, dl,
5095 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5096 .addReg(LHS1).addImm(0));
5097 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5098 .addReg(LHS2).addImm(0)
5099 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5100 } else {
5101 unsigned RHS1 = MI->getOperand(3).getReg();
5102 unsigned RHS2 = MI->getOperand(4).getReg();
5103 AddDefaultPred(BuildMI(BB, dl,
5104 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5105 .addReg(LHS1).addReg(RHS1));
5106 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5107 .addReg(LHS2).addReg(RHS2)
5108 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5109 }
5110
5111 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
5112 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
5113 if (MI->getOperand(0).getImm() == ARMCC::NE)
5114 std::swap(destMBB, exitMBB);
5115
5116 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5117 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
5118 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
5119 .addMBB(exitMBB);
5120
5121 MI->eraseFromParent(); // The pseudo instruction is gone now.
5122 return BB;
5123 }
Evan Chenga8e29892007-01-19 07:51:42 +00005124 }
5125}
5126
5127//===----------------------------------------------------------------------===//
5128// ARM Optimization Hooks
5129//===----------------------------------------------------------------------===//
5130
Chris Lattnerd1980a52009-03-12 06:52:53 +00005131static
5132SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
5133 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005134 SelectionDAG &DAG = DCI.DAG;
5135 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00005136 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00005137 unsigned Opc = N->getOpcode();
5138 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
5139 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
5140 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
5141 ISD::CondCode CC = ISD::SETCC_INVALID;
5142
5143 if (isSlctCC) {
5144 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
5145 } else {
5146 SDValue CCOp = Slct.getOperand(0);
5147 if (CCOp.getOpcode() == ISD::SETCC)
5148 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
5149 }
5150
5151 bool DoXform = false;
5152 bool InvCC = false;
5153 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
5154 "Bad input!");
5155
5156 if (LHS.getOpcode() == ISD::Constant &&
5157 cast<ConstantSDNode>(LHS)->isNullValue()) {
5158 DoXform = true;
5159 } else if (CC != ISD::SETCC_INVALID &&
5160 RHS.getOpcode() == ISD::Constant &&
5161 cast<ConstantSDNode>(RHS)->isNullValue()) {
5162 std::swap(LHS, RHS);
5163 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005164 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00005165 Op0.getOperand(0).getValueType();
5166 bool isInt = OpVT.isInteger();
5167 CC = ISD::getSetCCInverse(CC, isInt);
5168
5169 if (!TLI.isCondCodeLegal(CC, OpVT))
5170 return SDValue(); // Inverse operator isn't legal.
5171
5172 DoXform = true;
5173 InvCC = true;
5174 }
5175
5176 if (DoXform) {
5177 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
5178 if (isSlctCC)
5179 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
5180 Slct.getOperand(0), Slct.getOperand(1), CC);
5181 SDValue CCOp = Slct.getOperand(0);
5182 if (InvCC)
5183 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
5184 CCOp.getOperand(0), CCOp.getOperand(1), CC);
5185 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
5186 CCOp, OtherOp, Result);
5187 }
5188 return SDValue();
5189}
5190
Bob Wilson3d5792a2010-07-29 20:34:14 +00005191/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
5192/// operands N0 and N1. This is a helper for PerformADDCombine that is
5193/// called with the default operands, and if that fails, with commuted
5194/// operands.
5195static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
5196 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005197 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
5198 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
5199 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
5200 if (Result.getNode()) return Result;
5201 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00005202 return SDValue();
5203}
5204
Bob Wilson3d5792a2010-07-29 20:34:14 +00005205/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
5206///
5207static SDValue PerformADDCombine(SDNode *N,
5208 TargetLowering::DAGCombinerInfo &DCI) {
5209 SDValue N0 = N->getOperand(0);
5210 SDValue N1 = N->getOperand(1);
5211
5212 // First try with the default operand order.
5213 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI);
5214 if (Result.getNode())
5215 return Result;
5216
5217 // If that didn't work, try again with the operands commuted.
5218 return PerformADDCombineWithOperands(N, N1, N0, DCI);
5219}
5220
Chris Lattnerd1980a52009-03-12 06:52:53 +00005221/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00005222///
Chris Lattnerd1980a52009-03-12 06:52:53 +00005223static SDValue PerformSUBCombine(SDNode *N,
5224 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005225 SDValue N0 = N->getOperand(0);
5226 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00005227
Chris Lattnerd1980a52009-03-12 06:52:53 +00005228 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
5229 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
5230 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
5231 if (Result.getNode()) return Result;
5232 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00005233
Chris Lattnerd1980a52009-03-12 06:52:53 +00005234 return SDValue();
5235}
5236
Evan Cheng463d3582011-03-31 19:38:48 +00005237/// PerformVMULCombine
5238/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
5239/// special multiplier accumulator forwarding.
5240/// vmul d3, d0, d2
5241/// vmla d3, d1, d2
5242/// is faster than
5243/// vadd d3, d0, d1
5244/// vmul d3, d3, d2
5245static SDValue PerformVMULCombine(SDNode *N,
5246 TargetLowering::DAGCombinerInfo &DCI,
5247 const ARMSubtarget *Subtarget) {
5248 if (!Subtarget->hasVMLxForwarding())
5249 return SDValue();
5250
5251 SelectionDAG &DAG = DCI.DAG;
5252 SDValue N0 = N->getOperand(0);
5253 SDValue N1 = N->getOperand(1);
5254 unsigned Opcode = N0.getOpcode();
5255 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5256 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
5257 Opcode = N0.getOpcode();
5258 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5259 Opcode != ISD::FADD && Opcode != ISD::FSUB)
5260 return SDValue();
5261 std::swap(N0, N1);
5262 }
5263
5264 EVT VT = N->getValueType(0);
5265 DebugLoc DL = N->getDebugLoc();
5266 SDValue N00 = N0->getOperand(0);
5267 SDValue N01 = N0->getOperand(1);
5268 return DAG.getNode(Opcode, DL, VT,
5269 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
5270 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
5271}
5272
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005273static SDValue PerformMULCombine(SDNode *N,
5274 TargetLowering::DAGCombinerInfo &DCI,
5275 const ARMSubtarget *Subtarget) {
5276 SelectionDAG &DAG = DCI.DAG;
5277
5278 if (Subtarget->isThumb1Only())
5279 return SDValue();
5280
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005281 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5282 return SDValue();
5283
5284 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00005285 if (VT.is64BitVector() || VT.is128BitVector())
5286 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005287 if (VT != MVT::i32)
5288 return SDValue();
5289
5290 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
5291 if (!C)
5292 return SDValue();
5293
5294 uint64_t MulAmt = C->getZExtValue();
5295 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
5296 ShiftAmt = ShiftAmt & (32 - 1);
5297 SDValue V = N->getOperand(0);
5298 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005299
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005300 SDValue Res;
5301 MulAmt >>= ShiftAmt;
5302 if (isPowerOf2_32(MulAmt - 1)) {
5303 // (mul x, 2^N + 1) => (add (shl x, N), x)
5304 Res = DAG.getNode(ISD::ADD, DL, VT,
5305 V, DAG.getNode(ISD::SHL, DL, VT,
5306 V, DAG.getConstant(Log2_32(MulAmt-1),
5307 MVT::i32)));
5308 } else if (isPowerOf2_32(MulAmt + 1)) {
5309 // (mul x, 2^N - 1) => (sub (shl x, N), x)
5310 Res = DAG.getNode(ISD::SUB, DL, VT,
5311 DAG.getNode(ISD::SHL, DL, VT,
5312 V, DAG.getConstant(Log2_32(MulAmt+1),
5313 MVT::i32)),
5314 V);
5315 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005316 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005317
5318 if (ShiftAmt != 0)
5319 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
5320 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005321
5322 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005323 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005324 return SDValue();
5325}
5326
Owen Anderson080c0922010-11-05 19:27:46 +00005327static SDValue PerformANDCombine(SDNode *N,
5328 TargetLowering::DAGCombinerInfo &DCI) {
Eric Christopher29aeed12011-03-26 01:21:03 +00005329
Owen Anderson080c0922010-11-05 19:27:46 +00005330 // Attempt to use immediate-form VBIC
5331 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5332 DebugLoc dl = N->getDebugLoc();
5333 EVT VT = N->getValueType(0);
5334 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005335
Owen Anderson080c0922010-11-05 19:27:46 +00005336 APInt SplatBits, SplatUndef;
5337 unsigned SplatBitSize;
5338 bool HasAnyUndefs;
5339 if (BVN &&
5340 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5341 if (SplatBitSize <= 64) {
5342 EVT VbicVT;
5343 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
5344 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005345 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005346 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00005347 if (Val.getNode()) {
5348 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005349 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00005350 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005351 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00005352 }
5353 }
5354 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005355
Owen Anderson080c0922010-11-05 19:27:46 +00005356 return SDValue();
5357}
5358
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005359/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
5360static SDValue PerformORCombine(SDNode *N,
5361 TargetLowering::DAGCombinerInfo &DCI,
5362 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00005363 // Attempt to use immediate-form VORR
5364 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5365 DebugLoc dl = N->getDebugLoc();
5366 EVT VT = N->getValueType(0);
5367 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005368
Owen Anderson60f48702010-11-03 23:15:26 +00005369 APInt SplatBits, SplatUndef;
5370 unsigned SplatBitSize;
5371 bool HasAnyUndefs;
5372 if (BVN && Subtarget->hasNEON() &&
5373 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5374 if (SplatBitSize <= 64) {
5375 EVT VorrVT;
5376 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
5377 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005378 DAG, VorrVT, VT.is128BitVector(),
5379 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00005380 if (Val.getNode()) {
5381 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005382 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00005383 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005384 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00005385 }
5386 }
5387 }
5388
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005389 SDValue N0 = N->getOperand(0);
5390 if (N0.getOpcode() != ISD::AND)
5391 return SDValue();
5392 SDValue N1 = N->getOperand(1);
5393
5394 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
5395 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
5396 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
5397 APInt SplatUndef;
5398 unsigned SplatBitSize;
5399 bool HasAnyUndefs;
5400
5401 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
5402 APInt SplatBits0;
5403 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
5404 HasAnyUndefs) && !HasAnyUndefs) {
5405 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
5406 APInt SplatBits1;
5407 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
5408 HasAnyUndefs) && !HasAnyUndefs &&
5409 SplatBits0 == ~SplatBits1) {
5410 // Canonicalize the vector type to make instruction selection simpler.
5411 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
5412 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
5413 N0->getOperand(1), N0->getOperand(0),
5414 N1->getOperand(1));
5415 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
5416 }
5417 }
5418 }
5419
Jim Grosbach54238562010-07-17 03:30:54 +00005420 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
5421 // reasonable.
5422
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005423 // BFI is only available on V6T2+
5424 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
5425 return SDValue();
5426
Jim Grosbach54238562010-07-17 03:30:54 +00005427 DebugLoc DL = N->getDebugLoc();
5428 // 1) or (and A, mask), val => ARMbfi A, val, mask
5429 // iff (val & mask) == val
5430 //
5431 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
5432 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005433 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005434 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005435 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005436 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005437
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005438 if (VT != MVT::i32)
5439 return SDValue();
5440
Evan Cheng30fb13f2010-12-13 20:32:54 +00005441 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00005442
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005443 // The value and the mask need to be constants so we can verify this is
5444 // actually a bitfield set. If the mask is 0xffff, we can do better
5445 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00005446 SDValue MaskOp = N0.getOperand(1);
5447 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
5448 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005449 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005450 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005451 if (Mask == 0xffff)
5452 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005453 SDValue Res;
5454 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005455 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
5456 if (N1C) {
5457 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005458 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00005459 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005460
Evan Chenga9688c42010-12-11 04:11:38 +00005461 if (ARM::isBitFieldInvertedMask(Mask)) {
5462 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005463
Evan Cheng30fb13f2010-12-13 20:32:54 +00005464 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00005465 DAG.getConstant(Val, MVT::i32),
5466 DAG.getConstant(Mask, MVT::i32));
5467
5468 // Do not add new nodes to DAG combiner worklist.
5469 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005470 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005471 }
Jim Grosbach54238562010-07-17 03:30:54 +00005472 } else if (N1.getOpcode() == ISD::AND) {
5473 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005474 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5475 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00005476 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005477 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005478
Eric Christopher29aeed12011-03-26 01:21:03 +00005479 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
5480 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00005481 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005482 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005483 // The pack halfword instruction works better for masks that fit it,
5484 // so use that when it's available.
5485 if (Subtarget->hasT2ExtractPack() &&
5486 (Mask == 0xffff || Mask == 0xffff0000))
5487 return SDValue();
5488 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00005489 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00005490 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00005491 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00005492 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00005493 DAG.getConstant(Mask, MVT::i32));
5494 // Do not add new nodes to DAG combiner worklist.
5495 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005496 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005497 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005498 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005499 // The pack halfword instruction works better for masks that fit it,
5500 // so use that when it's available.
5501 if (Subtarget->hasT2ExtractPack() &&
5502 (Mask2 == 0xffff || Mask2 == 0xffff0000))
5503 return SDValue();
5504 // 2b
5505 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005506 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00005507 DAG.getConstant(lsb, MVT::i32));
5508 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00005509 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00005510 // Do not add new nodes to DAG combiner worklist.
5511 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005512 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005513 }
5514 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005515
Evan Cheng30fb13f2010-12-13 20:32:54 +00005516 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
5517 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
5518 ARM::isBitFieldInvertedMask(~Mask)) {
5519 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
5520 // where lsb(mask) == #shamt and masked bits of B are known zero.
5521 SDValue ShAmt = N00.getOperand(1);
5522 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
5523 unsigned LSB = CountTrailingZeros_32(Mask);
5524 if (ShAmtC != LSB)
5525 return SDValue();
5526
5527 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
5528 DAG.getConstant(~Mask, MVT::i32));
5529
5530 // Do not add new nodes to DAG combiner worklist.
5531 DCI.CombineTo(N, Res, false);
5532 }
5533
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005534 return SDValue();
5535}
5536
Evan Cheng0c1aec12010-12-14 03:22:07 +00005537/// PerformBFICombine - (bfi A, (and B, C1), C2) -> (bfi A, B, C2) iff
5538/// C1 & C2 == C1.
5539static SDValue PerformBFICombine(SDNode *N,
5540 TargetLowering::DAGCombinerInfo &DCI) {
5541 SDValue N1 = N->getOperand(1);
5542 if (N1.getOpcode() == ISD::AND) {
5543 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5544 if (!N11C)
5545 return SDValue();
5546 unsigned Mask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
5547 unsigned Mask2 = N11C->getZExtValue();
5548 if ((Mask & Mask2) == Mask2)
5549 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
5550 N->getOperand(0), N1.getOperand(0),
5551 N->getOperand(2));
5552 }
5553 return SDValue();
5554}
5555
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005556/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
5557/// ARMISD::VMOVRRD.
5558static SDValue PerformVMOVRRDCombine(SDNode *N,
5559 TargetLowering::DAGCombinerInfo &DCI) {
5560 // vmovrrd(vmovdrr x, y) -> x,y
5561 SDValue InDouble = N->getOperand(0);
5562 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
5563 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
5564 return SDValue();
5565}
5566
5567/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
5568/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
5569static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
5570 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
5571 SDValue Op0 = N->getOperand(0);
5572 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005573 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005574 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005575 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005576 Op1 = Op1.getOperand(0);
5577 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
5578 Op0.getNode() == Op1.getNode() &&
5579 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005580 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005581 N->getValueType(0), Op0.getOperand(0));
5582 return SDValue();
5583}
5584
Bob Wilson31600902010-12-21 06:43:19 +00005585/// PerformSTORECombine - Target-specific dag combine xforms for
5586/// ISD::STORE.
5587static SDValue PerformSTORECombine(SDNode *N,
5588 TargetLowering::DAGCombinerInfo &DCI) {
5589 // Bitcast an i64 store extracted from a vector to f64.
5590 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5591 StoreSDNode *St = cast<StoreSDNode>(N);
5592 SDValue StVal = St->getValue();
5593 if (!ISD::isNormalStore(St) || St->isVolatile() ||
5594 StVal.getValueType() != MVT::i64 ||
5595 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5596 return SDValue();
5597
5598 SelectionDAG &DAG = DCI.DAG;
5599 DebugLoc dl = StVal.getDebugLoc();
5600 SDValue IntVec = StVal.getOperand(0);
5601 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5602 IntVec.getValueType().getVectorNumElements());
5603 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
5604 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5605 Vec, StVal.getOperand(1));
5606 dl = N->getDebugLoc();
5607 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
5608 // Make the DAGCombiner fold the bitcasts.
5609 DCI.AddToWorklist(Vec.getNode());
5610 DCI.AddToWorklist(ExtElt.getNode());
5611 DCI.AddToWorklist(V.getNode());
5612 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
5613 St->getPointerInfo(), St->isVolatile(),
5614 St->isNonTemporal(), St->getAlignment(),
5615 St->getTBAAInfo());
5616}
5617
5618/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
5619/// are normal, non-volatile loads. If so, it is profitable to bitcast an
5620/// i64 vector to have f64 elements, since the value can then be loaded
5621/// directly into a VFP register.
5622static bool hasNormalLoadOperand(SDNode *N) {
5623 unsigned NumElts = N->getValueType(0).getVectorNumElements();
5624 for (unsigned i = 0; i < NumElts; ++i) {
5625 SDNode *Elt = N->getOperand(i).getNode();
5626 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
5627 return true;
5628 }
5629 return false;
5630}
5631
Bob Wilson75f02882010-09-17 22:59:05 +00005632/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
5633/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00005634static SDValue PerformBUILD_VECTORCombine(SDNode *N,
5635 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00005636 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
5637 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
5638 // into a pair of GPRs, which is fine when the value is used as a scalar,
5639 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00005640 SelectionDAG &DAG = DCI.DAG;
5641 if (N->getNumOperands() == 2) {
5642 SDValue RV = PerformVMOVDRRCombine(N, DAG);
5643 if (RV.getNode())
5644 return RV;
5645 }
Bob Wilson75f02882010-09-17 22:59:05 +00005646
Bob Wilson31600902010-12-21 06:43:19 +00005647 // Load i64 elements as f64 values so that type legalization does not split
5648 // them up into i32 values.
5649 EVT VT = N->getValueType(0);
5650 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
5651 return SDValue();
5652 DebugLoc dl = N->getDebugLoc();
5653 SmallVector<SDValue, 8> Ops;
5654 unsigned NumElts = VT.getVectorNumElements();
5655 for (unsigned i = 0; i < NumElts; ++i) {
5656 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
5657 Ops.push_back(V);
5658 // Make the DAGCombiner fold the bitcast.
5659 DCI.AddToWorklist(V.getNode());
5660 }
5661 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
5662 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
5663 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
5664}
5665
5666/// PerformInsertEltCombine - Target-specific dag combine xforms for
5667/// ISD::INSERT_VECTOR_ELT.
5668static SDValue PerformInsertEltCombine(SDNode *N,
5669 TargetLowering::DAGCombinerInfo &DCI) {
5670 // Bitcast an i64 load inserted into a vector to f64.
5671 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5672 EVT VT = N->getValueType(0);
5673 SDNode *Elt = N->getOperand(1).getNode();
5674 if (VT.getVectorElementType() != MVT::i64 ||
5675 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
5676 return SDValue();
5677
5678 SelectionDAG &DAG = DCI.DAG;
5679 DebugLoc dl = N->getDebugLoc();
5680 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5681 VT.getVectorNumElements());
5682 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
5683 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
5684 // Make the DAGCombiner fold the bitcasts.
5685 DCI.AddToWorklist(Vec.getNode());
5686 DCI.AddToWorklist(V.getNode());
5687 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
5688 Vec, V, N->getOperand(2));
5689 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00005690}
5691
Bob Wilsonf20700c2010-10-27 20:38:28 +00005692/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
5693/// ISD::VECTOR_SHUFFLE.
5694static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
5695 // The LLVM shufflevector instruction does not require the shuffle mask
5696 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
5697 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
5698 // operands do not match the mask length, they are extended by concatenating
5699 // them with undef vectors. That is probably the right thing for other
5700 // targets, but for NEON it is better to concatenate two double-register
5701 // size vector operands into a single quad-register size vector. Do that
5702 // transformation here:
5703 // shuffle(concat(v1, undef), concat(v2, undef)) ->
5704 // shuffle(concat(v1, v2), undef)
5705 SDValue Op0 = N->getOperand(0);
5706 SDValue Op1 = N->getOperand(1);
5707 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
5708 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
5709 Op0.getNumOperands() != 2 ||
5710 Op1.getNumOperands() != 2)
5711 return SDValue();
5712 SDValue Concat0Op1 = Op0.getOperand(1);
5713 SDValue Concat1Op1 = Op1.getOperand(1);
5714 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
5715 Concat1Op1.getOpcode() != ISD::UNDEF)
5716 return SDValue();
5717 // Skip the transformation if any of the types are illegal.
5718 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5719 EVT VT = N->getValueType(0);
5720 if (!TLI.isTypeLegal(VT) ||
5721 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
5722 !TLI.isTypeLegal(Concat1Op1.getValueType()))
5723 return SDValue();
5724
5725 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
5726 Op0.getOperand(0), Op1.getOperand(0));
5727 // Translate the shuffle mask.
5728 SmallVector<int, 16> NewMask;
5729 unsigned NumElts = VT.getVectorNumElements();
5730 unsigned HalfElts = NumElts/2;
5731 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
5732 for (unsigned n = 0; n < NumElts; ++n) {
5733 int MaskElt = SVN->getMaskElt(n);
5734 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005735 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00005736 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005737 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00005738 NewElt = HalfElts + MaskElt - NumElts;
5739 NewMask.push_back(NewElt);
5740 }
5741 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
5742 DAG.getUNDEF(VT), NewMask.data());
5743}
5744
Bob Wilson1c3ef902011-02-07 17:43:21 +00005745/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
5746/// NEON load/store intrinsics to merge base address updates.
5747static SDValue CombineBaseUpdate(SDNode *N,
5748 TargetLowering::DAGCombinerInfo &DCI) {
5749 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5750 return SDValue();
5751
5752 SelectionDAG &DAG = DCI.DAG;
5753 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
5754 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
5755 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
5756 SDValue Addr = N->getOperand(AddrOpIdx);
5757
5758 // Search for a use of the address operand that is an increment.
5759 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
5760 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
5761 SDNode *User = *UI;
5762 if (User->getOpcode() != ISD::ADD ||
5763 UI.getUse().getResNo() != Addr.getResNo())
5764 continue;
5765
5766 // Check that the add is independent of the load/store. Otherwise, folding
5767 // it would create a cycle.
5768 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
5769 continue;
5770
5771 // Find the new opcode for the updating load/store.
5772 bool isLoad = true;
5773 bool isLaneOp = false;
5774 unsigned NewOpc = 0;
5775 unsigned NumVecs = 0;
5776 if (isIntrinsic) {
5777 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
5778 switch (IntNo) {
5779 default: assert(0 && "unexpected intrinsic for Neon base update");
5780 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
5781 NumVecs = 1; break;
5782 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
5783 NumVecs = 2; break;
5784 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
5785 NumVecs = 3; break;
5786 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
5787 NumVecs = 4; break;
5788 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
5789 NumVecs = 2; isLaneOp = true; break;
5790 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
5791 NumVecs = 3; isLaneOp = true; break;
5792 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
5793 NumVecs = 4; isLaneOp = true; break;
5794 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
5795 NumVecs = 1; isLoad = false; break;
5796 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
5797 NumVecs = 2; isLoad = false; break;
5798 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
5799 NumVecs = 3; isLoad = false; break;
5800 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
5801 NumVecs = 4; isLoad = false; break;
5802 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
5803 NumVecs = 2; isLoad = false; isLaneOp = true; break;
5804 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
5805 NumVecs = 3; isLoad = false; isLaneOp = true; break;
5806 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
5807 NumVecs = 4; isLoad = false; isLaneOp = true; break;
5808 }
5809 } else {
5810 isLaneOp = true;
5811 switch (N->getOpcode()) {
5812 default: assert(0 && "unexpected opcode for Neon base update");
5813 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
5814 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
5815 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
5816 }
5817 }
5818
5819 // Find the size of memory referenced by the load/store.
5820 EVT VecTy;
5821 if (isLoad)
5822 VecTy = N->getValueType(0);
5823 else
5824 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
5825 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
5826 if (isLaneOp)
5827 NumBytes /= VecTy.getVectorNumElements();
5828
5829 // If the increment is a constant, it must match the memory ref size.
5830 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
5831 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
5832 uint64_t IncVal = CInc->getZExtValue();
5833 if (IncVal != NumBytes)
5834 continue;
5835 } else if (NumBytes >= 3 * 16) {
5836 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
5837 // separate instructions that make it harder to use a non-constant update.
5838 continue;
5839 }
5840
5841 // Create the new updating load/store node.
5842 EVT Tys[6];
5843 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
5844 unsigned n;
5845 for (n = 0; n < NumResultVecs; ++n)
5846 Tys[n] = VecTy;
5847 Tys[n++] = MVT::i32;
5848 Tys[n] = MVT::Other;
5849 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
5850 SmallVector<SDValue, 8> Ops;
5851 Ops.push_back(N->getOperand(0)); // incoming chain
5852 Ops.push_back(N->getOperand(AddrOpIdx));
5853 Ops.push_back(Inc);
5854 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
5855 Ops.push_back(N->getOperand(i));
5856 }
5857 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
5858 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
5859 Ops.data(), Ops.size(),
5860 MemInt->getMemoryVT(),
5861 MemInt->getMemOperand());
5862
5863 // Update the uses.
5864 std::vector<SDValue> NewResults;
5865 for (unsigned i = 0; i < NumResultVecs; ++i) {
5866 NewResults.push_back(SDValue(UpdN.getNode(), i));
5867 }
5868 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
5869 DCI.CombineTo(N, NewResults);
5870 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
5871
5872 break;
5873 }
5874 return SDValue();
5875}
5876
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005877/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
5878/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
5879/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
5880/// return true.
5881static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
5882 SelectionDAG &DAG = DCI.DAG;
5883 EVT VT = N->getValueType(0);
5884 // vldN-dup instructions only support 64-bit vectors for N > 1.
5885 if (!VT.is64BitVector())
5886 return false;
5887
5888 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
5889 SDNode *VLD = N->getOperand(0).getNode();
5890 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
5891 return false;
5892 unsigned NumVecs = 0;
5893 unsigned NewOpc = 0;
5894 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
5895 if (IntNo == Intrinsic::arm_neon_vld2lane) {
5896 NumVecs = 2;
5897 NewOpc = ARMISD::VLD2DUP;
5898 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
5899 NumVecs = 3;
5900 NewOpc = ARMISD::VLD3DUP;
5901 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
5902 NumVecs = 4;
5903 NewOpc = ARMISD::VLD4DUP;
5904 } else {
5905 return false;
5906 }
5907
5908 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
5909 // numbers match the load.
5910 unsigned VLDLaneNo =
5911 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
5912 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
5913 UI != UE; ++UI) {
5914 // Ignore uses of the chain result.
5915 if (UI.getUse().getResNo() == NumVecs)
5916 continue;
5917 SDNode *User = *UI;
5918 if (User->getOpcode() != ARMISD::VDUPLANE ||
5919 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
5920 return false;
5921 }
5922
5923 // Create the vldN-dup node.
5924 EVT Tys[5];
5925 unsigned n;
5926 for (n = 0; n < NumVecs; ++n)
5927 Tys[n] = VT;
5928 Tys[n] = MVT::Other;
5929 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
5930 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
5931 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
5932 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
5933 Ops, 2, VLDMemInt->getMemoryVT(),
5934 VLDMemInt->getMemOperand());
5935
5936 // Update the uses.
5937 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
5938 UI != UE; ++UI) {
5939 unsigned ResNo = UI.getUse().getResNo();
5940 // Ignore uses of the chain result.
5941 if (ResNo == NumVecs)
5942 continue;
5943 SDNode *User = *UI;
5944 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
5945 }
5946
5947 // Now the vldN-lane intrinsic is dead except for its chain result.
5948 // Update uses of the chain.
5949 std::vector<SDValue> VLDDupResults;
5950 for (unsigned n = 0; n < NumVecs; ++n)
5951 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
5952 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
5953 DCI.CombineTo(VLD, VLDDupResults);
5954
5955 return true;
5956}
5957
Bob Wilson9e82bf12010-07-14 01:22:12 +00005958/// PerformVDUPLANECombine - Target-specific dag combine xforms for
5959/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005960static SDValue PerformVDUPLANECombine(SDNode *N,
5961 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00005962 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005963
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005964 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
5965 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
5966 if (CombineVLDDUP(N, DCI))
5967 return SDValue(N, 0);
5968
5969 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
5970 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005971 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00005972 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00005973 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00005974 return SDValue();
5975
5976 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
5977 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
5978 // The canonical VMOV for a zero vector uses a 32-bit element size.
5979 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5980 unsigned EltBits;
5981 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
5982 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005983 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005984 if (EltSize > VT.getVectorElementType().getSizeInBits())
5985 return SDValue();
5986
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005987 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005988}
5989
Bob Wilson5bafff32009-06-22 23:27:02 +00005990/// getVShiftImm - Check if this is a valid build_vector for the immediate
5991/// operand of a vector shift operation, where all the elements of the
5992/// build_vector must have the same constant integer value.
5993static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
5994 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005995 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00005996 Op = Op.getOperand(0);
5997 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
5998 APInt SplatBits, SplatUndef;
5999 unsigned SplatBitSize;
6000 bool HasAnyUndefs;
6001 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
6002 HasAnyUndefs, ElementBits) ||
6003 SplatBitSize > ElementBits)
6004 return false;
6005 Cnt = SplatBits.getSExtValue();
6006 return true;
6007}
6008
6009/// isVShiftLImm - Check if this is a valid build_vector for the immediate
6010/// operand of a vector shift left operation. That value must be in the range:
6011/// 0 <= Value < ElementBits for a left shift; or
6012/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006013static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006014 assert(VT.isVector() && "vector shift count is not a vector type");
6015 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6016 if (! getVShiftImm(Op, ElementBits, Cnt))
6017 return false;
6018 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
6019}
6020
6021/// isVShiftRImm - Check if this is a valid build_vector for the immediate
6022/// operand of a vector shift right operation. For a shift opcode, the value
6023/// is positive, but for an intrinsic the value count must be negative. The
6024/// absolute value must be in the range:
6025/// 1 <= |Value| <= ElementBits for a right shift; or
6026/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006027static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00006028 int64_t &Cnt) {
6029 assert(VT.isVector() && "vector shift count is not a vector type");
6030 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6031 if (! getVShiftImm(Op, ElementBits, Cnt))
6032 return false;
6033 if (isIntrinsic)
6034 Cnt = -Cnt;
6035 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
6036}
6037
6038/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
6039static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
6040 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
6041 switch (IntNo) {
6042 default:
6043 // Don't do anything for most intrinsics.
6044 break;
6045
6046 // Vector shifts: check for immediate versions and lower them.
6047 // Note: This is done during DAG combining instead of DAG legalizing because
6048 // the build_vectors for 64-bit vector element shift counts are generally
6049 // not legal, and it is hard to see their values after they get legalized to
6050 // loads from a constant pool.
6051 case Intrinsic::arm_neon_vshifts:
6052 case Intrinsic::arm_neon_vshiftu:
6053 case Intrinsic::arm_neon_vshiftls:
6054 case Intrinsic::arm_neon_vshiftlu:
6055 case Intrinsic::arm_neon_vshiftn:
6056 case Intrinsic::arm_neon_vrshifts:
6057 case Intrinsic::arm_neon_vrshiftu:
6058 case Intrinsic::arm_neon_vrshiftn:
6059 case Intrinsic::arm_neon_vqshifts:
6060 case Intrinsic::arm_neon_vqshiftu:
6061 case Intrinsic::arm_neon_vqshiftsu:
6062 case Intrinsic::arm_neon_vqshiftns:
6063 case Intrinsic::arm_neon_vqshiftnu:
6064 case Intrinsic::arm_neon_vqshiftnsu:
6065 case Intrinsic::arm_neon_vqrshiftns:
6066 case Intrinsic::arm_neon_vqrshiftnu:
6067 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00006068 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006069 int64_t Cnt;
6070 unsigned VShiftOpc = 0;
6071
6072 switch (IntNo) {
6073 case Intrinsic::arm_neon_vshifts:
6074 case Intrinsic::arm_neon_vshiftu:
6075 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
6076 VShiftOpc = ARMISD::VSHL;
6077 break;
6078 }
6079 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
6080 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
6081 ARMISD::VSHRs : ARMISD::VSHRu);
6082 break;
6083 }
6084 return SDValue();
6085
6086 case Intrinsic::arm_neon_vshiftls:
6087 case Intrinsic::arm_neon_vshiftlu:
6088 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
6089 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006090 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006091
6092 case Intrinsic::arm_neon_vrshifts:
6093 case Intrinsic::arm_neon_vrshiftu:
6094 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
6095 break;
6096 return SDValue();
6097
6098 case Intrinsic::arm_neon_vqshifts:
6099 case Intrinsic::arm_neon_vqshiftu:
6100 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6101 break;
6102 return SDValue();
6103
6104 case Intrinsic::arm_neon_vqshiftsu:
6105 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6106 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006107 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006108
6109 case Intrinsic::arm_neon_vshiftn:
6110 case Intrinsic::arm_neon_vrshiftn:
6111 case Intrinsic::arm_neon_vqshiftns:
6112 case Intrinsic::arm_neon_vqshiftnu:
6113 case Intrinsic::arm_neon_vqshiftnsu:
6114 case Intrinsic::arm_neon_vqrshiftns:
6115 case Intrinsic::arm_neon_vqrshiftnu:
6116 case Intrinsic::arm_neon_vqrshiftnsu:
6117 // Narrowing shifts require an immediate right shift.
6118 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
6119 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00006120 llvm_unreachable("invalid shift count for narrowing vector shift "
6121 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006122
6123 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006124 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00006125 }
6126
6127 switch (IntNo) {
6128 case Intrinsic::arm_neon_vshifts:
6129 case Intrinsic::arm_neon_vshiftu:
6130 // Opcode already set above.
6131 break;
6132 case Intrinsic::arm_neon_vshiftls:
6133 case Intrinsic::arm_neon_vshiftlu:
6134 if (Cnt == VT.getVectorElementType().getSizeInBits())
6135 VShiftOpc = ARMISD::VSHLLi;
6136 else
6137 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
6138 ARMISD::VSHLLs : ARMISD::VSHLLu);
6139 break;
6140 case Intrinsic::arm_neon_vshiftn:
6141 VShiftOpc = ARMISD::VSHRN; break;
6142 case Intrinsic::arm_neon_vrshifts:
6143 VShiftOpc = ARMISD::VRSHRs; break;
6144 case Intrinsic::arm_neon_vrshiftu:
6145 VShiftOpc = ARMISD::VRSHRu; break;
6146 case Intrinsic::arm_neon_vrshiftn:
6147 VShiftOpc = ARMISD::VRSHRN; break;
6148 case Intrinsic::arm_neon_vqshifts:
6149 VShiftOpc = ARMISD::VQSHLs; break;
6150 case Intrinsic::arm_neon_vqshiftu:
6151 VShiftOpc = ARMISD::VQSHLu; break;
6152 case Intrinsic::arm_neon_vqshiftsu:
6153 VShiftOpc = ARMISD::VQSHLsu; break;
6154 case Intrinsic::arm_neon_vqshiftns:
6155 VShiftOpc = ARMISD::VQSHRNs; break;
6156 case Intrinsic::arm_neon_vqshiftnu:
6157 VShiftOpc = ARMISD::VQSHRNu; break;
6158 case Intrinsic::arm_neon_vqshiftnsu:
6159 VShiftOpc = ARMISD::VQSHRNsu; break;
6160 case Intrinsic::arm_neon_vqrshiftns:
6161 VShiftOpc = ARMISD::VQRSHRNs; break;
6162 case Intrinsic::arm_neon_vqrshiftnu:
6163 VShiftOpc = ARMISD::VQRSHRNu; break;
6164 case Intrinsic::arm_neon_vqrshiftnsu:
6165 VShiftOpc = ARMISD::VQRSHRNsu; break;
6166 }
6167
6168 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006169 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006170 }
6171
6172 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00006173 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006174 int64_t Cnt;
6175 unsigned VShiftOpc = 0;
6176
6177 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
6178 VShiftOpc = ARMISD::VSLI;
6179 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
6180 VShiftOpc = ARMISD::VSRI;
6181 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006182 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006183 }
6184
6185 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
6186 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00006187 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006188 }
6189
6190 case Intrinsic::arm_neon_vqrshifts:
6191 case Intrinsic::arm_neon_vqrshiftu:
6192 // No immediate versions of these to check for.
6193 break;
6194 }
6195
6196 return SDValue();
6197}
6198
6199/// PerformShiftCombine - Checks for immediate versions of vector shifts and
6200/// lowers them. As with the vector shift intrinsics, this is done during DAG
6201/// combining instead of DAG legalizing because the build_vectors for 64-bit
6202/// vector element shift counts are generally not legal, and it is hard to see
6203/// their values after they get legalized to loads from a constant pool.
6204static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
6205 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00006206 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00006207
6208 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00006209 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6210 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00006211 return SDValue();
6212
6213 assert(ST->hasNEON() && "unexpected vector shift");
6214 int64_t Cnt;
6215
6216 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006217 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006218
6219 case ISD::SHL:
6220 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
6221 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006222 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006223 break;
6224
6225 case ISD::SRA:
6226 case ISD::SRL:
6227 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
6228 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
6229 ARMISD::VSHRs : ARMISD::VSHRu);
6230 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006231 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006232 }
6233 }
6234 return SDValue();
6235}
6236
6237/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
6238/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
6239static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
6240 const ARMSubtarget *ST) {
6241 SDValue N0 = N->getOperand(0);
6242
6243 // Check for sign- and zero-extensions of vector extract operations of 8-
6244 // and 16-bit vector elements. NEON supports these directly. They are
6245 // handled during DAG combining because type legalization will promote them
6246 // to 32-bit types and it is messy to recognize the operations after that.
6247 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
6248 SDValue Vec = N0.getOperand(0);
6249 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006250 EVT VT = N->getValueType(0);
6251 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006252 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6253
Owen Anderson825b72b2009-08-11 20:47:22 +00006254 if (VT == MVT::i32 &&
6255 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00006256 TLI.isTypeLegal(Vec.getValueType()) &&
6257 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006258
6259 unsigned Opc = 0;
6260 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006261 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006262 case ISD::SIGN_EXTEND:
6263 Opc = ARMISD::VGETLANEs;
6264 break;
6265 case ISD::ZERO_EXTEND:
6266 case ISD::ANY_EXTEND:
6267 Opc = ARMISD::VGETLANEu;
6268 break;
6269 }
6270 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
6271 }
6272 }
6273
6274 return SDValue();
6275}
6276
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006277/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
6278/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
6279static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
6280 const ARMSubtarget *ST) {
6281 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00006282 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006283 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
6284 // a NaN; only do the transformation when it matches that behavior.
6285
6286 // For now only do this when using NEON for FP operations; if using VFP, it
6287 // is not obvious that the benefit outweighs the cost of switching to the
6288 // NEON pipeline.
6289 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
6290 N->getValueType(0) != MVT::f32)
6291 return SDValue();
6292
6293 SDValue CondLHS = N->getOperand(0);
6294 SDValue CondRHS = N->getOperand(1);
6295 SDValue LHS = N->getOperand(2);
6296 SDValue RHS = N->getOperand(3);
6297 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
6298
6299 unsigned Opcode = 0;
6300 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00006301 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006302 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00006303 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006304 IsReversed = true ; // x CC y ? y : x
6305 } else {
6306 return SDValue();
6307 }
6308
Bob Wilsone742bb52010-02-24 22:15:53 +00006309 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006310 switch (CC) {
6311 default: break;
6312 case ISD::SETOLT:
6313 case ISD::SETOLE:
6314 case ISD::SETLT:
6315 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006316 case ISD::SETULT:
6317 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006318 // If LHS is NaN, an ordered comparison will be false and the result will
6319 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
6320 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6321 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
6322 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6323 break;
6324 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
6325 // will return -0, so vmin can only be used for unsafe math or if one of
6326 // the operands is known to be nonzero.
6327 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
6328 !UnsafeFPMath &&
6329 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6330 break;
6331 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006332 break;
6333
6334 case ISD::SETOGT:
6335 case ISD::SETOGE:
6336 case ISD::SETGT:
6337 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006338 case ISD::SETUGT:
6339 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006340 // If LHS is NaN, an ordered comparison will be false and the result will
6341 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
6342 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6343 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
6344 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6345 break;
6346 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
6347 // will return +0, so vmax can only be used for unsafe math or if one of
6348 // the operands is known to be nonzero.
6349 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
6350 !UnsafeFPMath &&
6351 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6352 break;
6353 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006354 break;
6355 }
6356
6357 if (!Opcode)
6358 return SDValue();
6359 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
6360}
6361
Dan Gohman475871a2008-07-27 21:46:04 +00006362SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006363 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006364 switch (N->getOpcode()) {
6365 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006366 case ISD::ADD: return PerformADDCombine(N, DCI);
6367 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006368 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006369 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00006370 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00006371 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00006372 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006373 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00006374 case ISD::STORE: return PerformSTORECombine(N, DCI);
6375 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
6376 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00006377 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006378 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006379 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00006380 case ISD::SHL:
6381 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006382 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00006383 case ISD::SIGN_EXTEND:
6384 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006385 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
6386 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Bob Wilson1c3ef902011-02-07 17:43:21 +00006387 case ARMISD::VLD2DUP:
6388 case ARMISD::VLD3DUP:
6389 case ARMISD::VLD4DUP:
6390 return CombineBaseUpdate(N, DCI);
6391 case ISD::INTRINSIC_VOID:
6392 case ISD::INTRINSIC_W_CHAIN:
6393 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
6394 case Intrinsic::arm_neon_vld1:
6395 case Intrinsic::arm_neon_vld2:
6396 case Intrinsic::arm_neon_vld3:
6397 case Intrinsic::arm_neon_vld4:
6398 case Intrinsic::arm_neon_vld2lane:
6399 case Intrinsic::arm_neon_vld3lane:
6400 case Intrinsic::arm_neon_vld4lane:
6401 case Intrinsic::arm_neon_vst1:
6402 case Intrinsic::arm_neon_vst2:
6403 case Intrinsic::arm_neon_vst3:
6404 case Intrinsic::arm_neon_vst4:
6405 case Intrinsic::arm_neon_vst2lane:
6406 case Intrinsic::arm_neon_vst3lane:
6407 case Intrinsic::arm_neon_vst4lane:
6408 return CombineBaseUpdate(N, DCI);
6409 default: break;
6410 }
6411 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006412 }
Dan Gohman475871a2008-07-27 21:46:04 +00006413 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006414}
6415
Evan Cheng31959b12011-02-02 01:06:55 +00006416bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
6417 EVT VT) const {
6418 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
6419}
6420
Bill Wendlingaf566342009-08-15 21:21:19 +00006421bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00006422 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00006423 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00006424
6425 switch (VT.getSimpleVT().SimpleTy) {
6426 default:
6427 return false;
6428 case MVT::i8:
6429 case MVT::i16:
6430 case MVT::i32:
6431 return true;
6432 // FIXME: VLD1 etc with standard alignment is legal.
6433 }
6434}
6435
Evan Chenge6c835f2009-08-14 20:09:37 +00006436static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
6437 if (V < 0)
6438 return false;
6439
6440 unsigned Scale = 1;
6441 switch (VT.getSimpleVT().SimpleTy) {
6442 default: return false;
6443 case MVT::i1:
6444 case MVT::i8:
6445 // Scale == 1;
6446 break;
6447 case MVT::i16:
6448 // Scale == 2;
6449 Scale = 2;
6450 break;
6451 case MVT::i32:
6452 // Scale == 4;
6453 Scale = 4;
6454 break;
6455 }
6456
6457 if ((V & (Scale - 1)) != 0)
6458 return false;
6459 V /= Scale;
6460 return V == (V & ((1LL << 5) - 1));
6461}
6462
6463static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
6464 const ARMSubtarget *Subtarget) {
6465 bool isNeg = false;
6466 if (V < 0) {
6467 isNeg = true;
6468 V = - V;
6469 }
6470
6471 switch (VT.getSimpleVT().SimpleTy) {
6472 default: return false;
6473 case MVT::i1:
6474 case MVT::i8:
6475 case MVT::i16:
6476 case MVT::i32:
6477 // + imm12 or - imm8
6478 if (isNeg)
6479 return V == (V & ((1LL << 8) - 1));
6480 return V == (V & ((1LL << 12) - 1));
6481 case MVT::f32:
6482 case MVT::f64:
6483 // Same as ARM mode. FIXME: NEON?
6484 if (!Subtarget->hasVFP2())
6485 return false;
6486 if ((V & 3) != 0)
6487 return false;
6488 V >>= 2;
6489 return V == (V & ((1LL << 8) - 1));
6490 }
6491}
6492
Evan Chengb01fad62007-03-12 23:30:29 +00006493/// isLegalAddressImmediate - Return true if the integer value can be used
6494/// as the offset of the target addressing mode for load / store of the
6495/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00006496static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006497 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00006498 if (V == 0)
6499 return true;
6500
Evan Cheng65011532009-03-09 19:15:00 +00006501 if (!VT.isSimple())
6502 return false;
6503
Evan Chenge6c835f2009-08-14 20:09:37 +00006504 if (Subtarget->isThumb1Only())
6505 return isLegalT1AddressImmediate(V, VT);
6506 else if (Subtarget->isThumb2())
6507 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00006508
Evan Chenge6c835f2009-08-14 20:09:37 +00006509 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00006510 if (V < 0)
6511 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00006512 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00006513 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006514 case MVT::i1:
6515 case MVT::i8:
6516 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00006517 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006518 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006519 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00006520 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006521 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006522 case MVT::f32:
6523 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00006524 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00006525 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00006526 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00006527 return false;
6528 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006529 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00006530 }
Evan Chenga8e29892007-01-19 07:51:42 +00006531}
6532
Evan Chenge6c835f2009-08-14 20:09:37 +00006533bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
6534 EVT VT) const {
6535 int Scale = AM.Scale;
6536 if (Scale < 0)
6537 return false;
6538
6539 switch (VT.getSimpleVT().SimpleTy) {
6540 default: return false;
6541 case MVT::i1:
6542 case MVT::i8:
6543 case MVT::i16:
6544 case MVT::i32:
6545 if (Scale == 1)
6546 return true;
6547 // r + r << imm
6548 Scale = Scale & ~1;
6549 return Scale == 2 || Scale == 4 || Scale == 8;
6550 case MVT::i64:
6551 // r + r
6552 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
6553 return true;
6554 return false;
6555 case MVT::isVoid:
6556 // Note, we allow "void" uses (basically, uses that aren't loads or
6557 // stores), because arm allows folding a scale into many arithmetic
6558 // operations. This should be made more precise and revisited later.
6559
6560 // Allow r << imm, but the imm has to be a multiple of two.
6561 if (Scale & 1) return false;
6562 return isPowerOf2_32(Scale);
6563 }
6564}
6565
Chris Lattner37caf8c2007-04-09 23:33:39 +00006566/// isLegalAddressingMode - Return true if the addressing mode represented
6567/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006568bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006569 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006570 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00006571 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00006572 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006573
Chris Lattner37caf8c2007-04-09 23:33:39 +00006574 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006575 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006576 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006577
Chris Lattner37caf8c2007-04-09 23:33:39 +00006578 switch (AM.Scale) {
6579 case 0: // no scale reg, must be "r+i" or "r", or "i".
6580 break;
6581 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00006582 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00006583 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006584 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00006585 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006586 // ARM doesn't support any R+R*scale+imm addr modes.
6587 if (AM.BaseOffs)
6588 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006589
Bob Wilson2c7dab12009-04-08 17:55:28 +00006590 if (!VT.isSimple())
6591 return false;
6592
Evan Chenge6c835f2009-08-14 20:09:37 +00006593 if (Subtarget->isThumb2())
6594 return isLegalT2ScaledAddressingMode(AM, VT);
6595
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006596 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00006597 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00006598 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006599 case MVT::i1:
6600 case MVT::i8:
6601 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006602 if (Scale < 0) Scale = -Scale;
6603 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006604 return true;
6605 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00006606 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006607 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00006608 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00006609 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006610 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006611 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00006612 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006613
Owen Anderson825b72b2009-08-11 20:47:22 +00006614 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00006615 // Note, we allow "void" uses (basically, uses that aren't loads or
6616 // stores), because arm allows folding a scale into many arithmetic
6617 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006618
Chris Lattner37caf8c2007-04-09 23:33:39 +00006619 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00006620 if (Scale & 1) return false;
6621 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00006622 }
6623 break;
Evan Chengb01fad62007-03-12 23:30:29 +00006624 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00006625 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00006626}
6627
Evan Cheng77e47512009-11-11 19:05:52 +00006628/// isLegalICmpImmediate - Return true if the specified immediate is legal
6629/// icmp immediate, that is the target has icmp instructions which can compare
6630/// a register against the immediate without having to materialize the
6631/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00006632bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00006633 if (!Subtarget->isThumb())
6634 return ARM_AM::getSOImmVal(Imm) != -1;
6635 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00006636 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00006637 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00006638}
6639
Owen Andersone50ed302009-08-10 22:56:29 +00006640static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00006641 bool isSEXTLoad, SDValue &Base,
6642 SDValue &Offset, bool &isInc,
6643 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00006644 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
6645 return false;
6646
Owen Anderson825b72b2009-08-11 20:47:22 +00006647 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00006648 // AddressingMode 3
6649 Base = Ptr->getOperand(0);
6650 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006651 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00006652 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006653 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00006654 isInc = false;
6655 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6656 return true;
6657 }
6658 }
6659 isInc = (Ptr->getOpcode() == ISD::ADD);
6660 Offset = Ptr->getOperand(1);
6661 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00006662 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00006663 // AddressingMode 2
6664 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006665 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00006666 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006667 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00006668 isInc = false;
6669 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6670 Base = Ptr->getOperand(0);
6671 return true;
6672 }
6673 }
6674
6675 if (Ptr->getOpcode() == ISD::ADD) {
6676 isInc = true;
6677 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
6678 if (ShOpcVal != ARM_AM::no_shift) {
6679 Base = Ptr->getOperand(1);
6680 Offset = Ptr->getOperand(0);
6681 } else {
6682 Base = Ptr->getOperand(0);
6683 Offset = Ptr->getOperand(1);
6684 }
6685 return true;
6686 }
6687
6688 isInc = (Ptr->getOpcode() == ISD::ADD);
6689 Base = Ptr->getOperand(0);
6690 Offset = Ptr->getOperand(1);
6691 return true;
6692 }
6693
Jim Grosbache5165492009-11-09 00:11:35 +00006694 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00006695 return false;
6696}
6697
Owen Andersone50ed302009-08-10 22:56:29 +00006698static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00006699 bool isSEXTLoad, SDValue &Base,
6700 SDValue &Offset, bool &isInc,
6701 SelectionDAG &DAG) {
6702 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
6703 return false;
6704
6705 Base = Ptr->getOperand(0);
6706 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
6707 int RHSC = (int)RHS->getZExtValue();
6708 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
6709 assert(Ptr->getOpcode() == ISD::ADD);
6710 isInc = false;
6711 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6712 return true;
6713 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
6714 isInc = Ptr->getOpcode() == ISD::ADD;
6715 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
6716 return true;
6717 }
6718 }
6719
6720 return false;
6721}
6722
Evan Chenga8e29892007-01-19 07:51:42 +00006723/// getPreIndexedAddressParts - returns true by value, base pointer and
6724/// offset pointer and addressing mode by reference if the node's address
6725/// can be legally represented as pre-indexed load / store address.
6726bool
Dan Gohman475871a2008-07-27 21:46:04 +00006727ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
6728 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006729 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006730 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006731 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006732 return false;
6733
Owen Andersone50ed302009-08-10 22:56:29 +00006734 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006735 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006736 bool isSEXTLoad = false;
6737 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
6738 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006739 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006740 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6741 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
6742 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006743 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006744 } else
6745 return false;
6746
6747 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006748 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006749 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006750 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
6751 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006752 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006753 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00006754 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00006755 if (!isLegal)
6756 return false;
6757
6758 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
6759 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006760}
6761
6762/// getPostIndexedAddressParts - returns true by value, base pointer and
6763/// offset pointer and addressing mode by reference if this node can be
6764/// combined with a load / store to form a post-indexed load / store.
6765bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00006766 SDValue &Base,
6767 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006768 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006769 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006770 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006771 return false;
6772
Owen Andersone50ed302009-08-10 22:56:29 +00006773 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006774 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006775 bool isSEXTLoad = false;
6776 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006777 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006778 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006779 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6780 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006781 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006782 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006783 } else
6784 return false;
6785
6786 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006787 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006788 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006789 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00006790 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006791 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006792 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
6793 isInc, DAG);
6794 if (!isLegal)
6795 return false;
6796
Evan Cheng28dad2a2010-05-18 21:31:17 +00006797 if (Ptr != Base) {
6798 // Swap base ptr and offset to catch more post-index load / store when
6799 // it's legal. In Thumb2 mode, offset must be an immediate.
6800 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
6801 !Subtarget->isThumb2())
6802 std::swap(Base, Offset);
6803
6804 // Post-indexed load / store update the base pointer.
6805 if (Ptr != Base)
6806 return false;
6807 }
6808
Evan Chenge88d5ce2009-07-02 07:28:31 +00006809 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
6810 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006811}
6812
Dan Gohman475871a2008-07-27 21:46:04 +00006813void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00006814 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006815 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006816 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00006817 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00006818 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006819 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00006820 switch (Op.getOpcode()) {
6821 default: break;
6822 case ARMISD::CMOV: {
6823 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00006824 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00006825 if (KnownZero == 0 && KnownOne == 0) return;
6826
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006827 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00006828 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
6829 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00006830 KnownZero &= KnownZeroRHS;
6831 KnownOne &= KnownOneRHS;
6832 return;
6833 }
6834 }
6835}
6836
6837//===----------------------------------------------------------------------===//
6838// ARM Inline Assembly Support
6839//===----------------------------------------------------------------------===//
6840
Evan Cheng55d42002011-01-08 01:24:27 +00006841bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
6842 // Looking for "rev" which is V6+.
6843 if (!Subtarget->hasV6Ops())
6844 return false;
6845
6846 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
6847 std::string AsmStr = IA->getAsmString();
6848 SmallVector<StringRef, 4> AsmPieces;
6849 SplitString(AsmStr, AsmPieces, ";\n");
6850
6851 switch (AsmPieces.size()) {
6852 default: return false;
6853 case 1:
6854 AsmStr = AsmPieces[0];
6855 AsmPieces.clear();
6856 SplitString(AsmStr, AsmPieces, " \t,");
6857
6858 // rev $0, $1
6859 if (AsmPieces.size() == 3 &&
6860 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
6861 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
6862 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
6863 if (Ty && Ty->getBitWidth() == 32)
6864 return IntrinsicLowering::LowerToByteSwap(CI);
6865 }
6866 break;
6867 }
6868
6869 return false;
6870}
6871
Evan Chenga8e29892007-01-19 07:51:42 +00006872/// getConstraintType - Given a constraint letter, return the type of
6873/// constraint it is for this target.
6874ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00006875ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
6876 if (Constraint.size() == 1) {
6877 switch (Constraint[0]) {
6878 default: break;
6879 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006880 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00006881 }
Evan Chenga8e29892007-01-19 07:51:42 +00006882 }
Chris Lattner4234f572007-03-25 02:14:49 +00006883 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00006884}
6885
John Thompson44ab89e2010-10-29 17:29:13 +00006886/// Examine constraint type and operand type and determine a weight value.
6887/// This object must already have been set up with the operand type
6888/// and the current alternative constraint selected.
6889TargetLowering::ConstraintWeight
6890ARMTargetLowering::getSingleConstraintMatchWeight(
6891 AsmOperandInfo &info, const char *constraint) const {
6892 ConstraintWeight weight = CW_Invalid;
6893 Value *CallOperandVal = info.CallOperandVal;
6894 // If we don't have a value, we can't do a match,
6895 // but allow it at the lowest weight.
6896 if (CallOperandVal == NULL)
6897 return CW_Default;
6898 const Type *type = CallOperandVal->getType();
6899 // Look at the constraint type.
6900 switch (*constraint) {
6901 default:
6902 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
6903 break;
6904 case 'l':
6905 if (type->isIntegerTy()) {
6906 if (Subtarget->isThumb())
6907 weight = CW_SpecificReg;
6908 else
6909 weight = CW_Register;
6910 }
6911 break;
6912 case 'w':
6913 if (type->isFloatingPointTy())
6914 weight = CW_Register;
6915 break;
6916 }
6917 return weight;
6918}
6919
Bob Wilson2dc4f542009-03-20 22:42:55 +00006920std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00006921ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00006922 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006923 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00006924 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00006925 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006926 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00006927 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00006928 return std::make_pair(0U, ARM::tGPRRegisterClass);
6929 else
6930 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006931 case 'r':
6932 return std::make_pair(0U, ARM::GPRRegisterClass);
6933 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00006934 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006935 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00006936 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006937 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00006938 if (VT.getSizeInBits() == 128)
6939 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006940 break;
Evan Chenga8e29892007-01-19 07:51:42 +00006941 }
6942 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00006943 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00006944 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00006945
Evan Chenga8e29892007-01-19 07:51:42 +00006946 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6947}
6948
6949std::vector<unsigned> ARMTargetLowering::
6950getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00006951 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006952 if (Constraint.size() != 1)
6953 return std::vector<unsigned>();
6954
6955 switch (Constraint[0]) { // GCC ARM Constraint Letters
6956 default: break;
6957 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00006958 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
6959 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
6960 0);
Evan Chenga8e29892007-01-19 07:51:42 +00006961 case 'r':
6962 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
6963 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
6964 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
6965 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006966 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00006967 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006968 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
6969 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
6970 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
6971 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
6972 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
6973 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
6974 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
6975 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00006976 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006977 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
6978 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
6979 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
6980 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00006981 if (VT.getSizeInBits() == 128)
6982 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
6983 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006984 break;
Evan Chenga8e29892007-01-19 07:51:42 +00006985 }
6986
6987 return std::vector<unsigned>();
6988}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006989
6990/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6991/// vector. If it is invalid, don't add anything to Ops.
6992void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
6993 char Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006994 std::vector<SDValue>&Ops,
6995 SelectionDAG &DAG) const {
6996 SDValue Result(0, 0);
6997
6998 switch (Constraint) {
6999 default: break;
7000 case 'I': case 'J': case 'K': case 'L':
7001 case 'M': case 'N': case 'O':
7002 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
7003 if (!C)
7004 return;
7005
7006 int64_t CVal64 = C->getSExtValue();
7007 int CVal = (int) CVal64;
7008 // None of these constraints allow values larger than 32 bits. Check
7009 // that the value fits in an int.
7010 if (CVal != CVal64)
7011 return;
7012
7013 switch (Constraint) {
7014 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007015 if (Subtarget->isThumb1Only()) {
7016 // This must be a constant between 0 and 255, for ADD
7017 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007018 if (CVal >= 0 && CVal <= 255)
7019 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007020 } else if (Subtarget->isThumb2()) {
7021 // A constant that can be used as an immediate value in a
7022 // data-processing instruction.
7023 if (ARM_AM::getT2SOImmVal(CVal) != -1)
7024 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007025 } else {
7026 // A constant that can be used as an immediate value in a
7027 // data-processing instruction.
7028 if (ARM_AM::getSOImmVal(CVal) != -1)
7029 break;
7030 }
7031 return;
7032
7033 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007034 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007035 // This must be a constant between -255 and -1, for negated ADD
7036 // immediates. This can be used in GCC with an "n" modifier that
7037 // prints the negated value, for use with SUB instructions. It is
7038 // not useful otherwise but is implemented for compatibility.
7039 if (CVal >= -255 && CVal <= -1)
7040 break;
7041 } else {
7042 // This must be a constant between -4095 and 4095. It is not clear
7043 // what this constraint is intended for. Implemented for
7044 // compatibility with GCC.
7045 if (CVal >= -4095 && CVal <= 4095)
7046 break;
7047 }
7048 return;
7049
7050 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007051 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007052 // A 32-bit value where only one byte has a nonzero value. Exclude
7053 // zero to match GCC. This constraint is used by GCC internally for
7054 // constants that can be loaded with a move/shift combination.
7055 // It is not useful otherwise but is implemented for compatibility.
7056 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
7057 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007058 } else if (Subtarget->isThumb2()) {
7059 // A constant whose bitwise inverse can be used as an immediate
7060 // value in a data-processing instruction. This can be used in GCC
7061 // with a "B" modifier that prints the inverted value, for use with
7062 // BIC and MVN instructions. It is not useful otherwise but is
7063 // implemented for compatibility.
7064 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
7065 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007066 } else {
7067 // A constant whose bitwise inverse can be used as an immediate
7068 // value in a data-processing instruction. This can be used in GCC
7069 // with a "B" modifier that prints the inverted value, for use with
7070 // BIC and MVN instructions. It is not useful otherwise but is
7071 // implemented for compatibility.
7072 if (ARM_AM::getSOImmVal(~CVal) != -1)
7073 break;
7074 }
7075 return;
7076
7077 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007078 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007079 // This must be a constant between -7 and 7,
7080 // for 3-operand ADD/SUB immediate instructions.
7081 if (CVal >= -7 && CVal < 7)
7082 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007083 } else if (Subtarget->isThumb2()) {
7084 // A constant whose negation can be used as an immediate value in a
7085 // data-processing instruction. This can be used in GCC with an "n"
7086 // modifier that prints the negated value, for use with SUB
7087 // instructions. It is not useful otherwise but is implemented for
7088 // compatibility.
7089 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
7090 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007091 } else {
7092 // A constant whose negation can be used as an immediate value in a
7093 // data-processing instruction. This can be used in GCC with an "n"
7094 // modifier that prints the negated value, for use with SUB
7095 // instructions. It is not useful otherwise but is implemented for
7096 // compatibility.
7097 if (ARM_AM::getSOImmVal(-CVal) != -1)
7098 break;
7099 }
7100 return;
7101
7102 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007103 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007104 // This must be a multiple of 4 between 0 and 1020, for
7105 // ADD sp + immediate.
7106 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
7107 break;
7108 } else {
7109 // A power of two or a constant between 0 and 32. This is used in
7110 // GCC for the shift amount on shifted register operands, but it is
7111 // useful in general for any shift amounts.
7112 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
7113 break;
7114 }
7115 return;
7116
7117 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007118 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007119 // This must be a constant between 0 and 31, for shift amounts.
7120 if (CVal >= 0 && CVal <= 31)
7121 break;
7122 }
7123 return;
7124
7125 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007126 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007127 // This must be a multiple of 4 between -508 and 508, for
7128 // ADD/SUB sp = sp + immediate.
7129 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
7130 break;
7131 }
7132 return;
7133 }
7134 Result = DAG.getTargetConstant(CVal, Op.getValueType());
7135 break;
7136 }
7137
7138 if (Result.getNode()) {
7139 Ops.push_back(Result);
7140 return;
7141 }
Dale Johannesen1784d162010-06-25 21:55:36 +00007142 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007143}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00007144
7145bool
7146ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7147 // The ARM target isn't yet aware of offsets.
7148 return false;
7149}
Evan Cheng39382422009-10-28 01:44:26 +00007150
7151int ARM::getVFPf32Imm(const APFloat &FPImm) {
7152 APInt Imm = FPImm.bitcastToAPInt();
7153 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
7154 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
7155 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
7156
7157 // We can handle 4 bits of mantissa.
7158 // mantissa = (16+UInt(e:f:g:h))/16.
7159 if (Mantissa & 0x7ffff)
7160 return -1;
7161 Mantissa >>= 19;
7162 if ((Mantissa & 0xf) != Mantissa)
7163 return -1;
7164
7165 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7166 if (Exp < -3 || Exp > 4)
7167 return -1;
7168 Exp = ((Exp+3) & 0x7) ^ 4;
7169
7170 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7171}
7172
7173int ARM::getVFPf64Imm(const APFloat &FPImm) {
7174 APInt Imm = FPImm.bitcastToAPInt();
7175 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
7176 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
7177 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
7178
7179 // We can handle 4 bits of mantissa.
7180 // mantissa = (16+UInt(e:f:g:h))/16.
7181 if (Mantissa & 0xffffffffffffLL)
7182 return -1;
7183 Mantissa >>= 48;
7184 if ((Mantissa & 0xf) != Mantissa)
7185 return -1;
7186
7187 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7188 if (Exp < -3 || Exp > 4)
7189 return -1;
7190 Exp = ((Exp+3) & 0x7) ^ 4;
7191
7192 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7193}
7194
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007195bool ARM::isBitFieldInvertedMask(unsigned v) {
7196 if (v == 0xffffffff)
7197 return 0;
7198 // there can be 1's on either or both "outsides", all the "inside"
7199 // bits must be 0's
7200 unsigned int lsb = 0, msb = 31;
7201 while (v & (1 << msb)) --msb;
7202 while (v & (1 << lsb)) ++lsb;
7203 for (unsigned int i = lsb; i <= msb; ++i) {
7204 if (v & (1 << i))
7205 return 0;
7206 }
7207 return 1;
7208}
7209
Evan Cheng39382422009-10-28 01:44:26 +00007210/// isFPImmLegal - Returns true if the target can instruction select the
7211/// specified FP immediate natively. If false, the legalizer will
7212/// materialize the FP immediate as a load from a constant pool.
7213bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
7214 if (!Subtarget->hasVFP3())
7215 return false;
7216 if (VT == MVT::f32)
7217 return ARM::getVFPf32Imm(Imm) != -1;
7218 if (VT == MVT::f64)
7219 return ARM::getVFPf64Imm(Imm) != -1;
7220 return false;
7221}
Bob Wilson65ffec42010-09-21 17:56:22 +00007222
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007223/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00007224/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
7225/// specified in the intrinsic calls.
7226bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
7227 const CallInst &I,
7228 unsigned Intrinsic) const {
7229 switch (Intrinsic) {
7230 case Intrinsic::arm_neon_vld1:
7231 case Intrinsic::arm_neon_vld2:
7232 case Intrinsic::arm_neon_vld3:
7233 case Intrinsic::arm_neon_vld4:
7234 case Intrinsic::arm_neon_vld2lane:
7235 case Intrinsic::arm_neon_vld3lane:
7236 case Intrinsic::arm_neon_vld4lane: {
7237 Info.opc = ISD::INTRINSIC_W_CHAIN;
7238 // Conservatively set memVT to the entire set of vectors loaded.
7239 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
7240 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7241 Info.ptrVal = I.getArgOperand(0);
7242 Info.offset = 0;
7243 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7244 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7245 Info.vol = false; // volatile loads with NEON intrinsics not supported
7246 Info.readMem = true;
7247 Info.writeMem = false;
7248 return true;
7249 }
7250 case Intrinsic::arm_neon_vst1:
7251 case Intrinsic::arm_neon_vst2:
7252 case Intrinsic::arm_neon_vst3:
7253 case Intrinsic::arm_neon_vst4:
7254 case Intrinsic::arm_neon_vst2lane:
7255 case Intrinsic::arm_neon_vst3lane:
7256 case Intrinsic::arm_neon_vst4lane: {
7257 Info.opc = ISD::INTRINSIC_VOID;
7258 // Conservatively set memVT to the entire set of vectors stored.
7259 unsigned NumElts = 0;
7260 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
7261 const Type *ArgTy = I.getArgOperand(ArgI)->getType();
7262 if (!ArgTy->isVectorTy())
7263 break;
7264 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
7265 }
7266 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7267 Info.ptrVal = I.getArgOperand(0);
7268 Info.offset = 0;
7269 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7270 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7271 Info.vol = false; // volatile stores with NEON intrinsics not supported
7272 Info.readMem = false;
7273 Info.writeMem = true;
7274 return true;
7275 }
7276 default:
7277 break;
7278 }
7279
7280 return false;
7281}